#!/bin/sh
#
#   get_icc
#
#  Usage:   get_icc  (from any 50 station on the system)
#           JUST BE SURE NOBODY ELSE IS EDITING ANY CP/GW!!!!
#           Not sure?. Then, run "g_locked"
#
#  By: Angel Corbera, TSID1, Refinery Isla, Curacao, N.A.
#
#  Purpose: Create a Control Blocks database from ICC files
#           residing on each CP's host.
#  Database format:  
#           CP - CMPD - BLOCK - BLOCK_TYPE
#  Note: ECB blocks are included.
#
check_cp()
{
STAD=`/opt/fox/bin/tools/fist $x | grep $x |awk '{print $3}'`
if [ $STAD != "found" ]
then
   echo "CP is available...\c"
   flag=0
else
   echo "CP is NOT available...\c"
   flag=1
fi 
}
cd /opt/ac
echo "\nThis script will retrieve:"
echo "    CP-CMPD-BLK-BLKTYPE   into ascii file /opt/ac/PLANT.icc"
echo "from ALL icc CP/GW control databases in the system."
echo "All CP/GWs MUST be available. Nobody should be using ICC at this moment."
echo "Before to continue, do you want to check if any CP is locked (y/n)?"
read answer
if [ $answer = "y" ]
then
    if [ -f g_locked ]
    then
       /opt/ac/g_locked
    else
       echo "Script g_locked is missing from /opt/ac"
    fi
fi            
echo "If CP/GWs are locked ... 'get_cio' shows which station has ICC opened.\n"
echo "Is it ok to continue now? (y/n)?"
read answer
if [ $answer = "y" ]
then
   continue
else
   exit
fi
# rm *.tmp PLANT.icc
CPS=`sort /etc/cplns`
cd /opt/fox/ciocfg/api
for x in $CPS
do
 echo "Retrieving data from ICC files for $x ... \c"
 check_cp
 if [ $flag -eq "0" ]
 then
    iccprt -n \* -o /opt/ac/$x.tmp $x
 fi
 echo "Done!"
done
echo "\n"
cd /opt/ac
for x in $CPS
do
 echo "Cleanup, make-up, .. for $x ... \c"
 if [ -f $x.tmp ]
 then
    sed -e '/^-END/d' $x.tmp > tmp1
    sed -e '/^END/d' tmp1 > tmp2
    awk ' BEGIN { a="'$x'" }
      $0~/^LIST OF / { b=$9 }
      $0~/^  NAME:/ { printf "%-7s %-13s %-13s %-8s\n",a,b,$2,$4 }
      ' tmp2 > tmp3
    cat tmp3 >> PLANT.icc
    echo "Done!"
 else
    echo "File for $x not available"
 fi
done
echo "\nPLANT.icc is ready!\n"
rm tmp1 tmp2 *.tmp



