#!/bin/sh
#
# snap_cmp   (Snapshot and Compare)
#
# Usage:   snap_cmp CPLBUG
#
# By: Angel Corbera (corbera@rocketmail.com)
#
# Purpose:   To know which blocks have changed
# their output/status after a CP has been rebooted,
# probably during an On-line upgrade.
# The script will check first if CP is not rebooting.
#
# Procedure:
# Run this script just BEFORE the CP is rebooted.
# It will save current values/status of selected
# blocks (PID controllers, AOUT, etc).
#
# Run this script AGAIN after the CP has rebooted
# completely.
# After getting the new values, the script will
# show ONLY the blocks that have changed their
# output values (AOUT), or Status (M/A, L/R).
#
# MA/LR retrieved from: PID, PIDA, PIDE, PIDX, PIDXE
#                       RATIO
# OUT values retrieved from: AOUT, AOUTR
#
cd /opt/ac
# Check for argument
CP=$1
if [ $# != 1 ]
then
   echo "\n\nUsage: $0 CPLBUG\n\n"
   exit 1
fi
#
# Check if CP is rebooting
#
ME=`uname -n`
LOCK=n
echo "Getting $CP host: ... :\c"
HOST=`awk '$1=="'$CP'" {print $2}' /usr/fox/sp/sldb`
echo "$HOST"
echo "Querying $HOST ...\c"
#
if [ $HOST = $ME ]
then
     ls -l /usr/fox/sp/locks | grep "$CP+"
     if [ $? -eq 0 ]
     then
        echo "$CP is locked!, \c"
        LOCK=y
     else
        echo "Not locked!, \c"
     fi
     ls -l /usr/fox/sp/locks | grep "$CP-"
     if [ $? -eq 0 ]
     then
        echo "$CP is REBOOTING!, "
        LOCK=y
     else
        echo "Not rebooting!, "
     fi
else
     rexec $HOST /usr/bin/ls -l /usr/fox/sp/locks > tmp1
     grep "$CP+" tmp1
     if [ $? -eq 0 ]
     then
        echo "$CP is locked!\c"
        LOCK=y
     else
        echo "Not locked!\c"
     fi
     grep "$CP-" tmp1
     if [ $? -eq 0 ]
     then
        echo "$CP is REBOOTING!"
        LOCK=y
     else
        echo "Not rebooting!"
     fi
fi
if [ $LOCK = y ]
then
   echo "$CP is locked or rebooting. Exiting!"
   exit 1
else
   echo "$CP is NOT locked, nor rebooting. Continuing."
fi

echo "\nDo you want to continue getting values/status (y/n)?"
read answer
if [ $answer = y ]
then
   continue
else
   exit 1
fi

if [ -f malr.txt ]
then
   mv malr.txt malr.old
   touch malr.txt
else
   touch malr.txt
fi
echo "\nMA/LR status for $CP controllers" >> malr.txt
echo "Date = `date`">> malr.txt
echo "MA= 0(Man) / 1(Auto)   LR= 0(Loc) / 1(Rem)\n" >> malr.txt
echo "Compound      Block          Type   MA LR" >> malr.txt
echo "==========================================" >> malr.txt
for x in PID PIDA PIDE PIDX PIDXE RATIO
do
  echo "Retrieving MA/LR status for $x blocks ...\c"
  /opt/fox/bin/tools/getpars -n -U$CP -mCMPNM:%14s -t$x -mTYPE:%7s -mMA:%0d -mLR:%0d > tmp1
  grep -v BLOCK-NAME tmp1 > tmp2
  awk '$0 ~ /^[A-Z0-9][A-Z0-9]/ {printf "%-14s%-14s %-6s %2d %2d\n",$2,$1,$3,$4,$5}' tmp2 >> malr.txt
  echo " Done!"
done
echo "\nCompound      Block          Type  MA  OUT  " >> malr.txt
echo "==========================================" >> malr.txt
for x in AOUT AOUTR
do
  echo "Retrieving OUT values from $x blocks ...\c"
  /opt/fox/bin/tools/getpars -n -U$CP -mCMPNM:%14s -t$x -mTYPE:%7s -mMA:%0d -mOUT:%.2f > tmp1
  grep -v BLOCK-NAME tmp1 > tmp2
  awk '$0 ~ /^[A-Z0-9][A-Z0-9]/ {printf "%-14s%-14s %-6s %0d %-6.2f\n",$2,$1,$3,$4,$5}' tmp2 >> malr.txt
  echo " Done!"
done
cat malr.txt
echo "\nALL these values have been saved to: /opt/ac/malr.txt"
echo "\nComparison between PREVIOUS and NEW files:"
echo "PREVIOUS : `grep Date malr.old`"
echo "NEW      : `grep Date malr.txt`\n"
echo "                                    PREVIOUS  |   N E W "
echo "Compound      Block          PIDx   MA   LR   |  MA   LR  "
echo "Compound      Block          AOUT   MA  OUT   |  MA  OUT  "
echo "================================================================"
sdiff malr.old malr.txt | grep "|" | grep -v Date > tmp1
awk '
   $3~/^PID/ {printf "%-14s%-14s %-6s %2d  %2d    |  %2d  %2d\n",$2,$1,$3,$4,$5,$10,$11}
   $3~/^RATIO/ {printf "%-14s%-14s %-6s %2d  %2d    |  %2d  %2d\n",$2,$1,$3,$4,$5,$10,$11}
   $3~/^AOUT/ {printf "%-14s%-14s %-6s %2d %-.2f  |  %2d %.2f\n",$2,$1,$3,$4,$5,$10,$11}
' tmp1 > /opt/ac/changes.txt
more /opt/ac/changes.txt
1