#!/bin/sh
#
# geticc        (version 1.1)
#
# Usage:  geticc
#
# By: Angel Corbera, TSID1, Refinery Isla, Curacao, N.A.
# Comments to:  corbera@rocketmail.com
#
# This script will retrieve ALL Integrated Control Configuration
# parameters from ALL CPs/Gateways referenced in /opt/ac/mycps
#   ----- Be sure nobody else is using ICC at this time! -----
#
# The Output file:  PLANT.ICC , can be used as the input file
# for my script:  "icc2rows", to convert it to multi-column style.
#
# Steps to use the utilities:
#    - mkdir /opt/ac 
#    - Copy "geticc", "icc2rows", "get_head" utilities to /opt/ac
#    - If needed, delete undesired CP/GW lbugs from /opt/ac/mycps
#    - Run "geticc".   This will produce: PLANT.ICC
#    - Run "icc2rows". This will produce: PLANT.TXT
#    - Run "get_head". It will produce: HEADERS.TXT
#    - If desired, run "get_fbm. It will produce FBMS.TXT.
#    - Combine files:  cat HEADERS.TXT PLANT.TXT > COMBINED.TXT
#    - Transfer COMBINED.TXT to a PC.  Open it in Excel.
#    - Create a new column: A. Type AAAA for cells from HEADERS.TXT
#      Type BBBB for cells from PLANT.TXT.
#    - Sort now by BLOCKTYPE column, and by the new column A.
#    - Enjoy it ...
#
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 "ALL cmpd/block PARAMETERS into ascii file /opt/ac/PLANT.TXT"
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
cd /opt/ac
if [ ! -s mycps ]
	then
           sort /etc/cplns > mycps
fi
CPS=`cat /opt/ac/mycps`
echo "About to start retrieving Control databases from following CP/GWs:\n"
awk '{printf "%6s ",$1}' /opt/ac/mycps
echo "\nBe sure nobody else is accessing these databases!"
echo "(You may edit /opt/ac/mycps to process less CPs)"
echo "Do you want to continue (y/n)?"
read answer
if [ $answer = "n" ]
   then
        echo "If desired, edit /opt/ac/mycps "
	exit 1
fi
cd /opt/fox/ciocfg/api
for x in $CPS
do
  echo "\nProcessing $x ...\c"
  check_cp
  if [ $flag = "0" ]
  then
    iccprt -p -o /opt/ac/$x.icc $x
    echo "$x database has been saved to /opt/ac/$x.icc..."
  else
    echo "Skipping"
  fi
done
#
echo "\nConsolidating all icc files into one..."
cd /opt/ac
rm PLANT.ICC > /dev/null 2>&1
touch PLANT.ICC
for x in $CPS
do
  echo "\nAdding $x.icc ...\c"
  if [ -s $x.icc ]
  then
    # Replace any ! with another symbol (")
    tr ! "\042" < $x.icc > tmp1
    # Replace any @ with another symbol (a)
    tr @ a < tmp1 > tmp2
    # Insert CPname in Cmpd/Blk name
    sed -e 's/NAME   = /NAME   = '$x':/' tmp2 > tmp1
    # Delete all END lines at the end of each block
    sed -e 's/^  //' tmp1 | sed -e '/^END$/d' >> PLANT.ICC
    echo "Done!"
  else
    echo "Not found!"
  fi
done
rm tmp1 *.icc
BLKS=`grep "NAME   =" PLANT.ICC |wc -l|awk '{print $1}'`
echo "\nFile PLANT.ICC ($BLKS cmpd/blks) is ready to be processed by 'icc2rows' \n"



