Bootstrap: docker
From: debian:bookworm-slim

%labels
    Description RStudio Server without bundled R - allows using external R installations
    RStudio_Version 2026.01.0-392

%post
    # Install system libraries for R package compilation
    apt-get update && apt-get install -y --no-install-recommends \
      locales \
      libcurl4-openssl-dev \
      libicu-dev \
      libpng-dev \
      libglpk-dev \
      libfontconfig1-dev \
      libfreetype6-dev \
      libx11-dev \
      libxml2-dev \
      gdal-bin \
      libgdal-dev \
      libudunits2-dev \
      cmake \
      libcairo2-dev \
      libgit2-dev \
      libhdf5-dev \
      libmagick++-dev \
      libharfbuzz-dev \
      libfribidi-dev \
      libwebp-dev \
      wget \
      gdebi-core \
      libssl-dev \
      libpq5 \
      libclang-dev \
      libc6 \
      libpam0g \
      psmisc \
      lsof \
      sudo \
      libsqlite3-0 \
      lsb-release \
      ca-certificates

    # Generate locale
    sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen
    locale-gen en_US.UTF-8

    # Download and install RStudio Server
    # Change RSTUDIO_VERSION to match your R's OpenSSL version:
    #   - 2026.01.0-392: OpenSSL 3.x
    #   - 2023.12.1-402: OpenSSL 1.1.x
    # Check https://download2.rstudio.org/server/jammy/amd64/ for available versions
    RSTUDIO_VERSION="2026.01.0-392"
    wget -q https://download2.rstudio.org/server/jammy/amd64/rstudio-server-${RSTUDIO_VERSION}-amd64.deb
    
    # Install without R dependency (--force-depends skips R requirement)
    dpkg --force-depends -i rstudio-server-${RSTUDIO_VERSION}-amd64.deb || true
    apt-get install -f -y
    rm rstudio-server-${RSTUDIO_VERSION}-amd64.deb

    # Create necessary directories for RStudio Server
    mkdir -p /var/run/rstudio-server /var/lib/rstudio-server /var/log/rstudio-server

    # Cleanup to reduce image size
    apt-get clean
    rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/* /var/cache/debconf/*
    rm -rf /usr/share/doc/* /usr/share/man/* /usr/share/info/* /usr/share/lintian/*
    find /usr/share/locale -mindepth 1 -maxdepth 1 ! -name 'en_US' -exec rm -rf {} +
    rm -rf /usr/lib/rstudio-server/resources/{presentation,tutorial,templates}
    find / -type f \( -name "*.pyc" -o -name "*.bak" -o -name "*~" \) -delete 2>/dev/null || true
    rm -rf /var/log/*

%environment
    export PATH=/usr/lib/rstudio-server/bin:$PATH
    export LANG=en_US.UTF-8
    export LC_ALL=en_US.UTF-8

%runscript
    echo "RStudio Server (no-R) container"
    exec "$@"

%help
    RStudio Server container WITHOUT bundled R.
    Based on debian:bookworm-slim with system libraries for R package compilation.
    
    Usage:
        sbatch rstudio_cumc_cloud.sh -i <hpc_ip> -s /path/to/rstudio-no-r.sif
    
    To change RStudio version, edit RSTUDIO_VERSION and rebuild:
        singularity build --fakeroot rstudio-no-r.sif rstudio.def
