#!/bin/bash

#$ -N rstudiolauncher
#$ -l h_rt=8:0:0
#$ -cwd

source /etc/profile.d/ge_interactive-environment.sh
echo "Your RStudio instance awaits you at http://$(hostname -f):${SGE_INTERACT_PORT}" | \
    mail -s "RStudio instance started for Job ${JOB_ID} (${JOB_NAME})" ${USER}

# Launch a shell that is isolated from the rest of the system through
# Linux namespaces (= container).  Share the network of the host
# system.

# Note that only the current working directory will be available.  If
# you want other directories to be available, add
# "--share=/the/directory" (for read/write access) or
# "--expose=/the/dir" (for read-only access).

/gnu/remote/bin/guix shell                              \
    --container --network                               \
    --no-cwd                                            \
    --share="/gnu/store"                                \
    --expose="/etc/profile.d"                           \
    --expose="/fast"                                    \
    --preserve=LANG                                     \
    --manifest=/gnu/remote/manifests/rstudio-server.scm \
    $@ \
    -- \
    /bin/sh -c                                                    \
      'export SSL_CERT_PATH="$GUIX_ENVIRONMENT/etc/ssl/certs";    \
       export SSL_CERT_FILE="$SSL_CERT_PATH/ca-certificates.crt"; \
       export GUIX_LOCPATH="$GUIX_ENVIRONMENT/lib/locale";        \
       source /etc/profile.d/ge_interactive-environment.sh;       \
       env > ~/.Renviron;                                         \
      '"rserver --database-config-file=<(echo -e 'provider=sqlite\ndirectory=$HOME/rstudio-server-db') \
                --auth-none=1                   \
                --www-address=${HOSTNAME}       \
                --www-port=${SGE_INTERACT_PORT} \
                --server-user=${USER}           \
                --server-data-dir=${TMPDIR}/${USER}-${SGE_INTERACT_PORT}-rserver-data \
                --secure-cookie-key-file=${TMPDIR}/${USER}-${SGE_INTERACT_PORT}-secure-cookie-key"
