#!/bin/bash
#==============================================================
#  Name: rfInitResFnal.sh
#  Abs:  Initialize resonance chassis for LCLS-II LLRF
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 17 Jan 2019
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#         rfInitResFnal.sh <prefix> <ip> <bitfile>
#
#============================================================

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:
       
       rfInitResLerf.sh <prefix> <ip> <bitfile> [<pythonsetup>]

       Required:

          PREFIX:     Chassis PV prefix, ex ACCL:AD10:0100:RESA

          IP:         Chassis IP, ex 192.168.0.100
          
          BITFILE:    Kintex bitfile name, including path
"
}

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

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

# Halt communication between EPICS and chassis
caput -t $PREFIX:CTRL_HALT 1 

echo "Executing python /usr/local/lcls/package/lcls2_llrf/software/res_ctl/prc.py -a $IP -r -b $BITFILE"
xvfb-run python /usr/local/lcls/package/lcls2_llrf/software/res_ctl/res_ctl.py -a $IP -r -b $BITFILE

# Resume communication
caput -t $PREFIX:CTRL_RESET 1 

sleep 2

echo 'Done'

exit 0
