#! /bin/bash


# ESA (C) 2000-2025
# 
# This file is part of ESA's XMM-Newton Scientific Analysis System (SAS).
#
# SAS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SAS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SAS. If not, see <http://www.gnu.org/licenses/>.
#

# Script to run a SAS docker image.
#
# The script can execute the SAS docker image either in background 
# (option -d or -d=true) or in foreground (option -d not present or 
# -d=false present). 
# 
# When in foreground, the usual is to specify as well -i -t options.
# Option -t allocates a pseudotty and -i keeps stdin open even if not 
# attached. These options are required to attach to the console 
# the stdin, stdout and stderr. 
# 
# Containers started in detach mode exit when the root process used 
# to run the container, exits. 

# The image must be defined in environment variable SAS_DOCKER_IMAGE
# Otherwise, the scripts aborts.

if [ -z "$SAS_DOCKER_IMAGE" ] ; then 
 echo "Error: SAS docker image undefined!" ; echo
 echo "Please define it in variable SAS_DOCKER_IMAGE" ; echo
 echo "e.g.: "
 echo " export SAS_DOCKER_IMAGE=docker4sas:sas_22_1" ; echo
 exit 1
fi

# The presence/absence of any additional parameters determines whether the 
# image is requested to run in detach mode or interactively.
# E.g.: If $*="fv", the FTOOLS fv is run in detached mode.
# If $* is "", the image is requested to run in the foreground, that is
# interactively with switch -it.

if [ -z "$*" ] ; then
 mod="-it"
fi

# MY_HOST_IP stores the IP of the host.
#
# MY_HOST_IP is used to set the DISPLAY variable.
# However, the IP of the host depends on the OS where Docker is run.
# In macOS, docker is integrated with the OS, so the host IP is
# the real IP of the machine. However, in Windows, Docker works
# integrated with the Windows Subsystem for Linux, WSL, version 2, 
# which sets a particular IP inside a specific subnetwork, different
# to the IP of the machine. As aconsequence, the method used in this
# script, it does not work in Winodws, unless we set the variable
# MT_HOST_IP to the real IP of the host running Windows. 


osname=`uname -s`

# Allow to set MY_HOST_IP before running this script

if [ -z "${MY_HOST_IP}" ] ; then 
 case "$osname" in
 Linux) 
 #MY_HOST_IP=`ifconfig eth0 | grep "inet " | awk '{print $2}'` 
 MY_HOST_IP=`ifconfig ens160 | grep "inet " | awk '{print $2}'`
 ;;
 Darwin) 
 MY_HOST_IP=`ifconfig en0 | grep "inet " | awk '{print $2}'` 
 ;;
 esac
fi

if [ -n "$MY_HOST_IP" ]; then
 echo "MY_HOST_IP = $MY_HOST_IP" ; echo
 xhost +${MY_HOST_IP}
else
 echo "Error: MY_HOST_IP can not be determined !"
 exit 1
fi

# Required environment options

eopt="-e DISPLAY=${MY_HOST_IP}:0 -e XAUTHORITY=/.Xauthority"

# There is the option to define add_eopt as additional environment options 
# which should be a string formatted as "-e env_option -e env_option ...."

if [ "$add_eopt" ]; then
 eopt="$eopt $add_eopt"
fi

# Host volumes

vols="-v /tmp/.X11-unix:/tmp/.X11-unix -v ${HOME}/.Xauthority:/.Xauthority -v ${PWD}/ccf:/ccf -v ${PWD}/mywork:/home/xmmsas/mywork"

# There is the option to define add_vols as additional volumes
# which should be formatted as "-v add_volume -v add_volume ..."

if [ "$add_vols" ] ; then
 vols="$vols $add_vols" 
fi

# Ports

ports="-p 8888:8888"

# There is the option to define add_ports as additional ports
# which should be formatted as "-p out_port:in_port -p out_port:in_port ..."

if [ "$add_ports" ]; then
 ports="$ports $add_ports"
fi

# Run it

if [ "$DEBUG" ] ; then 
 echo "Would execute the command:" ; echo
 echo "docker run $mod --rm $eopt $vols $ports $SAS_DOCKER_IMAGE $*"
fi


docker run $mod --rm $eopt $vols $ports $SAS_DOCKER_IMAGE $*