FROM harbor.cta-observatory.org/proxy_cache/almalinux:9

ARG CTADIRAC_VERSION="3.0.14"
ARG USERID=1000
ARG GROUPID=1000

LABEL maintainer="Natthan Pigoux <natthan.pigoux@lupm.in2p3.fr>, Luisa Arrabito <arrabito@in2p3.fr>"
LABEL description="CTADIRAC client docker image"
LABEL org.opencontainers.image.url=https://gitlab.cta-observatory.org/cta-computing/dpps/CTADIRAC

ENV DIRAC_DIR="/home/dirac"
ENV DIRACOS="${DIRAC_DIR}/diracos"

RUN \
  if getent group ${GROUPID}; then \
    groupmod --new-name dirac $(getent group ${GROUPID} | cut -d: -f1); \
  else \
    groupadd --gid ${GROUPID} dirac; \
  fi\
  && adduser --uid ${USERID} --gid dirac -s /bin/bash -d /home/dirac dirac \
  && mkdir -p ${DIRAC_DIR} \
  && chown -R dirac:dirac ${DIRAC_DIR} \
  && mkdir -p ${DIRAC_DIR}/.globus \
  && chown -R dirac:dirac ${DIRAC_DIR}/.globus

WORKDIR ${DIRAC_DIR}

RUN dnf install -y bzip2 git && dnf clean all -y

COPY --chown=dirac:dirac --chmod=755 Entrypoint.sh /Entrypoint.sh

# Install DIRAC client
USER dirac

ENV PATH="${DIRACOS}/bin:${DIRACOS}/condabin:${DIRAC_DIR}/.local/bin:$PATH"

RUN mkdir -p "${DIRAC_DIR}/.local/bin" \
  && curl --proto "=https" -sSfL https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj -C "${DIRAC_DIR}/.local/bin" --strip-components=1 bin/micromamba  \
  && micromamba create -y -p "${DIRACOS}" python=3.12 dirac-grid 'diracx-api<0.2.0' 'diracx-core<0.2.0' \
  && ${DIRACOS}/bin/pip install --no-cache-dir CTADIRAC==${CTADIRAC_VERSION} \
  && ${DIRACOS}/bin/pip install --no-cache-dir dirac-cwl==1.3.0 \
  && mkdir -p ${DIRACOS}/etc/grid-security/certificates

ENV CONDA_PREFIX="${DIRACOS}" \
  MAMBA_ROOT_PREFIX="${DIRACOS}" \
  CONDOR_CONFIG="${DIRACOS}/etc/condor/condor_config" \
  DAVIX_DISABLE_REDIRECT_CACHING=1 \
  DAVIX_USE_LIBCURL=1 \
  GSETTINGS_SCHEMA_DIR="${DIRACOS}/share/glib-2.0/schemas" \
  X509_CERT_DIR="${DIRACOS}/etc/grid-security/certificates" \
  X509_VOMSES="${DIRACOS}/etc/grid-security/vomses" \
  X509_VOMS_DIR="${DIRACOS}/etc/grid-security/vomsdir" \
  XML_CATALOG_FILES="file://${DIRACOS}/etc/xml/catalog file:///etc/xml/catalog"

ENTRYPOINT ["/Entrypoint.sh"]
