# syntax=docker/dockerfile:1

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

# make sure the debian bullseye us up to date, install needed packages
RUN DEBIAN_FRONTEND=noninteractive apt update && DEBIAN_FRONTEND=noninteractive apt -y upgrade
# install packages needed to build binaries
RUN DEBIAN_FRONTEND=noninteractive apt -y install g++ gfortran make gcc-offload-nvptx libhdf5-dev 
# 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 python3 python-is-python3 python3-regex hdf5-tools && 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 / /


