#!/bin/bash
#==============================================================
#  Name: rfViewLauncherLog.bash
#  Abs:  Display log file for LLRF daemon process
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 27 Oct 2020
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#      
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#         rfViewLauncherLog.bash <path> <cm> <nlines>
#
#============================================================

function usage {
echo "
   Purpose: Display log file for LLRF daemon process

   Usage:
	  rfViewLauncherLog.sh <path> <cm> <nlines>

         path = path to log file directory

         cm determines file name. Convention:
           For cm = ACCL:<AREA>:<id>, log file is:
             <path>/rf_control_launcher_<area>-rf<id>.log
             (AREA converted to lower case)
      
         nlines = number of lines to display (from end)
"
}

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

PATHARG=`echo $1`
CM=`echo $2 | tr '[:upper:]' '[:lower:]' | awk -F: ' { print $2 "-rf" $3 } '`
NLINES=`echo $3`

# Hard code log file in development environment
if [ -d /afs/slac/g/lcls ]; then
	LOG="/nfs/slac/g/lcls/tools/FEED/log/rf_control_launcher.log"
else
	LOG=`ls -l ${PATHARG}/rf_control_launcher_${CM}.log`
fi

RVAL=${PIPESTATUS[0]}
if [ $RVAL -ne 0 ]
then
    echo "rfViewLauncherLog.sh: ls of log file failed:"
    echo "echo ls -l ${PATHARG}/rf_control_launcher_${CM}.log"
    exit 0
fi

echo "Displaying log file: $LOG"

tail -F -n $NLINES $LOG
