set -e

function usage {
echo "
   Purpose: Test one rack of LLRF chassis

   Usage: lcls2_rack.sh [-h] [-r rack] [-w datadir] [-u] [-p] [-q qf2predir]
                        [-s scriptdir] [-g pythonsetupfile] [-e pvprefix]
   Options:

   -p (optional) Flag to program FPGAs (4/19/2018 John Jones' latest)
                 before executing checkout. (No value needed)

   -b (required if -p set) Bitfile name (with path)

   -h (optional) Print help

   -r (optional) Rack ID. Options:
                A or a: rack A of LCLS-II cryomodule
                B or b: rack B of LCLS-II cryomodule
                C or c: LBNL Building 46 test rack
                D or d: SLAC B15 SSA/RFS test stand
                If not specified, rack A is assumed.

   -w (optional) Directory files are written to. For systems
                that don't have write access to all areas (linuxRT).

   -u (optional) Flag to set use_spartan to True
                 (No value needed)

   -q (optional) Directory of qf2pre scripts.
                 If not specified, SLAC B15 directory is assumed

   -s (optional) Define path to RF scripts (lcls2_rack.sh, etc.)
       If not specified, current directory is assumed
       example for LERF: /usr/local/lcls/package/lcls2_llrf/software/prc

   -g (optional) Full path and file name for python setup file
       example for LERF: /usr/local/lcls/epics/iocCommon/facility/GoPythonLinuxRT.sh

   -o (optional) EPICS PV to write status to

   Examples:

   Test B15 SRF rack in cage area from linux workstation
   (and program bitfiles):
     lcls2_rack.sh -r c -p

   Test B15 Room 121 rack A from linuxRT CPU cpu-b15-rf02
   (and program bitfiles):
     lcls2_rack.sh -r a -p -w /data/cpu-b15-rf02 -g /afs/slac/g/lcls/epics/iocCommon/cpu-b15-rf01/GoPython.sh

   Test LERF first cryomodule, rack B from user host, lclsapp2
   (and program bitfiles):
     lcls2_rack.sh -r b -p -w /data/lcls-llrfcpu01 -g /usr/local/lcls/epics/iocCommon/facility/GoPythonLinuxRT.sh ...
       -s /usr/local/lcls/package/lcls2_llrf/software/prc


   Exit code values:

         0   Rack checkout complete and successful
         1   Did not perform rack checkout
               (missing argument, etc.)
         2   Rack checkout failed

"
}
function status {
    if [ "$STATUSPV" != "" ]
    then
	if [ ! -x "$(command -v caput)" ]
	then
            echo "Cannot execute caput. Skipping caput commands for the remainder."
	    STATUSPV=""
	else
	    caput -t $STATUSPV $MSG
	fi
    else
	echo ""
	echo $MSG
	echo ""
    fi
}

# Default to use rack A and not override write file location
echo ""
echo "Entering lcls2_rack.sh"
RACK="A"
DATADIR=""
DIRFLAG=""
PROGRAM=0
SPARTANFLAG=""
PYTHONPATH=""
SCRIPTPATH=""
STATUSPV=""
STATUSFLAG=""
BITFILE=""
PYTHON=${PYTHON:-python}

# Define path to bmb7 software
BMB7_PATH="../bmb7"

while [ $# -gt 0 ];
do
    opt=$1;
    case $opt in
	"-h" )
	    usage
	    exit 1
	    ;;
	"-r" )
	    shift;
	    RACK=`echo $1 | tr '[:lower:]' '[:upper:]'`
	    echo "Rack $RACK"
	    shift
	    ;;
	"-w" )
	    shift;
	    DATADIR=$1
	    DIRFLAG=`echo -w $DATADIR`
	    echo "Will write files to $DATADIR"
	    shift;
	    ;;
	"-p" )
	    shift;
	    PROGRAM=1
	    echo "Will load new bitfiles to FPGAs"
	    ;;
	"-u" )
	    shift;
	    SPARTANFLAG=" -u"
	    echo "Will use spartan"
	    ;;
	"-g" )
	    shift;
	    PYTHONPATH=$1
	    echo "Will source $PYTHONPATH"
	    shift;
	    ;;
	"-s" )
	    shift;
	    SCRIPTPATH=$1
       	    echo "Will execute lcls2_rack.sh from $SCRIPTPATH"
	    shift;
	    ;;

	"-o" )
	    shift;
	    STATUSPV=$1
	    STATUSFLAG=`echo -o $STATUSPV`
       	    echo "Will write status to ${STATUSPV}"
	    shift;
	    ;;
	"-b" )
	    shift;
	    BITFILE=$1
       	    echo "Will load bitfile ${BITFILE}"
	    shift;
	    ;;
    esac
done

if [ "$RACK" == "A" ]; then
	RFS1=rfs1a
	RFS2=rfs2a
	PRC=prca
	RFS="$RFS1 $RFS2"
elif [ "$RACK" == "B" ]; then
	RFS1=rfs1b
	RFS2=rfs2b
	PRC=prcb
	RFS="$RFS1 $RFS2"
elif [ "$RACK" == "C" ]; then
	RFS1=192.168.165.84
	PRC=192.168.0.103
	RFS="$RFS1"
elif [ "$RACK" == "D" ]; then
	RFS1=192.168.1.30
	PRC=
	RFS="$RFS1"
else
	echo "Illegal rack selection $RACK. Must be a, A, b, B, c, C, d or D. Aborting."
	exit 1
fi

	ALL="$PRC $RFS"

if [ "$SCRIPTPATH" != "" ]
then
	cd $SCRIPTPATH
fi

if [ "$PYTHONPATH" != "" ]
then
	source $PYTHONPATH
fi

MSG="Beginning checkout for rack $RACK"
status

if [ $PROGRAM == 1 ]
then
    CURRENT=`pwd`
    echo $CURRENT
    cd ../submodules/qf2_pre
    for R in $ALL; do
	$PYTHON -m qf2_python.scripts.program_kintex_7 -t $R -b $BITFILE -v
    done
    cd $CURRENT

    MSG="FPGA programming complete"
    status
fi

if [ "$RACK" == "D" ]; then
$PYTHON lcls2_rack_helper.py -rfs1 $RFS1 -r $SPARTANFLAG $DIRFLAG $STATUSFLAG
elif [ "$RACK" == "C" ]; then
$PYTHON lcls2_rack_helper.py -rfs1 $RFS1 -prc $PRC -r $SPARTANFLAG $DIRFLAG $STATUSFLAG
else
$PYTHON lcls2_rack_helper.py -rfs1 $RFS1 -rfs2 $RFS2 -prc $PRC -r $SPARTANFLAG $DIRFLAG $STATUSFLAG
fi

MSG="Rack checkout complete"
status

exit 0
