#!/bin/bash
#==============================================================
#  Name: rfInitResLcls2.sh
#  Abs:  For LCLS-II, initialize resonance/interlock chassis
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 17 Jan 2019
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#         rfInitResLcls2.sh <ip> <scriptdir> <bitfile> [<pythonsetup>] 
#
#============================================================

function usage {
echo "
   Purpose: This runs res_ctl.py, which programs the Kintex
            firmware and performs other initialization.

            Previous settings/values will be lost.
            
   Usage:

       rfInitInitLcls2.sh <cpu> <user> <ip> <qf2dir> <bitfile> [<pythonsetup>]

       Required:

          IP:       IP address

          SCRIPTDIR:   Path to lcls2_llrf/software/res_ctl directory

          BITFILE:    Kintex bitfile name, including path

       Optional:

          PYTHONSETUP: Python setup file to source
"
}

# Check for input arguments
NARGS=$#
if [ $NARGS -ne 4 ] -a [ $NARGS -ne 5 ]
then
        echo "rfInitResLcls2.sh: 3 or 4 arguments expected, but received ${NARGS}."
        usage
        exit 0
fi

IP=`echo $1`
SCRIPTDIR=`echo $2`
BITFILE=`echo $3`

if [ $NARGS -eq 4 ]
then
    PYTHONSETUP=`echo $4`
    echo $PYTHONSETUP
    source $PYTHONSETUP
fi

CURRENT=`pwd`
echo $CURRENT
cd $SCRIPTDIR/../submodules/qf2_pre
CMD="python -m qf2_python.scripts.program_kintex_7 -t $IP -b $BITFILE"
echo "Executing this command: ${CMD}"
$CMD
cd $CURRENT

exit 0
