#!/bin/sh
#
#  updatecp
#
#  Usage:  updatecp
#
#  By:  Angel Corbera, TSID1, Refinery Isla, Curacao, N.A.
#  Comments to:  corbera@rocketmail.com
#
#  Purpose:  
#  Upload, Shrink, Checkpoint, and SaveAll CPs hosted by this AP/AW.
#
#  This script should be run ONLY on each CPs HOST,
#      better to run it by crontab at midnight (nobody using ICC!).
#  It will process only CP/GW that are ONLINE and NOT LOCKED!.
#  To begin create directory /opt/ac, copy this script to that directory,
#      and run it from there.
#  This script will find all CP/GWs hosted by this station, the
#      only customization needed is:
#  - If you want to process only specific CP/GWs, create a file
#     /opt/ac/MYCPS with a CP/GW lbug name per line
#  - Add any Remote Display Manager NAMES at the end of line "for x in $WP50..."
#  - Replace HLAP02 with your 2nd backup station lbug on line:
#        echo "Starting file transfer to HLAP02 ... \c"
#
# Additional actions of this script:
# - Create list of all CP/GWs hosted by this AP
# - Create list of all WP/AW/PW in this system
# - Send message/warnings to all screens
# - Create directories for each CP/GW under /opt/saveall
# - SaveAll each CP to /opt/saveall/CPLBUG
# - tar CP saveall files into: svallAPLBUG.tar
# - tar CP chkpnt files into:  chkptAPLBUG.tar
# - Compress both tar files and send them to another
#      50 station in the system, for additional backup.
# - Send activities info to /opt/ac/updatecp.log
# - Send log file via e-mail to I/A SysAdmin.
#
#
# -------------------------------------------------------------------
# Customs for HL plant:
# - Remote DMs = DMTSI1 DMTSI2 DMTSI3 DMDCS1 DMDCS2
# - Second 50 station for backup:  HLAP02
# -------------------------------------------------------------------
#
chk_lock()
{
  cd /opt/ac
  ls -l /usr/fox/sp/locks > tmp1
  awk ' $0 ~ /\+$/ {print substr ($NF,2,length($NF)-2)}' tmp1 > LOCKED
  grep $1 LOCKED
  if [ $? -eq 0 ] 
  then
     echo "$x is locked!...\c"
     lock=1
  else
     echo "$x is not locked...\c"
     lock=0
  fi
}
check_cp()
{
STAD=`/opt/fox/bin/tools/fist $x | grep $x |awk '{print $3}'`
if [ $STAD != "found" ]
then
   echo "CP is online."
   flag=0
else
   echo "CP is NOT online!"
   flag=1
fi  
}

cd /opt/ac
echo "\nThis script will Shrink/Update/Checkpoint/Save_All all CP's -HOSTED- by THIS station"
echo "If you  want only specific CPs, create ascii file /opt/ac/MYCPS with one CP lbug per line"
echo "\nSaveall files will be saved to directories: /opt/saveall/CPLBUG"
echo "Be sure nobody is editing any CP at this moment!"
echo "Do you want to continue (y/n)?"
read answer
if [ $answer != "y" ]
then
   exit 1
fi

rm tmp1 tmp2 updatecp.log > /dev/null 2>&1
ME=`uname -n`
CPS=`sort /etc/cplns`
#
touch updatecp.log update.log
#
echo "\nGetting my CPs list ..."
file=new

if [ -f /opt/ac/MYCPS ]
then
   echo "   Previous MYCPS file found!. Do you want to use it? (y/n)"
   read answer
   if [ $answer = "y" ]
   then
      file=old
   else
      rm /opt/ac/MYCPS
      file=new
   fi
else
   echo "   No previous /opt/ac/MYCPS file found. Creating new one."
fi

if [ $file = "old" ]
then
   echo "   Using partial list of CP/GWs on /opt/ac/MYCPS."
else
   awk '{print $1, $2}' /usr/fox/sp/sldb | grep $ME > tmp1
   for x in $CPS
   do
      awk ' $1=="'$x'" {print $1}' tmp1 >> MYCPS
   done
   echo "   Using ALL CP/GWs hosted by this station."
   if [ ! -s MYCPS ]
   then
      echo "This station does not host any CP!.  Exiting.\n"
      rm /opt/ac/MYCPS
      exit 1
   fi
fi
#
 
cat /opt/ac/MYCPS | awk '{printf "%6s ",$1}'
#
echo "\n\nGetting WP/AWs list ...\c"
sort /usr/fox/sp/hldb > tmp2
WP50=`awk '$2=="406" {print $1}' tmp2`
AW50=`awk '$2=="306" {print $1}' tmp2`
WP51=`awk '$2=="409" {print $1}' tmp2`
AW51=`awk '$2=="309" {print $1}' tmp2`
PWS=`awk '$2=="1001" {print $1}' tmp2`
echo "Done!"
rm tmp1 tmp2 > /dev/null 2>&1
#
# Check if directories exist:
if [ ! -d /opt/saveall ]
then
   mkdir /opt/saveall
fi
for x in `cat /opt/ac/MYCPS`
do
  if [ ! -d /opt/saveall/$x ]
  then
    mkdir /opt/saveall/$x
  fi
done

echo "===========================================" >> updatecp.log
echo "updatecp script started at: `date`" >> updatecp.log
#
# Warning Message to all local/remote Display Managers.
#
echo "\nSending WARNING Starting message to all screens ...\c"
for y in $WP50 $AW50 $WP51 $AW51 $PWS DMTSI1 DMTSI2 DMTSI3 DMDCS1 DMDCS2
do
  /usr/local/pref -$y dmcmd 'msglin "About to start UPLOAD/CHKPOINT/SAVEALL CPs. Exit from CtlCfg NOW!!!"'
done
echo "Done!"
#
# Upload/Shrink CP/GWs.
#
echo "\nStarting UPLOAD/SHRINKs"
for x in `cat /opt/ac/MYCPS`
do
  chk_lock $x
  check_cp
  if [ $lock = "0" ] && [ $flag = "0" ]
  then
    for y in $WP50 $AW50 $WP51 $AW51 $PWS DMTSI1 DMTSI2 DMTSI3 DMDCS1 DMDCS2
    do
      /usr/local/pref -$y dmcmd 'msglin "Uploading/Shrinking '$x' ...!!!"'
    done
    cd /opt/fox/ciocfg/api
    TYPE=`awk '$1=="'$x'" {print $2}' /usr/fox/sp/hldb`
    START=`date`
    echo "   Uploading/Shrinking $x ($TYPE) ... \c"
    echo OPEN $x MODIFY ACorbera > /opt/ac/$x.upshrink
    echo UPLOAD  >> /opt/ac/$x.upshrink
    echo SHRINK  >> /opt/ac/$x.upshrink
    echo CLOSE   >> /opt/ac/$x.upshrink
    echo EXIT    >> /opt/ac/$x.upshrink
    iccdrvr.tsk -i /opt/ac/$x.upshrink -o /opt/ac/$x.log -n ECHO
    cd /opt/ac
    cat $x.log >> /opt/ac/updatecp.log
    rm $x.log 2> /dev/null
    echo "Done!"
    END=`date`
    echo "$x UPSHRINK:  Start=$START ;  End=$END" >> /opt/ac/updatecp.log
    sleep 1
  else
    echo "$x is locked or not online. SHRINK/UPLOAD skipped!" >> /opt/ac/updatecp.log
  fi
done
#
# Checkpoint CP/GWs
#
echo "\nStarting CHECKPOINTS"
for x in `cat /opt/ac/MYCPS`
do
  chk_lock $x
  check_cp
  if [ $lock = "0" ] && [ $flag = "0" ]
  then
    for y in $WP50 $AW50 $WP51 $AW51 $PWS DMTSI1 DMTSI2 DMTSI3 DMDCS1 DMDCS2
    do
      /usr/local/pref -$y dmcmd 'msglin "Checkpointing '$x' ...!!!"'
    done
    START=`date`
    HOST=`awk ' $1=="'$x'" {print $2}' /usr/fox/sp/sldb`
    echo "   Checkpointing $x ... \c"
    /opt/fox/bin/tools/cpoint $x $ME
    echo "Done!"
    END=`date`
    echo "$x CHKPT :  Start=$START ;  End=$END" >> /opt/ac/updatecp.log
    sleep 300
  else
    echo "$x is locked or not online. CHECKPOINT skipped!" >> /opt/ac/updatecp.log
  fi
done

#
#  SaveAll CP/GWs
#
echo "\nStarting SAVEALLs"
cd /opt/fox/ciocfg/api
for x in `cat /opt/ac/MYCPS`
do
  chk_lock $x
  check_cp
  if [ $lock = "0" ] && [ $flag = "0" ]
  then
    for y in $WP50 $AW50 $WP51 $AW51 $PWS DMTSI1 DMTSI2 DMTSI3 DMDCS1 DMDCS2
    do
      /usr/local/pref -$y dmcmd 'msglin "SavingAll '$x' ...!!!"'
    done
    START=`date`
    echo "   SavingAll $x :"
    cd /opt/fox/ciocfg/api
    save_all $x /opt/saveall/$x
    echo "       Done!"
    END=`date`
    echo "$x SAVEALL: Start=$START ;  End=$END" >> /opt/ac/updatecp.log
    sleep 1
  else
    echo "$x is locked or not online. SAVEALL skipped!" >> /opt/ac/updatecp.log
  fi
done
#
# End Message to all screens.
#
echo "\nSending Finish message to all screens ...\c"
for y in $WP50 $AW50 $WP51 $AW51 $PWS DMTSI1 DMTSI2 DMTSI3 DMDCS1 DMDCS2
do
  /usr/local/pref -$y dmcmd 'msglin "UPLOAD/CHKPOINT/SAVEALL CPs finished!"' 
done
echo "Done!"
#
#  Tar files for SaveAlls & Checkpoints ,..... 
#
echo "\nCreating/Compressing tar files for SaveAlls and Checkpoints ... "
cd /opt/saveall
tar cvf svall$ME.tar ./[A-Z]*
cd /usr/fox/sp/files
tar cvf /opt/saveall/chkpt$ME.tar ./DB??????.UC
cd /opt/saveall
rm svall$ME.tar.Z
rm chkpt$ME.tar.Z
compress svall$ME.tar
compress chkpt$ME.tar
echo "     Done!"
#
# Transferring files to a second 50 station
# >>>   Replace HLAP02 with your APLBUG. <<<
# and create directory /opt/saveall on that station.
#
cd /opt/saveall
echo "\nStarting file transfer to HLAP02 ... \c"
rm ftp.inp > /dev/null 2>&1
touch ftp.inp
echo "user root gnomes" >> ftp.inp
echo "binary" >> ftp.inp
echo "cd /opt/saveall" >> ftp.inp
echo "put svall$ME.tar.Z" >> ftp.inp
echo "put chkpt$ME.tar.Z" >> ftp.inp
echo "bye" >> ftp.inp
ftp -n HLAP02 < ftp.inp
echo "Done!"
#
echo "\nupdatecp script finished at: `date`" >> /opt/ac/updatecp.log
#
# Send log to I/A SysAdmin
#
cd /opt/ac
mail tsid1@email.isla.pdv.com < updatecp.log
rm LOCKED
#
cat updatecp.log >> update.log
1