#################################################################################################
# The "mina-daemon-auto-hardfork" stage
# - Continues from the shared base-deps image, like every other staged service. This replaces the
#   last monolithic Dockerfile, which carried its own copy of the base layer and had drifted:
#   it installed google-cloud-sdk/kubectl/gke-gcloud-auth-plugin 462.0.1-0 from apt, while the
#   shared base installs the ${GCLOUD_VERSION} (476.0.0) SDK tarball and adds the same two
#   components via `gcloud components install`. The apt dependency list was already identical.
# - Installs the POSTFORK metapackage (mina-${network}-postfork-mesa) at deb_version and then the
#   legacy PREFORK package at deb_legacy_version, so one image can run either side of the fork.
#   This is why it cannot extend the mina-daemon stage: that stage installs the generic daemon
#   package (mina-generic), which is a different package set.
# - Sets MINA_HARDFORK_STATE_DIR; the entrypoint/user/puppeteer tail matches the daemon stage.
#################################################################################################
FROM base-deps AS mina-daemon-auto-hardfork

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

ENV MINA_DEB=mina-${network}-postfork-mesa

# Mina daemon package
# jemalloc is also installed automatically here to match the package dependencies for this $deb_codename.
# Both the postfork metapackage and the legacy prefork package are installed
# DIRECTLY from the local build context: scripts/docker/build.sh stages the
# freshly-built .deb files (and, via read_all_from_cache.sh, the legacy-root
# debs) 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). The postfork metapackage depends on mina-logproc, which
# has no repo to resolve it, so it is requested explicitly as a local file. The
# prefork package's (unversioned) mina-logproc dependency is already satisfied
# by that install, so the legacy prefork step only needs the prefork package.
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 echo "Building image with version $deb_version from repo $deb_release $deb_codename for network $network" \
  && chmod +x /usr/local/bin/install-mina-debs.sh \
  && /usr/local/bin/install-mina-debs.sh \
       "${MINA_DEB}=$deb_version" \
       "mina-logproc=$deb_version"

RUN echo "Installing legacy package with version $deb_legacy_version from repo $deb_release $deb_codename for network $network" \
  && /usr/local/bin/install-mina-debs.sh \
       "mina-${network}-prefork-mesa=$deb_legacy_version"

# Move to a non-root UID in the future (specifically 50000, as it is memorable and safely within the bounds of most systems)
# for now stick to root for compatibility
ARG UID=0

# TODO: flesh out this feature + test
# Create a mina user to execute the daemon with
RUN mkdir /home/mina \
  && adduser --uid 50000 --disabled-password --gecos '' mina \
  && passwd -l mina \
  && chown -R mina:mina /home/mina

WORKDIR /home/mina
USER mina

## Reset workdir, USER, and ${UID} for root-owned version
WORKDIR /root/
USER 0

COPY --chown=${UID} scripts/healthcheck-utilities.sh /healthcheck/utilities.sh
COPY --chown=${UID} scripts/cron_job_dump_ledger.sh /cron_job_dump_ledger.sh
COPY --chown=${UID} scripts/daemon-entrypoint.sh /entrypoint.sh
# Solve this by marking scripts executable in git
COPY --chown=${UID} ./auxiliary_entrypoints /entrypoint.d

COPY --chown=${UID} puppeteer-context/* /
RUN chmod +x /mina_daemon_puppeteer.py /find_puppeteer.sh /start.sh /stop.sh

ENV MINA_TIME_OFFSET 0

ENV MINA_HARDFORK_STATE_DIR /root/.mina-config

# MINA_APP is intentionally left unset: daemon-entrypoint.sh defaults to bare
# `mina`, which PATH-resolves to /usr/local/bin/mina (the mina-dispatch symlink).
# Invoking via the `mina` symlink keeps the dispatcher out of its direct-invocation
# (debugging) mode, so `docker run <img> daemon ...` / `--version` work as expected.

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