#!/bin/bash
#==============================================================
#  Name: rfTestViewLog.bash
#  Abs:  Display log file
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 23 May 2018
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#         rfTestViewLog.bash <pvprefix>
#
#         For LCLS-II LLRF checkout.
#         Display current log file
#
#============================================================

function usage {
echo "
   Purpose: Display current log file for specific LLRF rack

   Usage:
	  rfTestViewLog.bash <pvprefix>

          PVPREFIX: Prefix for EPICS PVs
                     Example for LERF first cryomodule, rack A
                         ACCL:L1B:0200:RACKA

"
}

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

PVPREFIX=`echo $1`

LOGDIR1=`caget -t ${PVPREFIX}:TESTLOGDIR1`
RVAL=${PIPESTATUS[0]}
if [ $RVAL -ne 0 ]
then
    echo "rfTestViewLog.sh: caget of log directory failed:"
    echo "${PVPREFIX}:TESTLOGDIR1"
    exit 0
fi

LOGDIR2=`caget -t ${PVPREFIX}:TESTLOGDIR2`
RVAL=${PIPESTATUS[0]}
if [ $RVAL -ne 0 ]
then
    echo "rfTestViewLog.sh: caget of log directory failed:"
    echo "${PVPREFIX}:TESTLOGDIR2"
    exit 0
fi

LOGFILE=`caget -t ${PVPREFIX}:TESTLOG`
RVAL=${PIPESTATUS[0]}
if [ $RVAL -ne 0 ]
then
    echo "rfTestViewLog.sh: caget of log filename failed:"
    echo "${PVPREFIX}:TESTLOG"
    exit 0
fi

LOG="${LOGDIR1}${LOGDIR2}/${LOGFILE}"
echo "Displaying log file: $LOG"

tail -F -n 10000 $LOG
