# 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_VERSION="v78010"
ARG SIMTEL_IMAGE_VERSION="latest"
ARG AVX_FLAG="generic"
ARG PYTHON_VERSION="3.12"
ARG ALMALINUX_VERSION="9.7"

FROM ghcr.io/gammasim/corsika7:${CORSIKA_IMAGE_VERSION}-${AVX_FLAG} AS corsika_img
FROM ghcr.io/gammasim/sim_telarray:${SIMTEL_IMAGE_VERSION} AS simtel_img

FROM almalinux:${ALMALINUX_VERSION}-minimal
ARG PYTHON_VERSION

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 && \
  echo /usr/local/lib > /etc/ld.so.conf.d/local.conf && \
  ldconfig

RUN { \
    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 simtools (main branch)
WORKDIR /workdir
RUN wget --quiet https://raw.githubusercontent.com/gammasim/simtools/main/pyproject.toml && \
    python${PYTHON_VERSION} -m venv env && \
    . env/bin/activate && \
    pip install --no-cache-dir -U pip && \
    pip install --no-cache-dir toml-to-requirements && \
    toml-to-req --toml-file pyproject.toml --optional-lists dev,doc,tests && \
    pip uninstall -y toml-to-requirements && \
    pip install --no-cache-dir -r requirements.txt

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"
SHELL ["/bin/bash", "-c"]

WORKDIR /workdir/external
