#!/bin/sh
#
# icc2rows        (version 1.0)
#
# Usage:  icc2rows PLANT.ICC
#
# By: Angel Corbera (TSID1), Refinery Isla, Curacao, N.A.
# Comments to:  corbera@rocketmail.com
#
# This script will convert a Control Blocks Database file: PLANT.ICC,
# in a single long column, to another ascii file: PLANT.TXT, 
# with Block parameters in a multi-column one-line per Block format.
# The output file can be imported by an spreadsheet program like Excel,
# or any database pgm, where parameters comparison could be done easily 
# in this format.
#
# The input file PLANT.ICC can be produced with my other utility: "geticc",
# that uses /opt/fox/ciocfg/api/iccprt to get the parameters.
#
# The output file PLANT.TXT can be opened in Excel, using "!" as delimiter.
# The file can be sorted by Blocktype, and Headers added for each type
# of block (HEADERS.TXT is obtained from "get_head" utility).
#
# Note: v4.x CALC blocks have the highest number of parameters: 152,
# and they take up to the EW column.  Others: FDSCAN (140), IND (120),
# PIDA (116), PIDX (102), FDRIN (95), PID (90).
#
# If you want, instead of working with one big file PLANT.TXT, you might
# want to split it into several small files, one for each type of blocks:
#	grep \!AIN\! PLANT.TXT > ain.txt
#	grep \!PID\! PLANT.TXT > pid.txt
#	... and so on
# Each of these files could be taken to Excel.
#
# Enjoy it! 
#
# Steps to use the utilities:
#    - mkdir /opt/ac 
#    - Copy "geticc", "icc2rows", "get_head" utilities to /opt/ac
#    - cp /etc/cplns /opt/ac/mycps
#    - If needed, delete undesired CP/GW names from mycps
#    - Run "geticc".   This will produce: PLANT.ICC
#    - Run "icc2rows". This will produce: PLANT.TXT
#    - Run "get_head". It will produce: HEADERS.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 PLANT.ICC ]
	then
	   echo "\nPLANT.ICC does not exist!"
	   echo "Run geticc to create it."
	   exit 1
fi
echo "\nProcessing PLANT.ICC ... please wait."
sed -e 's/NAME   = /NAME   = @/' PLANT.ICC > tmp1
sed -e 's/= /z/' tmp1 > tmp2
sed -e 's/NAME   z@/NAME   @/' tmp2 > tmp1
cut -c8-70 tmp1 > tmp2
tr "\012" ! < tmp2 > tmp1
tr @ "\012" < tmp1 > tmp2
tr -d z < tmp2 > tmp1
sed -e 's/\!COMPND\!/\!\!COMPND\!/' tmp1 > tmp2
sed -e 's/:/\!/' tmp2 | sed -e 's/:/\!/' > PLANT.TXT
BLKS=`wc -l PLANT.TXT | awk '{print $1}'`
rm tmp1 tmp2
echo "\nThe ascii file PLANT.TXT ($BLKS cmpd/blocks) is ready to be taken to Excel...\n"