#!/bin/bash
#==============================================================
#  Name: rfProgramFpgaLerf.sh
#  Abs:  Program Kintex firmware for LERF SRF rack chassis
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 20 Nov 2018
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#         rfProgramFpgaLerf.sh <prefix> <ip> <bitfile> <cpu> [<pythonsetup>]
#
#============================================================

function usage {
echo "
   Purpose: This loads the Kintex FPGA firmware.
            Previous settings/values will be lost.
            
   Usage:
       
       rfProgramFpgaLerf.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

          CPU:        CPU name

"
}

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

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

QF2DIR=/usr/local/lcls/package/lcls2_llrf/software/submodules/qf2_pre
OUTPUTDIR=/data/$CPU/llrf
PYTHONSETUP=/usr/local/lcls/epics/iocCommon/facility/GoPythonLinuxRTEnvs.sh

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

ssh laci@$CPU sh /usr/local/lcls/tools/script/rfProgramFpgaLcls2.sh $IP $QF2DIR $OUTPUTDIR $BITFILE $PYTHONSETUP

# Resume communication
caput -t $PREFIX:CTRL_RESET 1 

sleep 2

echo 'Done'

exit 0
