# syntax=docker/dockerfile:1

FROM debian:bookworm-slim AS np-tmcode-dev
WORKDIR /root

#
#RUN rm -rf /var/lib/apt/lists/* 
#RUN ls -l /etc/apt/sources.list.d/oneAPI.list && sleep 5s
#RUN cat /etc/apt/sources.list.d/oneAPI.list && sleep 5s
#RUN ls -l /usr/share/keyrings/oneapi-archive-keyring.gpg && sleep 5s
# make sure the debian bullseye us up to date, install needed packages
RUN apt update
RUN DEBIAN_FRONTEND=noninteractive apt -y upgrade
# install packages needed to build binaries
RUN DEBIAN_FRONTEND=noninteractive apt -y install g++ gfortran gcc-offload-nvptx g++-11 gfortran-11 gcc-11-offload-nvptx g++-12 gfortran-12 gcc-12-offload-nvptx clang libc++-dev libc++abi-dev clang-13 clang-14 clang-15 clang-16 flang-16 libhdf5-dev make
# install packages needed to test with Intel OneAPI compilers
COPY --chown=root:root containers/docker/dockerstuff/intelcomps/oneapi-archive-keyring.gpg /usr/share/keyrings/
COPY --chown=root:root containers/docker/dockerstuff/intelcomps/oneAPI.list /etc/apt/sources.list.d/
RUN apt update
RUN DEBIAN_FRONTEND=noninteractive apt -y install intel-oneapi-compiler-fortran intel-oneapi-compiler-dpcpp-cpp-and-cpp-classic intel-oneapi-compiler-dpcpp-cpp
# install lapacke and its dependencies, both standard and the version with 64 bit integers
RUN DEBIAN_FRONTEND=noninteractive apt -y install liblapacke-dev liblapacke64-dev libopenblas-dev libopenblas-openmp-dev libopenblas64-dev libopenblas64-openmp-dev
# install MPI stack
RUN DEBIAN_FRONTEND=noninteractive apt -y install mpi-default-dev mpi-default-bin
# install packages needed to run python scripts for checks
RUN DEBIAN_FRONTEND=noninteractive apt -y install python3 python-is-python3 python3-regex
# install packages needed to run doxygen to create html docs
RUN DEBIAN_FRONTEND=noninteractive apt -y install doxygen
# install packages needed to run pdflatex to create pdf docs
RUN DEBIAN_FRONTEND=noninteractive apt -y install texlive-latex-base texlive-latex-recommended texlive-latex-extra texlive-font-utils
# clean up apt lists
RUN rm -rf /var/lib/apt/lists/*


# create the container on which the gitlab pipeline runs, making sure to squash
# all layers to save space
FROM scratch AS np-tmcode
COPY --from=np-tmcode-dev / /


# create the container actually containing the code
FROM np-tmcode-dev AS np-tmcode-run-dev
WORKDIR /root
#  copy the parent dir to a temporary dir in the container, so we can compile everything
ADD src /root/np-tmcode/src
ADD doc /root/np-tmcode/doc
ADD build /root/np-tmcode/build
ADD test_data /root/np-tmcode/test_data
RUN cd np-tmcode/src && make wipe && make -j && cd ../doc/src && doxygen config.dox && cd ../build/latex && make -j


# create the container on which the np-tmcode is installed, restarting from
# a minimal distro and adding strictly only the needed runtime
FROM  debian:bookworm-slim AS np-tmcode-run-minimal
WORKDIR /root
# install the strictly needed runtime libraries needed to run the executables
# and the python check scripts
RUN DEBIAN_FRONTEND=noninteractive apt update && DEBIAN_FRONTEND=noninteractive apt upgrade && DEBIAN_FRONTEND=noninteractive apt -y install libgfortran5 libgcc-s1 libhdf5-103-1 libstdc++6 libssl3 libcurl4 libsz2 zlib1g libnghttp2-14 libidn2-0 librtmp1 libssh2-1 libpsl5 libgssapi-krb5-2 libldap-2.5-0 libzstd1 libbrotli1 libaec0 libunistring2 libgmp10 libkrb5-3 libk5crypto3 libcom-err2 libkrb5support0 libsasl2-2 libp11-kit0 libtasn1-6 libkeyutils1 libffi8 liblapacke64 libopenblas64-0-openmp python3 python-is-python3 python3-regex hdf5-tools mpi-default-bin && rm -rf /var/lib/apt/lists/*
COPY --from=np-tmcode-run-dev /root /root
# remove everything which is not needed to run the codes
RUN cd /root/np-tmcode && find build -name "*.o" -exec rm -v \{\} \; && find build -name "*.gcno" -exec rm -v \{\} \; && cd src && rm -rvf cluster libnptm trapping include sphere Makefile make.inc README.md && cd .. && rm -rvf containers && cd doc && rm -rvf src && cd build/latex && rm -rvf *.tex *.out *.sty *.ind *.log *.toc *.ilg *.idx *.aux *.eps Makefile class*.pdf
# move the installed software to /usr/local
RUN mv /root/np-tmcode /usr/local


# create the container on which the np-tmcode is installed,
# squashing all layers from previous stage
FROM scratch  AS np-tmcode-run
WORKDIR /root

COPY --from=np-tmcode-run-minimal / /