ARG BASE_REGISTRY=ghcr.io
ARG BASE_IMAGE=llnl/apbuilder/ironbank/redhat/ubi8
ARG BASE_TAG=8.10
ARG PYTHON_VERSION=3.12

FROM ${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG} AS builder
# re-declare and auto inherit the value set earlier for PYTHON_VERSION
ARG PYTHON_VERSION
WORKDIR /build

COPY pyproject.toml .
COPY src ./src
COPY .git ./.git
COPY dist ./dist

RUN if [ -z "$(ls dist/*.whl 2>/dev/null)" ]; then \
    yum -y install git python${PYTHON_VERSION} python${PYTHON_VERSION}-pip && \
    python3 -m pip install --upgrade pip setuptools && \
    python3 -m pip install build wheel setuptools-scm && \
    python3 -m build --no-isolation . -o dist/; \
    fi

FROM ${BASE_REGISTRY}/${BASE_IMAGE}:${BASE_TAG}
ARG USER=apbuilder
# re-declare and auto inherit the value set earlier for PYTHON_VERSION
ARG PYTHON_VERSION

# Create user to avoid using root
RUN groupadd -g 1002 ${USER} && \
    useradd -r -m -u 1002 -g ${USER} ${USER}

# Install miniconda
RUN yum -y install wget && \
    mkdir -p ~/miniconda3 && \
    wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh && \
    bash ~/miniconda3/miniconda.sh -b -u -p /opt/conda && \
    rm ~/miniconda3/miniconda.sh && \
    source /opt/conda/bin/activate && \
    conda init --all && \
    yum -y remove wget && \
    conda --version

USER ${USER}
ENV PATH=/opt/conda/bin:$PATH
ENV VENV_NAME=apbuilder-venv

# Create conda environment with dependencies
RUN echo ${VENV_NAME}
RUN conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main && \
    conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r && \
    conda create -y -n ${VENV_NAME} python=${PYTHON_VERSION} \ 
    'pygmt=0.12' \
    'libgdal-grib' \
    'libgdal-netcdf' \
    -c conda-forge && \
    conda clean -yaf && \
    conda init --all && \
    source /opt/conda/bin/activate && \
    conda activate ${VENV_NAME}

# Make RUN commands using the new environment:
# Unfortunately, SHELL command does not support use of ARG variables, 
# hence the name of the virtual environment is hardcoded here. 
# It must be the same value as argumenta VENV_NAME defined at the top of this file.
SHELL ["/opt/conda/bin/conda", "run", "-n", "apbuilder-venv", "/bin/bash", "-c"]

COPY --from=builder --chown=${USER}:${USER} /build/dist/ /home/${USER}/dist

RUN python3 -m pip install /home/${USER}/dist/*.whl && \
    rm -rf /home/${USER}/dist

WORKDIR /home/${USER}
COPY --chown=${USER} src/apbuilder/config/config.toml .config/herbie/config.toml
RUN mkdir -p logs && touch logs/.ignore
RUN touch cacert.pem
ENV REQUESTS_CA_BUNDLE=/home/${USER}/cacert.pem

# Unfortunately, ENTRYPOINT command does not support use of ARG or ENV variables directly,
# hence the name of the virtual environment is hardcoded here. 
# It must be the same value as argumenta VENV_NAME defined at the top of this file.
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "apbuilder-venv"]
CMD ["apbuilder", "-h"]

ARG VERSION
ARG BUILD_DATE
ARG COMMIT_HASH
ARG NAME="APBuilder"
ARG DESCRIPTION="Atmospheric Profile Builder (APBuilder)"
ARG LICENSE="BSD-Commercial"
ARG VENDOR="LLNL"
ARG AUTHOR="Raul Viera-Mercado (vieramercado1@llnl.gov)"
LABEL name=$NAME
LABEL description=$DESCRIPTION
LABEL distribution-scope=$LICENSE
LABEL maintainer=$AUTHOR
LABEL build-date=$BUILD_DATE
LABEL release=""
LABEL summary=$DESCRIPTION
LABEL url=""
LABEL vcs-ref=""
LABEL vcs-type=""
LABEL vendor=$VENDOR
LABEL version=$VERSION
LABEL git_commit=$COMMIT_HASH
LABEL org.opencontainers.image.created=$BUILD_DATE
LABEL org.opencontainers.image.name=$NAME
LABEL org.opencontainers.image.title="Atmospheric Profile Builder"
LABEL org.opencontainers.image.description=$DESCRIPTION
LABEL org.opencontainers.image.vendor=$VENDOR
LABEL org.opencontainers.image.authors=$AUTHOR
LABEL org.opencontainers.image.licenses=$LICENSE
LABEL org.opencontainers.image.version=$VERSION
LABEL org.opencontainers.image.revision=""
LABEL org.opencontainers.image.base.name="ironbank/redhat/ubi/ubi8:8.10"
LABEL gov.llnl.image.ironbank="True"
LABEL gov.llnl.tags="llnl, gmp, apbuilder, geophysics, python"
