#################################################################################################
# The "mina-archive" stage
# - Continues from the shared base-deps image.
# - Adds the archive-specific dependencies (postgresql, postgresql-contrib, apt-utils, man),
#   the archive entrypoint, the healthcheck-utilities fetch and the archive deb install.
# - With the 3-layer packaging split, the per-network archive package is an empty tent that
#   depends on mina-archive-generic and the profile leaf package; since there is no apt repo
#   to resolve the closure (local debs only), those deps are passed explicitly.
#################################################################################################
FROM base-deps AS mina-archive

# In multi-stage builds, Docker requires re-declaring ARG variables after each FROM.
ARG deb_version
ARG deb_codename=bullseye
ARG deb_release=unstable
ARG network=mainnet
ARG deb_profile=devnet
ARG deb_suffix
ARG build_flags_suffix

# if --build-arg deb_suffix is defined, add hyphen at beginning.
ENV SUFFIX=${deb_suffix:+-${deb_suffix}}
ENV BUILD_FLAGS_SUFFIX=${build_flags_suffix}
# construct mina debian package name based on network and suffix.
# possible values:
# - mina-archive-mainnet
# - mina-archive-devnet-lightnet etc.
ENV MINA_DEB=mina-archive-${network}${SUFFIX}

# The per-network config package ships the runtime config JSON and, for hardfork
# networks, the genesis ledger tarball(s) into /var/lib/coda. Bundling it lets
# `mina-archive-hardfork-toolbox populate-genesis-accounts` (and `mina-archive
# run --config-file`) resolve the fork genesis ledger locally -- no S3, no
# network -- to backfill accounts_accessed for the fork genesis block.
#
# Unlike the daemon (see stages/daemon/2-mina-daemon), the archive image can carry a
# config safely: its entrypoint never picks up /var/lib/coda/* implicitly, so
# these files stay inert unless a command names them via --config-file. The
# archive image is also already per-network, so there is no network-free generic
# base for the -configured pattern to layer onto.
#
# Architecture: all, and no profile/build-flag suffix -- the package is named
# mina-<network>-config for every variant.
ENV MINA_CONFIG_DEB=mina-${network}-config

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

COPY scripts/archive-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Archive-specific dependencies (the shared deps + gcloud come from base-deps).
RUN apt-get update --quiet --yes \
    && apt-get install --quiet --yes --no-install-recommends \
        postgresql \
        postgresql-contrib \
        apt-utils \
        man \
    && rm -rf /var/lib/apt/lists/*

RUN mkdir /healthcheck && curl https://raw.githubusercontent.com/MinaProtocol/mina/develop/dockerfiles/scripts/healthcheck-utilities.sh -o /healthcheck/utilities.sh

# archive-node package (+ the per-network config so the hardfork toolbox can
# resolve the fork genesis ledger from /var/lib/coda)
# 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 file with a single apt-get
# call (no apt repo / no apt index). With the 3-layer split, the per-network
# archive package is an empty tent that depends on mina-archive-generic and the
# profile leaf package. These deps must be passed explicitly.
ENV MINA_ARCHIVE_GENERIC_DEB=mina-archive-generic${BUILD_FLAGS_SUFFIX}
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 \
  && case "${deb_profile}" in lightnet|dev) PROFILE_DEB="mina-${deb_profile}" ;; *) PROFILE_DEB="mina-${deb_profile}-profile" ;; esac \
  && APT_NO_DOWNGRADE=1 \
     /usr/local/bin/install-mina-debs.sh \
       "${MINA_DEB}=$deb_version" \
       "${MINA_ARCHIVE_GENERIC_DEB}=$deb_version" \
       "${PROFILE_DEB}=$deb_version" \
       "${MINA_CONFIG_DEB}=$deb_version"

ENTRYPOINT ["/usr/bin/dumb-init", "/entrypoint.sh"]
