#!/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 ...
#
cd /opt/ac
if [ ! -s mycps ]
	then
           sort /etc/cplns > mycps
fi
CPS=`cat /opt/ac/mycps`
echo "\nAbout to start retrieving Control databases from following CP/GWs:\n$CPS"
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 now."
	exit 1
fi
cd /opt/fox/ciocfg/api
for x in $CPS
do
  echo "\nProcessing $x ..."
  iccprt -p -o /opt/ac/$x.icc $x
  echo "$x database has been saved to /opt/ac/$x.icc..."
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
  # 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
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"