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

ARG DIRACX_VERSION="v0.1.1"
ARG CTADIRAC_VERSION="3.2.0"
ARG RUCIO_VERSION="41.2.0"
ARG RUCIO_POLICY_VERSION="0.5.0"
ARG USERID=1000
ARG GROUPID=1000

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

RUN dnf install -y procps-ng psmisc openssh-clients iptables-services unzip bzip2 git \
  && dnf clean all -y \
  && \
  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 /etc/grid-security/certificates && chmod -R o+r /etc/grid-security/ \
  && mkdir -p ${DIRAC_DIR} \
  && chown -R dirac:dirac ${DIRAC_DIR}

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

# run everything here as dirac user to get correct permissions
USER dirac

# install dirac-grid and CTADIRAC
WORKDIR /opt/dirac

ENV PATH="${DIRACOS}/bin:${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}" \
    -c conda-forge -c diracgrid \
    python=3.12 \
    dirac-grid 'diracx-api<0.2.0' 'diracx-core<0.2.0' 'diracx-db<0.2.0' \
    'mysqlclient >=2.0.3,<2.1' 'mysql-client' 'python-gfal2>1.13.0' \
    'm2crypto>0.42' 'tornado_m2crypto>0.1.3' \
    "rucio-clients=${RUCIO_VERSION}" \
  && ${DIRACOS}/bin/pip install --no-cache-dir CTADIRAC[server]==${CTADIRAC_VERSION} \
  && ${DIRACOS}/bin/pip install --no-cache-dir ctao-bdms-rucio-policy==${RUCIO_POLICY_VERSION} \
  && mkdir -p /opt/dirac/etc/grid-security/certificates \
  && mkdir -p /home/dirac/.ssh /home/dirac/.globus

COPY --chown=dirac:dirac --chmod=755 ./configure.py ./delete_section.py /home/dirac/
COPY --chown=dirac:dirac --chmod=755 bashrc /opt/dirac/

ENV DIRAC="${DIRACOS}" \
  DIRAC_ROOT_PATH="${DIRAC_DIR}" \
  CONDA_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" \
  MAMBA_ROOT_PREFIX="${DIRACOS}" \
  X509_CERT_DIR="/etc/grid-security/certificates" \
  X509_VOMSES="/etc/grid-security/vomses" \
  X509_VOMS_DIR="/etc/grid-security/vomsdir" \
  XML_CATALOG_FILES="file://${DIRACOS}/etc/xml/catalog file:///etc/xml/catalog"

ENTRYPOINT ["/Entrypoint.sh"]
