#! /bin/bash
# ***********************************************************************
# IRA Istituto di Radioastronomia
# 
#
# This code is under GNU General Public Licence (GPL).
#
# Who                                           when            What
# Andrea Orlati(andrea.orlati@inaf.it)          28/06/2018      Creation
# Giuseppe Carboni(giuseppe.carboni@inaf.it)    25/02/2020      Update
#************************************************************************

LONGOPTS=start,view,help
SHORTOPTS=svh
CL_START=
CL_VIEW=
CL_HELP=
GEOMETRY=1920x1080x0x0

function printUsage {
   echo "Starts the DISCOS constrol system"
   echo ""
        echo "Usage: `basename $0` [OPTIONS]  "
        echo "Options: "
        echo "   -s | --start   load the Command Center for starting the system"
        echo "   -v | --view    load the Command Center for monitoring purposes"
        echo "   -h | --help    prints this help"
}

# This simply checks for the correctness of the command line
export POSIXLY_CORRECT=1

getopt -n `basename $0` -Q -u -a -l $LONGOPTS $SHORTOPTS "$@" || {
   printUsage
   exit
}

# Now let's parse the command line
set -- `getopt -u -a -l $LONGOPTS $SHORTOPTS "$@"`

for i
do
	case "$i" in
		-h) CL_HELP=true ;;
		--help) CL_HELP=true ;;
		--view) CL_VIEW=true ;;
		-v) CL_VIEW=true ;;
		--start) CL_START=true ;;
		-s) CL_START=true ;;
		--)  break;;
	esac
	shift
done

# restore
export POSIXLY_CORRECT=
unset POSIXLY_CORRECT

export MANAGER=

if [ -z "$MANAGER_REFERENCE" ] ; then
	export MANAGER="corbaloc::"$HOST":3000/Manager"
else
	export MANAGER=$MANAGER_REFERENCE
fi

if [ "$CL_HELP" ] ; then
   printUsage
   exit
fi

# Load current DISCOS environment
# ===============================
if [ -f /discos-sw/config/misc/load_branch ]; then
    source /discos-sw/config/misc/load_branch
fi

STARTUP_SCRIPT="$INTROOT/app-defaults/discosStartup.xml"

if [ "$CDB" == "test" ] ; then
    SIMULATION_SCRIPT="$INTROOT/app-defaults/simulationStartup.xml"
    if [ -f "$SIMULATION_SCRIPT" ] ; then
        STARTUP_SCRIPT="$SIMULATION_SCRIPT"
    fi
fi

if [ "$CL_START" ] ; then
   acscommandcenter -g $GEOMETRY -r $STARTUP_SCRIPT &
   exit
fi

if [ "$CL_VIEW" ] ; then
   acscommandcenter -g $GEOMETRY -r $STARTUP_SCRIPT $MANAGER &
fi



#
# ___oOo___
