# Solidipes curation image, independent of the Renku stack.
#
# Based on the official Python image, with two build targets:
#   slim : minimal image able to boot the curation service (used by tests)
#   full : complete image used for DCSM deployments (default target)
#
# Build:            podman build -t solidipes docker/
# Build slim only:  podman build --target slim -t solidipes-slim docker/
#
# The container is launched by dcsm (server-deployment) with:
#   /entrypoint.sh jupyter server --ServerApp.ip=0.0.0.0 --ServerApp.port=8888 \
#       --IdentityProvider.token=<token> --ServerApp.default_url=/solidipes \
#       --ServerApp.base_url=/<study> --ServerApp.root_dir=/home/jovyan/work/<study>
# and mounts the study directory at /home/jovyan/work/<study>.

ARG BASE_IMAGE=python:3.12-slim-bookworm

FROM ${BASE_IMAGE} AS slim
ARG NB_USER=jovyan
ARG NB_UID=1000
ARG NB_GID=100
# Version of solidipes to install (branch, tag or commit hash)
ARG SOLIDIPES_VERSION=main

ENV DEBIAN_FRONTEND=noninteractive \
    NB_USER=${NB_USER} \
    NB_UID=${NB_UID} \
    NB_GID=${NB_GID} \
    HOME=/home/${NB_USER} \
    SHELL=/bin/bash \
    PYTHONUNBUFFERED=1

USER root

# Minimal system dependencies required to boot the service
RUN apt-get update && apt-get install -y --no-install-recommends \
        ca-certificates \
        curl \
        file \
        git \
        libarchive-dev \
        libgl1 \
        libglib2.0-0 \
        libsm6 \
        libxext6 \
        libxrender1 \
        openssh-server \
        sudo \
        tini \
        wget \
        xvfb \
        && apt-get clean && rm -rf /var/lib/apt/lists/*

# libarchive python package expects liblibarchive
RUN ln -s /usr/lib/x86_64-linux-gnu/libarchive.a /usr/lib/x86_64-linux-gnu/liblibarchive.a

# Non-privileged user matching the Jupyter stack conventions.
# DCSM's spawner chowns files to jovyan:users, so the `users` group (GID 100)
# must exist.
RUN getent group ${NB_GID} >/dev/null || groupadd -g ${NB_GID} users
RUN useradd -l -m -s /bin/bash -u ${NB_UID} -g ${NB_GID} ${NB_USER}

# ~/.ssh is populated at runtime by dcsm's launch-study (docker cp of
# authorized_keys). It must pre-exist in the image: docker cp does not
# create intermediate directories.
RUN mkdir -p ${HOME}/.ssh && chown ${NB_UID}:${NB_GID} ${HOME}/.ssh && chmod 700 ${HOME}/.ssh

# Allow the user to install packages and start services at runtime
COPY sudoers.apt /etc/sudoers.d/apt
RUN chmod go-rwx /etc/sudoers.d/apt

# sshd (started by the entrypoint)
RUN mkdir -p /run/sshd && ssh-keygen -A

# Install the jupyter stack and solidipes
RUN pip install --no-cache-dir jupyterlab jupyter-server-proxy libarchive
RUN pip install --no-cache-dir "solidipes @ git+https://gitlab.com/solidipes/solidipes.git@${SOLIDIPES_VERSION}"

# Configuration files
COPY env-vars.sh /etc/profile.d/env-vars.sh
RUN chmod +x /etc/profile.d/env-vars.sh && \
    echo "source /etc/profile.d/env-vars.sh" >> /etc/bash.bashrc
COPY --chown=${NB_UID}:${NB_GID} .streamlit ${HOME}/.streamlit/
# jupyter server (ServerApp) only reads jupyter_server_config.py — it never
# loads jupyter_notebook_config.py, which is why the server-proxy routes
# (solidipes, filebrowser, webdav) silently disappeared with the jupyter stack
# upgrade. ~/.jupyter is the default config dir, so this is picked up by both
# 'jupyter server' and 'jupyter lab'.
COPY --chown=${NB_UID}:${NB_GID} jupyter_server_config.py ${HOME}/.jupyter/

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

USER ${NB_USER}
WORKDIR ${HOME}

ENTRYPOINT ["tini", "--", "/entrypoint.sh"]
CMD ["jupyter", "server", "--ip", "0.0.0.0"]

LABEL org.opencontainers.image.source="https://gitlab.com/solidipes/solidipes/-/blob/main/docker/Dockerfile"

FROM slim AS full
USER root

# Complete system dependencies for DCSM deployments
# (libgl1-mesa-glx was removed in Debian bookworm, libgl1 replaces it;
#  the GL dev headers and libpq-dev/python3-dev support packages users
#  commonly need for their runtime requirements.txt)
RUN apt-get update && apt-get install -y --no-install-recommends \
        apt-utils \
        emacs-nox \
        ffmpeg \
        fuse \
        gfortran \
        ghostscript \
        gnuplot \
        htop \
        libgl-dev \
        libglu1-mesa-dev \
        libglx-dev \
        libpq-dev \
        lsof \
        poppler-utils \
        python3-dev \
        python3-webdav \
        rubber \
        s3fs \
        texlive-latex-extra \
        unzip \
        zip \
        && apt-get clean && rm -rf /var/lib/apt/lists/*

# install juicefs
COPY juicefs /usr/bin/

RUN cd /usr/bin ; wget -q https://github.com/filebrowser/filebrowser/releases/download/v2.27.0/linux-amd64-filebrowser.tar.gz ; tar xzf linux-amd64-filebrowser.tar.gz filebrowser ; rm linux-amd64-filebrowser.tar.gz

COPY filebrowser.db /tmp/

USER ${NB_USER}
