ARG WMS_BASE_IMAGE="harbor.cta-observatory.org/dpps/wms-base:dev"
FROM ${WMS_BASE_IMAGE}

ARG WEB_APP_DIRAC_VERSION="6.0.1"
ARG USERID=1000
ARG GROUPID=1000

RUN dnf install -y procps-ng psmisc openssh-clients iptables-services bzip2 git \
  https://diracproject.web.cern.ch/diracproject/rpm/runit-2.1.2-2.el9.x86_64.rpm \
  && dnf clean all -y

COPY ./runsvdir-start.service /usr/lib/systemd/system/runsvdir-start.service
RUN systemctl enable runsvdir-start

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


RUN micromamba install -y -p "${DIRACOS}" \
    -c conda-forge -c diracgrid \
    'diracx-db<0.2.0' \
    'mysqlclient<2.1' 'mysql-client' 'python-gfal2>1.13.0' 'setuptools<82' \
    'tornado_m2crypto>0.1.3' \
  && "${DIRACOS}/bin/pip" install --no-cache-dir \
    "CTADIRAC[server] ${CTADIRAC_REQUIREMENT}" \
    "WebAppDIRAC[server]==${WEB_APP_DIRAC_VERSION}" \
    diracx-cli \
  && micromamba clean -a -y

COPY ./runsvdir-start ${DIRACOS}/sbin/runsvdir-start

RUN mkdir -p /opt/dirac/etc/grid-security/certificates \
  && chown -R dirac:dirac "${DIRACOS}/etc"

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

WORKDIR /home/dirac


# switch back to root for running systemd
USER root
CMD ["/usr/sbin/init"]

ENV DIRAC_ROOT_PATH="${DIRACOS}"
