#!/bin/sh

myArray=( "$@" )
if [[ ! " ${myArray[*]}" =~ " --help" ]]; then
    #---- forbid usage of IRMA on submit node.
    #     unless help required
    if [[ $(hostname) = "maestro-submit" ]]; then 
        cat << EOT
************************************************************
*                                                          *
*    you MUST run IRMA through a slurm allocation          *
*        either srun sbatch or salloc                      *
*    exiting now                                           *
*                                                          *
************************************************************
EOT
        exit -1
    fi 
fi

export PATH=/opt/gensoft/adm/bin:$PATH

# bind mount specific FS to be abble to accces usual files
# /pasteur => homes, projets
# /local => scratch, databases
# /mount => gensoft 
# ${HOME} to be sure to have ~ 

# bind mount not always available: cluster vs other machine
# enable vglrun required env var 
for vglvar in `env | grep ^VGL_`; do export APPTAINERENV_${vglvar}; done 
if [ ! -z "${LD_PRELOAD}" ]; then export APPTAINERENV_LD_PRELOAD=${LD_PRELOAD}; fi
MOUNTS=''
if [ -d /pasteur ]; then MOUNTS="${MOUNTS} -B /pasteur"; fi
if [ -d /local/databases ]; then MOUNTS="${MOUNTS} -B /local/databases"; fi
if [ -d /local/scratch ]; then MOUNTS="${MOUNTS} -B /local/scratch"; fi
if [ -d /mount ]; then MOUNTS="${MOUNTS} -B /mount"; fi
if [ -d /run/user/${UID} ]; then MOUNTS="${MOUNTS} -B /run/user/${UID}"; fi
if [ ! -z ${TMPDIR} ]; then MOUNTS="${MOUNTS} -B ${TMPDIR}:/tmp"; fi

#---- APPTAINER_BINDPATH can be set in order to bind mount additional volumes

NV=''
PROG=$(basename $0) 

test "x$DEBUG" = xx && cat <<EOT 
"############################################################" 
apptainer run ${NV} ${ENV_OPT} \
              -B ${HOME} \
              ${MOUNTS} \
              /opt/gensoft/exe/irma/1.3.5/libexec/irma-1.3.5.simg \
              $(basename $0) ${1+"$@"} 
"############################################################\n"
EOT

apptainer run ${NV} ${ENV_OPT} \
              -B ${HOME} \
              ${MOUNTS} \
              /opt/gensoft/exe/irma/1.3.5/libexec/irma-1.3.5.simg \
              $(basename $0) ${1+"$@"}

