# Dockerfile for simtools production.
#
# Uses CORSIKA and sim_telarray pre-built images; the selected CORSIKA image
# determines the available AVX optimization.
# No QGSJet tables are included.
#
# hadolint global ignore=DL3013,DL3041
# - DL3013, DL3041: ignore warnings about using latest

ARG CORSIKA_IMAGE
ARG SIMTEL_IMAGE
ARG BASE_IMAGE

FROM ${CORSIKA_IMAGE} AS corsika_img
FROM ${SIMTEL_IMAGE} AS simtel_img

FROM ${BASE_IMAGE}
ARG PYTHON_VERSION
ARG SIMTOOLS_VERSION
ARG SIMTOOLS_GIT_REVISION
ARG CORSIKA_IMAGE
ARG SIMTEL_IMAGE
ARG BASE_IMAGE
ARG CONTAINER_USER="simtools"
ARG CONTAINER_UID="1000"
ARG CONTAINER_GID="1000"

COPY --from=corsika_img /workdir/simulation_software/corsika7  /workdir/simulation_software/corsika7
COPY --from=simtel_img /workdir/simulation_software/sim_telarray /workdir/simulation_software/sim_telarray
COPY --from=simtel_img /workdir/simulation_software/hessioxxx /workdir/simulation_software/hessioxxx
COPY --from=simtel_img /workdir/simulation_software/stdtools /workdir/simulation_software/stdtools
COPY --from=simtel_img /usr/local/lib/ /usr/local/lib/

RUN microdnf update -y && microdnf install -y \
  bc bzip2 findutils gcc-c++ git libgfortran procps wget zstd \
  python"${PYTHON_VERSION}" python"${PYTHON_VERSION}"-pip python"${PYTHON_VERSION}"-devel && \
  microdnf clean all && \
  rm -rf /var/cache/dnf/* /tmp/* /var/tmp/* && \
  ln -sf /usr/bin/python"${PYTHON_VERSION}" /usr/bin/python && \
  groupadd --gid "${CONTAINER_GID}" "${CONTAINER_USER}" && \
  useradd --uid "${CONTAINER_UID}" --gid "${CONTAINER_USER}" \
    --create-home --home-dir "/home/${CONTAINER_USER}" --shell /bin/bash "${CONTAINER_USER}" && \
  echo /usr/local/lib > /etc/ld.so.conf.d/local.conf && \
  ldconfig

WORKDIR /workdir

COPY LICENSE README.md dependency_versions.yml pyproject.toml /workdir/simtools/
COPY src /workdir/simtools/src

ENV SIMTOOLS_SIM_TELARRAY_PATH="/workdir/simulation_software/sim_telarray"
ENV SIMTOOLS_CORSIKA_PATH="/workdir/simulation_software/corsika7"
ENV SIMTOOLS_DEPENDENCY_MANIFEST="/opt/simtools/provenance/dependency-manifest.json"
ENV SIMTOOLS_GIT_REVISION=${SIMTOOLS_GIT_REVISION}
ENV SIMTOOLS_BASE_IMAGE=${BASE_IMAGE}
ENV SIMTOOLS_CORSIKA_IMAGE=${CORSIKA_IMAGE}
ENV SIMTOOLS_SIMTEL_IMAGE=${SIMTEL_IMAGE}

# hadolint ignore=DL4006
RUN python"${PYTHON_VERSION}" -m venv env \
  && . env/bin/activate \
  && python -m pip install --no-cache-dir --upgrade pip \
  && SETUPTOOLS_SCM_PRETEND_VERSION_FOR_GAMMASIMTOOLS="${SIMTOOLS_VERSION}" \
    pip install --no-cache-dir /workdir/simtools \
  && SIMTOOLS_CONTAINER_BUILD=1 simtools-dependency-manifest \
  && pip cache purge && rm -rf /root/.cache \
  && mkdir -p /opt/simtools/provenance \
  && if [ ! -f /opt/simtools/provenance/dependency-manifest.json.sha256 ]; then \
       if [ ! -f /opt/simtools/provenance/dependency-manifest.json ]; then \
         python -c 'from simtools.dependencies import write_dependency_manifest; \
write_dependency_manifest("/opt/simtools/provenance/dependency-manifest.json")'; \
    fi \
    && digest="$(python -c 'from pathlib import Path; \
from simtools.dependencies import get_dependency_manifest_digest; \
manifest=Path("/opt/simtools/provenance/dependency-manifest.json"); \
print(get_dependency_manifest_digest() + "  " + manifest.name)')"; \
    printf '%s\n' "$digest" > /opt/simtools/provenance/dependency-manifest.json.sha256; \
     fi \
  && echo ". /workdir/env/bin/activate" >> "/home/${CONTAINER_USER}/.bashrc" \
  && chown -R "${CONTAINER_UID}:${CONTAINER_GID}" /workdir "/home/${CONTAINER_USER}"

ENV LD_LIBRARY_PATH="/workdir/simulation_software/hessioxxx/lib:\
/workdir/simulation_software/stdtools/lib:\
/workdir/simulation_software/sim_telarray/LightEmission/lib:\
/usr/local/lib"
ENV PATH="/workdir/env/bin/:$PATH"
ENV HOME="/home/${CONTAINER_USER}"
# hadolint ignore=DL3066
USER ${CONTAINER_UID}:${CONTAINER_GID}

LABEL org.opencontainers.image.source="https://github.com/gammasim/simtools" \
      org.opencontainers.image.revision="${SIMTOOLS_GIT_REVISION}" \
      org.opencontainers.image.version="${SIMTOOLS_VERSION}" \
      org.gammasim.simtools.dependency-manifest="/opt/simtools/provenance/dependency-manifest.json"
