FROM nvidia/cuda:12.4.1-cudnn-runtime-ubuntu22.04 AS base
# Set to non-interactive mode (does not block on input)
ENV DEBIAN_FRONTEND=noninteractive
# Do not create .pyc
ENV PYTHONDONTWRITEBYTECODE=1
# Disable buffering
ENV PYTHONUNBUFFERED=1
# Path to the root python environment
ENV PYTHONPATH=/usr/local/lib/python3.10/dist-packages
# Location to save pretrained models in torchvision
ENV TORCH_HOME=/app/.cache

WORKDIR /app

RUN apt update && \
    apt install -y \
    wget \
    bzip2 \
    build-essential \
    git \
    git-lfs \
    curl \
    ca-certificates \
    libsndfile1-dev \
    libgl1 \
    python3.10 \
    python3-pip 

# For SageMath (cf. https://sagemanifolds.obspm.fr/install_ubuntu.html)
RUN apt install -y \
    bc \
    binutils \
    bzip2 \
    ca-certificates \
    cliquer \
    cmake \
    curl \
    ecl \
    eclib-tools \
    fflas-ffpack \
    flintqs \
    g++ \
    gengetopt \
    gfan \
    gfortran \
    git \
    glpk-utils \
    gmp-ecm \
    lcalc \
    libatomic-ops-dev \
    libboost-dev \
    libbraiding-dev \
    libbrial-dev \
    libbrial-groebner-dev \
    libbz2-dev \
    libcdd-dev \
    libcdd-tools \
    libcliquer-dev \
    libcurl4-openssl-dev \
    libec-dev \
    libecm-dev \
    libffi-dev \
    libflint-arb-dev \
    libflint-dev \
    libfreetype6-dev \
    libgc-dev \
    libgd-dev \
    libgf2x-dev \
    libgiac-dev \
    libgivaro-dev \
    libglpk-dev \
    libgmp-dev \
    libgsl-dev \
    libhomfly-dev \
    libiml-dev \
    liblfunction-dev \
    liblrcalc-dev \
    liblzma-dev \
    libm4rie-dev \
    libmpc-dev \
    libmpfi-dev \
    libmpfr-dev \
    libncurses5-dev \
    libntl-dev \
    libopenblas-dev \
    libpari-dev \
    libpcre3-dev \
    libplanarity-dev \
    libppl-dev \
    libprimesieve-dev \
    libpython3-dev \
    libqhull-dev \
    libreadline-dev \
    librw-dev \
    libsingular4-dev \
    libsqlite3-dev \
    libssl-dev \
    libsuitesparse-dev \
    libsymmetrica2-dev \
    libz-dev \
    libzmq3-dev \
    libzn-poly-dev \
    m4 \
    make \
    nauty \
    openssl \
    palp \
    pari-doc \
    pari-elldata \
    pari-galdata \
    pari-galpol \
    pari-gp2c \
    pari-seadata \
    patch \
    perl \
    pkg-config \
    planarity \
    ppl-dev \
    python3-distutils \
    python3-venv \
    r-base-dev \
    r-cran-lattice \
    singular \
    sqlite3 \
    sympow \
    tachyon \
    tar \
    tox \
    xcas \
    xz-utils \
    texlive-latex-extra \
    texlive-xetex \
    latexmk \
    pandoc \
    dvipng

# ----------------------------------------------------------------
# create user
# ----------------------------------------------------------------
# UID and GID are given from the outside
ARG USER_UID=1024
ARG USER_GID=1024

# User name and group name in the container
ARG USER_NAME=user
ARG GROUP_NAME=user

# If the group does not exist, create an appropriate group
RUN if ! getent group $USER_GID >/dev/null; then \
    groupadd -g $USER_GID $GROUP_NAME; \
    fi

# Create a user
RUN useradd -m -u $USER_UID -g $USER_GID -s /bin/bash $USER_NAME

# Change the initial user
USER $USER_NAME
ENV PATH=/home/$USER_NAME/.local/bin:$PATH

# ----------------------------------------------------------------
# requirements may be updated more than torch, so do not include them here
RUN pip3 install --no-cache-dir -U pip setuptools wheel

# ----------------------------------------------------------------
FROM base AS torch-2.3.0

RUN pip3 install --no-cache-dir torch==2.3.0+cu121 torchvision==0.18.0+cu121 -f https://download.pytorch.org/whl/torch_stable.html 

# COPY requirements.txt /app
COPY . /app
RUN pip3 install --no-cache-dir -r requirements.txt

ENV UID=1024
ENV GID=1024
COPY --chown=$UID:$GID . ./app
USER $USER_NAME

RUN apt install -y nodejs npm
RUN npm install -g @google/gemini-cli

# COPY setup-sage.sh /app
# RUN bash setup-sage.sh  # does not run properly?
