#!/usr/bin/env bash
# By MB
# Apptainer run wrapper script
# binds directories and runs a container

if [ $# -lt 1 ]; then
	echo "Run script for VSC containers @IMGW"
	echo "$0 [filename] [arguments]"
	exit 1
fi

# Check if singularity or apptainer is available
if [ -x "$(command -v apptainer)" ]; then
	echo "$0: using apptainer"
	singularity=apptainer
elif [ -x "$(command -v singularity)" ]; then
	echo "$0: using singularity"
	singularity=singularity
fi	
# Check if a module system is available
if [ -z "$singularity" ]; then	
	if [ -x "$(command -v module)" ]; then
		echo "$0: using module system"
		module load apptainer >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			module load singularity >/dev/null 2>&1
			if [ $? -ne 0 ]; then
				echo "$0: failed to load apptainer or singularity module"
				exit 1
			else
				echo "$0: loaded singularity module"
				singularity=singularity
			fi
		else
			echo "$0: loaded apptainer module"
			singularity=apptainer
		fi
	else
		echo "$0: no module system found, using singularity directly"
	fi
fi
# SINGULARITY_BIND=/opt/sw,/opt/vsc4,/opt/slurm
#
# Add a list of directories to bind to the container
# these are non default directories that are not automatically bound by singularity
#
SINGULARITY_BIND=""
if [ -d "/opt/sw" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,/opt/sw"
fi
if [ -d "/gpfs" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,/gpfs"
fi
if [ -d "/eodc" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,/eodc"
fi
if [ -d "$DATA" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,$DATA"
fi
if [ -d "$HOME" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,$HOME"
fi
if [ -d "$BINFL" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,$BINFL"
fi
if [ -d "$BINFS" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,$BINFS"
fi
if [ -d "$GLOBAL" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,$GLOBAL"
fi
if [ -d "/srvfs" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,/srvfs"
fi
if [ -d "/jetfs" ]; then
	SINGULARITY_BIND="$SINGULARITY_BIND,/jetfs"
fi

# Remove the first character from SINGULARITY_BIND if it is a comma
if [[ ${SINGULARITY_BIND:0:1} == "," ]]; then
	SINGULARITY_BIND=${SINGULARITY_BIND:1}
fi
# export to the environment
export SINGULARITY_BIND
export APPTAINER_BIND="$SINGULARITY_BIND"
echo "$0: SINGULARITY_BIND= $SINGULARITY_BIND"

# on VSC5, there is a global container directory
# /gpfs/data/fs71386/imgw/containers/

CONTAINER_HOME=/gpfs/data/fs71386/imgw/containers/
CONTAINER_PATH=$1
shift 
if [ -f $CONTAINER_HOME/$CONTAINER_PATH ]; then
	# run the container from the global directory
	echo "$0: $CONTAINER_HOME/$CONTAINER_PATH"
	$singularity run $CONTAINER_HOME/$CONTAINER_PATH "$@"
elif [ -f $CONTAINER_PATH ]; then
	# run the container from the current directory
	echo "$0: $CONTAINER_PATH"
	$singularity run $CONTAINER_PATH "$@"
elif [ -d $CONTAINER_HOME ]; then
	# complain about the container not found in global directory
	echo "$0: $CONTAINER_PATH not found ?"
	echo "$0: $CONTAINER_HOME"
	ls -lhtr $CONTAINER_HOME
else
	# complain about the container not found
	echo "$0: $CONTAINER_PATH not found ?"
	echo "Please check the container path and try again."
	exit 1
fi
