#################################################################################################
# The "mina-rosetta" stage
# - Continues from the rosetta-base image.
# - Installs the mina rosetta/archive/daemon debs (3-layer split: generic + profile + per-network
#   tent, plus the network config for full builds), then sets up the postgres cluster and the
#   rosetta entrypoint.
#################################################################################################
FROM rosetta-base AS mina-rosetta

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

ARG MINA_DAEMON_PORT=8302
ARG MINA_CONFIG_DIR=/data/.mina-config

# if --build-arg deb_suffix is defined, add hyphen at beginning.
ENV SUFFIX=${deb_suffix:+-${deb_suffix}}
# build_flags_suffix contains only the build flags part (e.g. "instrumented")
# without custom suffixes like "generic". Used for packages like archive
# that get the instrumented suffix but not the generic suffix.
ENV BUILD_FLAGS_SUFFIX=${build_flags_suffix}
# construct mina debian package names based on network and suffix.
# The daemon binaries now live in the generic package, with the genesis/config
# data split out into a separate config package.
#
# Apps-only builds are invoked with deb_suffix=generic, so ${SUFFIX} already
# selects the generic package (e.g. -generic, -generic-instrumented) and the
# config package is intentionally omitted. Full builds do not pass a generic
# suffix, so the install step below adds "-generic" and also pulls the config
# package (reproducing the former monolithic mina-${network} package).
# possible values:
# - mina-${profile}-profile + mina-devnet-config (full image)
# - mina-${profile}-profile / mina-${profile}-profile-instrumented (apps-only, no config)
ENV MINA_CONFIG_DEB=mina-${network}-config
ENV MINA_ARCHIVE_DEB=mina-archive-${network}${BUILD_FLAGS_SUFFIX}
ENV MINA_ROSETTA_DEB=mina-rosetta-${network}${BUILD_FLAGS_SUFFIX}
ENV MINA_ARCHIVE_GENERIC_DEB=mina-archive-generic${BUILD_FLAGS_SUFFIX}
ENV MINA_ROSETTA_GENERIC_DEB=mina-rosetta-generic${BUILD_FLAGS_SUFFIX}

# Mina daemon package
# jemalloc is also installed automatically here to match the package dependencies for this $deb_codename.
# 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 a single apt-get
# call (no apt repo / no apt index). Since there is no repo to resolve the mina
# dependency closure, the daemon metapackage's dependencies (mina-logproc and
# mina-${network}-config) are requested explicitly as local files too.
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 \
    && case "${deb_profile}" in lightnet|dev) PROFILE_DEB="mina-${deb_profile}" ;; *) PROFILE_DEB="mina-${deb_profile}-profile" ;; esac \
    && if echo "${SUFFIX}" | grep -q -- '-generic'; then \
         /usr/local/bin/install-mina-debs.sh \
           "mina${SUFFIX}=$deb_version" \
           "mina-logproc=$deb_version" \
           "${MINA_ARCHIVE_GENERIC_DEB}=$deb_version" \
           "${MINA_ARCHIVE_DEB}=$deb_version" \
           "${MINA_ROSETTA_GENERIC_DEB}=$deb_version" \
           "${MINA_ROSETTA_DEB}=$deb_version" \
           "${PROFILE_DEB}=$deb_version" ; \
       else \
         /usr/local/bin/install-mina-debs.sh \
           "mina-generic${SUFFIX}=$deb_version" \
           "mina-logproc=$deb_version" \
           "${MINA_CONFIG_DEB}=$deb_version" \
           "${MINA_ARCHIVE_GENERIC_DEB}=$deb_version" \
           "${MINA_ARCHIVE_DEB}=$deb_version" \
           "${MINA_ROSETTA_GENERIC_DEB}=$deb_version" \
           "${MINA_ROSETTA_DEB}=$deb_version" \
           "${PROFILE_DEB}=$deb_version" ; \
       fi

# --- Set up postgres
USER postgres
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN POSTGRES_VERSION=$(psql -V | cut -d " " -f 3 | sed 's/.[[:digit:]]*$//g') \
    && echo "$POSTGRES_VERSION" "$(psql -V)" \
    && echo "host all  all    0.0.0.0/0  md5" >> "/etc/postgresql/${POSTGRES_VERSION}/main/pg_hba.conf" \
    && pg_dropcluster --stop "${POSTGRES_VERSION}" main
# Run as root so it can create /data/postgresql
USER root
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN POSTGRES_VERSION=$(psql -V | cut -d " " -f 3 | sed 's/.[[:digit:]]*$//g') \
    && pg_createcluster --start -d /data/postgresql --createclusterconf /etc/mina/rosetta/scripts/postgresql.conf "${POSTGRES_VERSION}" main

# --- Container workdir, ports, entrypoint, etc.
WORKDIR /etc/mina/rosetta/scripts

EXPOSE 3087
EXPOSE $MINA_DAEMON_PORT

ENTRYPOINT ["bash", "./docker-start.sh"]
