#################################################################################################
# The "stateless verification build" Stage
# - builds stateless verification tool
# - should not include any data related to joining a specific network, only the node software itself
#################################################################################################
ARG image=europe-west3-docker.pkg.dev/o1labs-192920/euro-docker-repo/debian:bullseye-slim
FROM ${image}

ARG deb_version
ARG deb_codename=bullseye
ARG deb_release=unstable
ARG network=mainnet

ENV DEBIAN_FRONTEND noninteractive
RUN echo "Building image with version $deb_codename $deb_release $deb_version"

# Install ca-certificates using HTTP
RUN apt-get update --quiet --yes \
  && apt-get install --quiet --yes --no-install-recommends ca-certificates \
  && rm -rf /var/lib/apt/lists/*

# Switch to HTTPS
RUN if [ -f /etc/apt/sources.list ]; then \
  sed -i 's/http:\/\//https:\/\//g' /etc/apt/sources.list; \
else \
  sed -i 's/http:\/\//https:\/\//g' /etc/apt/sources.list.d/debian.sources; \
fi

# install mina delegation verifier
# Installed DIRECTLY from the local build context: scripts/docker/build.sh stages
# the freshly-built .deb files under _debs, which we COPY to /opt/mina-local-debs,
# and install-mina-debs.sh installs the resolved .deb files with apt-get (no apt
# repo / no apt index). mina-delegation-verify depends on mina-logproc, which
# has no repo to resolve it, so it is requested explicitly as a local file.
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
COPY scripts/install-mina-debs.sh /usr/local/bin/install-mina-debs.sh
COPY _debs /opt/mina-local-debs
RUN chmod +x /usr/local/bin/install-mina-debs.sh \
  && /usr/local/bin/install-mina-debs.sh \
       "mina-delegation-verify=$deb_version" \
       "mina-logproc=$deb_version"

# awscli and cqlsh-expansion are used by the delegation verification tool
RUN apt-get update && apt-get install -y --no-install-recommends tzdata python3 python3-pip python3-venv jq wget dnsutils gawk \
  && rm -rf /var/lib/apt/lists/*

# Create a virtual environment for Python tools
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV \
  && $VIRTUAL_ENV/bin/pip install --no-cache-dir awscli cqlsh-expansion pytz
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

# set home to root dir
ENV HOME="/root"

ENTRYPOINT ["mina-delegation-verify"]
