# Dockerfile for simtools development.
#
# Uses CORSIKA (no vector optimization) and sim_telarray pre-built images.
#
# CORSIKA and sim_telarray are installed, but not simtools itself.
# It is expected that simtools is installed in an external directory
# mounted in the container.
# CORSIKA interaction tables are expected to be cloned into an external directory
# and mounted to the container (set SIMTOOLS_CORSIKA_INTERACTION_TABLE_PATH accordingly).
#
# 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_GIT_REVISION
ARG CORSIKA_IMAGE
ARG SIMTEL_IMAGE
ARG BASE_IMAGE

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 diffutils findutils gcc-c++ git git-lfs libgfortran procps tree vim 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 && \
  echo /usr/local/lib > /etc/ld.so.conf.d/local.conf && \
  ldconfig && \
  { \
    printf '%s\n' "if [ -n \"\${GITHUB_WORKSPACE:-}\" ]; then"; \
    printf '%s\n' "    git config --global --add safe.directory \"\${GITHUB_WORKSPACE}\""; \
    printf '%s\n' "fi"; \
    printf '%s\n' "git config --global --add safe.directory \"/workdir\""; \
    printf '%s\n' "git config --global --add safe.directory \"/workdir/external\""; \
    printf '%s\n' "git config --global --add safe.directory \"/workdir/external/simtools\""; \
} > /etc/profile.d/git_safe_dir.sh

# Install the simtools development environment without installing simtools itself.
WORKDIR /workdir
COPY pyproject.toml /workdir/pyproject.toml
COPY dependency_versions.yml /workdir/dependency_versions.yml
COPY src /workdir/src
RUN python"${PYTHON_VERSION}" -m pip install --no-cache-dir --upgrade pip packaging pyyaml && \
    python"${PYTHON_VERSION}" /workdir/src/simtools/applications/dependency_versions.py \
      --pyproject /workdir/pyproject.toml --format python-requirements \
      --extras dev doc tests > /workdir/requirements-dev.txt && \
    python"${PYTHON_VERSION}" -m venv env && \
    . env/bin/activate && \
    python -m pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir --requirement /workdir/requirements-dev.txt && \
    SIMTOOLS_CONTAINER_BUILD=1 \
      SIMTOOLS_GIT_REVISION="${SIMTOOLS_GIT_REVISION}" \
      SIMTOOLS_BASE_IMAGE="${BASE_IMAGE}" \
      SIMTOOLS_CORSIKA_IMAGE="${CORSIKA_IMAGE}" \
      SIMTOOLS_SIMTEL_IMAGE="${SIMTEL_IMAGE}" \
      PYTHONPATH=/workdir/src python -m simtools.applications.dependency_manifest \
        --development \
        --project_file /workdir/pyproject.toml \
        --build_option_files \
          /workdir/simulation_software/corsika7/build_opts.yml \
          /workdir/simulation_software/sim_telarray/build_opts.yml && \
    echo ". /workdir/env/bin/activate" >> /root/.bashrc && \
    mkdir -p /workdir/external

ENV SIMTOOLS_SIM_TELARRAY_PATH="/workdir/simulation_software/sim_telarray"
ENV SIMTOOLS_CORSIKA_PATH="/workdir/simulation_software/corsika7"
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 SIMTOOLS_DEPENDENCY_MANIFEST="/opt/simtools/provenance/dependency-manifest.json"
SHELL ["/bin/bash", "-c"]

# Development requires writing to bind-mounted source trees and to the image virtual environment.
# A fixed non-root UID cannot portably match host ownership, especially with Podman Desktop.
WORKDIR /workdir/external

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