# ------------------------------------------------------------
# Containerfile with build-time UID/GID variables
# Creates group allmambausers (GID=$TARGET_GID)
# Creates user  mambauser     (UID=$TARGET_UID)
# ------------------------------------------------------------
# -- tailourd for alpine --
# alpine uses busybox, some commands flag patterns are different
#   eg: tar x instead of tar -x on Deb/RHEL distros
#   eg: -r flag for gunzip not supported, using find and piping the file names to mimic recurse
# ------------------------------------------------------------

# ------------------ Notes ------------------------------
## gpaw data needs installing into the setup folder. See tutor instructions.
## The following two lines should install data into the set directory and register it with gpaw,
## However, the --gpaw data is included in the installation, the --basis data install caused an unknown error
# && micromamba run -n base gpaw install-data --gpaw --version=24.11.0 --register /opt/conda/lib/python3.11/site-packages/gpaw_data/setups \
# && micromamba run -n base gpaw install-data --basis --version=24.11.0 --register /opt/conda/lib/python3.11/site-packages/gpaw_data/setups \

## Basis data were first installed into a test conda env and compressed into a tar file.
## This file (gpaw-basis-NAO-sz+coopt-NGTO-0.9.11271.tar.xz) is copied into the contianer and extracted into the setup folder.
## The default --gpaw data is in .gz format and we run the tar command to decomrpess them in place using gunzip

# --------------------------------------------------------

FROM ghcr.io/mamba-org/micromamba:alpine

# Build-time variables (override with --build-arg)
ARG TARGET_UID=30001
ARG TARGET_GID=20001
ARG TARGET_USER=mambauser
ARG TARGET_GROUP=allmambausers

USER root

# Copying conda env file
COPY env.yaml /tmp/env.yaml

# COPY gpaw-setups-24.11.0.tar.xz /tmp/gpaw-setups-24.11.0.tar.xz # these files were already included in gpaw installation
COPY gpaw-basis-NAO-sz+coopt-NGTO-0.9.11271.tar.xz /tmp/gpaw-basis-NAO-sz+coopt-NGTO-0.9.11271.tar.xz

RUN set -eux \
    && micromamba install -y -n base -f /tmp/env.yaml \
    && micromamba clean --all --yes \
    && tar x -f /tmp/gpaw-basis-NAO-sz+coopt-NGTO-0.9.11271.tar.xz -C /opt/conda/lib/python3.11/site-packages/gpaw_data/setups --strip-components=1 \
    && find /opt/conda/lib/python3.11/site-packages/gpaw_data/setups -name "*.gz" -exec gunzip {} \; \
    && micromamba run -n base pip install --no-cache-dir mace-torch \
    && rm -rf /opt/conda/pkgs /tmp/* \
    && deluser mambauser 2>/dev/null || true \
    && delgroup mambauser 2>/dev/null || true \
    && addgroup -g "${TARGET_GID}" "${TARGET_GROUP}" \
    && adduser -D -u "${TARGET_UID}" -G "${TARGET_GROUP}" "${TARGET_USER}" \
    && mkdir -p /workdir /course_data_readonly \
    && chown -R "${TARGET_UID}:${TARGET_GID}" /workdir /course_data_readonly /home/"${TARGET_USER}"  /opt/conda/lib/python3.11/site-packages/gpaw_data/setups

USER ${TARGET_USER}
WORKDIR /workdir
