#!/bin/bash
#==============================================================
# luCompareKlystronDAta
#
# ARGUMENTS:
#
# OPTION:     
#
# Facility:  SLAC/LCLS
#
# Compare klystron and subbooster settings between SCP and EPICS
#
# Usage: luCompareKlystronData <location>
#
# Arguments: 
#            location: ioc location name, for example li28
# 
#----------------------------------------------------------------------------
# Mod:
#        
#==============================================================

loca=`echo $1 | tr '[:upper:]' '[:lower:]'`

LOCA=`echo $1 | tr '[:lower:]' '[:upper:]'`

appdir=`ls -l $EPICS_IOCS/sioc-${loca}-ky00/bin_epics | awk 'BEGIN {FS="/"} ; {print $15}'`

echo  Diffing ${LOCA} Klystron SLC and EPICS Settings - takes a couple minutes...

workpath='/home/physics/linacUpgrade/klys-diff'
datapath=/u1/lcls/epics/ioc/data/ioc-${loca}-cv01/slcdata

current=`pwd`

cd ${workpath}

# Copy over setInitialConditions scripts to use for a PV list
cp $EPICS_IOC_TOP/KlystronSupport/${appdir}/iocBoot/sioc-${loca}-ky00/setInitialConditions klys-${loca}.siocscript
cp $EPICS_IOC_TOP/KlystronSupport/${appdir}/iocBoot/sioc-${loca}-ky00/setInitialConditionsIOC klys-${loca}.iocscript

# Create one file from setInitialConditions scripts
# Exclude lines that do not include caget (they are for EPICS PVs with no SLC equivalent)
cat klys-${loca}.siocscript klys-${loca}.iocscript | grep caget > klys-${loca}.script

#----- Array PV handling -----

# Extract the array PVs; we use different caget parameters for these (wrt the scalars)
grep "\-a" klys-${loca}.script | awk 'BEGIN {FS="`"} ; {print $2}' | awk 'BEGIN {FS=" "} ; {print $3}' > klys-${loca}.slcpvlist_a
awk 'BEGIN {FS=":"} ; {print $2 ":" $1 ":" $3 ":" $4}' klys-${loca}.slcpvlist_a > klys-${loca}.epicspvlist_a

# Create caget scripts for EPICS and SLCCAS PVs; use default caget parameters for POLY and VFNP PVs
awk '!(/POLY/||/VFNP/) {print "caget -g 10 " $1} ; /POLY/||/VFNP/ {print "caget  " $1}' klys-${loca}.slcpvlist_a > klys-${loca}.slcscript_a
awk '!(/POLY/||/VFNP/) {print "caget -g 10 " $1} ; /POLY/||/VFNP/ {print "caget " $1}' klys-${loca}.epicspvlist_a > klys-${loca}.epicsscript_a

#----- Scalar PV handling -----

# Exclude arrays, as they are handled separately
# Create a file of klys/sbst EPICS PVs and SLCCAS PVs; use default caget parameters for SERN PVs
grep -v "\-a" klys-${loca}.script | awk 'BEGIN {FS="`"} ; {print $2}' | awk 'BEGIN {FS=" "} ; {print $3}' > klys-${loca}.slcpvlist_s
awk 'BEGIN {FS=":"} ; {print $2 ":" $1 ":" $3 ":" $4}' klys-${loca}.slcpvlist_s > klys-${loca}.epicspvlist_s

# Create caget scripts for EPICS and SLCCAS PVs
awk '!/SERN/ {print "caget -g 6 " $1}; /SERN/ {print "caget " $1}' klys-${loca}.slcpvlist_s > klys-${loca}.slcscript_s
awk '!/SERN/ {print "caget -g 6 " $1}; /SERN/ {print "caget " $1}' klys-${loca}.epicspvlist_s > klys-${loca}.epicsscript_s

#----- Merge array and scalar PVs -----

cat klys-${loca}.slcscript_a klys-${loca}.slcscript_s > klys-${loca}.slcscript
cat klys-${loca}.epicsscript_a klys-${loca}.epicsscript_s > klys-${loca}.epicsscript
cat klys-${loca}.epicspvlist_a klys-${loca}.epicspvlist_s > klys-${loca}.epicspvlist

# Make scripts executable
chmod +x klys-${loca}.slcscript
chmod +x klys-${loca}.epicsscript

# Execute scripts, extract values only and output them to a file
./klys-${loca}.slcscript > klys-${loca}.slcdata
awk '{$1=",";print}' klys-${loca}.slcdata > klys-${loca}.slcvals
./klys-${loca}.epicsscript > klys-${loca}.epicsdata
awk '{$1=",";print}' klys-${loca}.epicsdata > klys-${loca}.epicsvals

# Make sure there are equal number of rows in SLC PV data, EPICS PV data, and PV list
countslc=`wc -l klys-${loca}.slcvals | awk '{print $1}'`
countepics=`wc -l klys-${loca}.epicsvals | awk '{print $1}'`
countpvs=`wc -l klys-${loca}.epicspvlist | awk '{print $1}'`

if [ ${countslc} = ${countepics} -a ${countslc} = ${countpvs} ]; then
    echo 'SLC and EPICS caget results length match. Proceed.'
else
    echo 'SLC and EPICS caget results have different lengths; cannot diff these files'
    exit 0
fi

# Paste PV names, SLC values, and EPICS values into single file and remove whitespace
paste klys-${loca}.epicspvlist klys-${loca}.slcvals klys-${loca}.epicsvals | awk '{$1=$1};1' > klys-${loca}.data

# Extract lines where SLC and EPICS values do not match; add formatting to make it readable
awk 'BEGIN {FS="," ; 
printf "PV name              | SLC value            | EPICS value \n------------------------------------------------------------------\n"} ; 
($2!~$3) {printf "%-20s | %-20s | %s\n",$1,$2,$3 }' klys-${loca}.data > klys-${loca}.diff

datestr=`date '+%d-%h-%Y-%h:%M.%S'`

cp klys-${loca}.diff ${datapath}/klys-${loca}.diff.${datestr}

# Filter out readbacks for more useful diff (and SID for now--SLC provides in signed format; EPICS in unsigned)
grep -v MODE klys-${loca}.diff | grep -v SWRD | grep -v STAT | grep -v BC | grep -v BV | grep -v PERV | grep -v FEPK | grep -v REPK | grep -v FMON | \
grep -v KVAC | grep -v WVAC | grep -v KDT | grep -v KIT | grep -v TPAD | grep -v TMK | grep -v RFAP | grep -v AJTN | grep -v SID | grep -v DSTA | \
grep -v BVMX | grep -v AMPL | grep -v PRAW | grep -v PPAD | grep -v PHAS | grep -v DRPK | grep -v LRAT | grep -v DACV | grep -v VACT | grep -v ARAT | \
grep -v MRAT | grep -v PJTN | grep -v AJTN | grep -v KPHR > klys-${loca}.diff.filtered

cp klys-${loca}.diff.filtered ${datapath}/klys-${loca}.diff.filtered.${datestr}

# Print out difference for user
cat klys-${loca}.diff.filtered

echo Copying files to ${datapath}...

cd ${current}

echo Done. Exiting

exit 0
