# Ubuntu 26.04 LTS ships gfortran 15.2 as the default gfortran package,
# which supports the do concurrent locality specifiers (including the
# Fortran 2023 reduce clause) this project requires.
FROM ubuntu:26.04

ENV DEBIAN_FRONTEND=noninteractive

# Version of the repository to build: leave unset (default) to build from
# this local checkout, including any uncommitted changes; set to a tag or
# commit hash (--build-arg GIT_REF=v0.9.0) to instead clone and check out
# that exact revision from GitHub, ignoring the local checkout.
ARG GIT_REF=""

# python3-numpy/python3-scipy provide the packages needed by the python
# test scripts; python-is-python3 lets them be invoked as `python`
RUN apt-get update && apt-get install -y \
    git \
    curl \
    ca-certificates \
    vim \
    gfortran \
    python3 \
    python-is-python3 \
    python3-numpy \
    python3-scipy \
    python3-pip \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# matplotlib/equibin/lintsampler are needed by the chi-square validation
# scripts under test/python/ (test_chisquare_*.py, chisquare_harness.py,
# sample_from_pdf.py); not available via apt, unlike numpy/scipy above.
# Matches the same three packages environment.yaml installs for the
# ifx/flang containers.  h5py is for the precomputed normalization
# tables (python/cli/normgrid.py), pooch for fetching the published ones
# (python/cli/normtables.py).
RUN pip install --break-system-packages --no-cache-dir \
    matplotlib equibin lintsampler h5py pooch

# Set up working directory
WORKDIR /app

# Install fpm from source (bootstraps itself with gfortran)
# There is an fpm package on conda but building from source gives a
# newer version
RUN git clone https://github.com/fortran-lang/fpm \
    && cd fpm \
    && ./install.sh

ENV PATH="/root/.local/bin:${PATH}"

# Stage the source: defaults to this local checkout; if GIT_REF is set,
# the RUN step below discards this and clones that revision instead.
COPY src /tmp/band_src/src/
COPY test /tmp/band_src/test
COPY fpm.toml /tmp/band_src/
COPY python /tmp/band_src/python/
COPY scripts /tmp/band_src/scripts/

RUN if [ -n "$GIT_REF" ]; then \
        echo "Building band_distribution @ $GIT_REF (overriding local checkout)" && \
        rm -rf /tmp/band_src && \
        git clone https://github.com/det-lab/band_distribution.git /tmp/band_src && \
        cd /tmp/band_src && git checkout "$GIT_REF"; \
    else \
        echo "Building band_distribution from the local checkout"; \
    fi

RUN mkdir -p /app/src /app/test /app/python /app/scripts \
    && cp -r /tmp/band_src/src/. /app/src/ \
    && cp -r /tmp/band_src/test/. /app/test/ \
    && cp -r /tmp/band_src/python/. /app/python/ \
    && cp -r /tmp/band_src/scripts/. /app/scripts/ \
    && cp /tmp/band_src/fpm.toml /app/ \
    && rm -rf /tmp/band_src

# version.f90.inc (PpqFort_version(); not committed, see the script) must
# exist before fpm build/test -- it is a plain `include` in src/PpqFort_m.f90
RUN python scripts/generate_version_include.py

# Now compile the fortran code
RUN fpm test --compiler gfortran --profile release --flag "-march=native -fopenmp -ftree-parallelize-loops=4 -fcoarray=single -fPIC"
RUN fpm install --prefix=. --flag "-march=native -fopenmp -ftree-parallelize-loops=4 -fcoarray=single -fPIC"

# Needed to find the appropriate .so files from within python
ENV LD_LIBRARY_PATH=/app/lib

# Published normalization tables (python/cli/table_registry.json), fetched and
# hash-checked at build time -- HPC compute nodes often have no internet.
# python/cli/normtables.py (and so PpqPDF(ppqn_table=NAME)) finds them here.
ENV BAND_TABLES_DIR=/app/tables
RUN mkdir -p /app/tables && python /app/python/cli/normtables.py fetch --all
