#!/bin/bash
#==============================================================
#  Name: rfProgramFpgaLcls.sh
#  Abs:  For LCLS-II LLRF chassis, program Kintex Firmware
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 20 Nov 2018
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#         rfProgramFpgaLcls2.sh <ip> <qf2dir> <bitfile> <outputdir> [<pythonsetup>] 
#
#============================================================

function usage {
echo "
   Purpose: This loads the Kintex FPGA firmware.
            Previous settings/values will be lost.
            
   Usage:

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

       Required:

          IP:       IP address

          QF2DIR:   Path to QF2Pre directory

          OUTPUTDIR:  Indicates where LLRF scripts on CPU write/read data files
                      Can be '.'

          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 "rfProgramFpgaLcls2.sh: 4 or 5 arguments expected, but received ${NARGS}."
        usage
        exit 0
fi

IP=`echo $1`
QF2DIR=`echo $2`
OUTPUTDIR=`echo $3`
BITFILE=`echo $4`

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

echo "rfProgramFpgaLcls2.sh: Programming Kintex firmware"

CURRENT=`pwd`
echo $CURRENT
cd $QF2DIR
echo "Executing this command: python -m qf2_python.scripts.program_kintex_7 -t $R -b $BITFILE -v"
python -m qf2_python.scripts.program_kintex_7 -t $IP -b $BITFILE -v
cd $CURRENT

exit 0
