#!/bin/sh
# Script to create an FBM Database with the following fields:
#      CPlbug - Compound - Block - Type - FBM - Pnt
#
# The output file can be imported to Excel or any other spreadsheet.
# Includes lines for PLB, MAIN, MCIN, etc that do not specify PNT_NO.
#
# Created by: 
# A.Corbera (Nov96) -TSID1-Refinery ISLA-Curacao, N.A.
#
# Usage:  getfbm CPlbug
#
if [ $# != 1 ]
    then
        echo "Usage: $0 CPLBUG"
	echo "This script will create 2 files:"
	echo "/opt/ac/CPLBUG.icc = All compound/block parameters"
	echo "/opt/ac/CPLBUG.fbm = FBM database"
    exit 1
fi

CP=$1
cd /opt/fox/ciocfg/api
echo "Retrieving All Parameters from $CP Workfile. Please wait ...."
iccprt -p -o /opt/ac/$CP.icc $CP
echo "Compound/Block Parameters DB saved to /opt/ac/$CP.icc"
#
#
# If iccprt is not available, get parameters from CHECKPOINT file:
# 
# cd /usr/fox/sp/files
# echo "Retrieving parameters from $CP Checkpoint file. Please wait ...."
# /opt/fox/bin/tools/dbvu30 -t DB$CP.UC > /opt/ac/$CP.dbv
#
# Removing COMPND lines
# cat /opt/ac/$CP.dbv | grep -v COMPND > /opt/ac/z
# Removing asterisks, colons, dots from file.
# tr -d '[\047-\047][\052-\052][\056-\056]' < /opt/ac/z > /opt/ac/$CP.icc
# echo "Compound/Block Parameters DB saved to /opt/ac/$CP.icc"
# rm /opt/ac/$CP.dbv
#
echo "Extracting FBM parameters ..."
cd /opt/ac
cat $CP.icc | awk ' { a="'$CP'" }
	$1 == "NAME" { printf "\n%-8s\t%-28s",a,$3 }
	$1 == "TYPE" { printf "\t%-7s",$3 }
	$1 == "IOM_ID" { printf "\t%-7s",$3 }
	$1 == "PNT_NO" { printf "\t%-3s",$3 }' > z
# Converting semi-colons (:) to Tabs
tr : "\011" < z > x
#
echo "Formatting the output ..."
cat x | awk ' BEGIN { FS = "\t" }
NF == 6 { print $1,$2,$3,$4,$5,$6 }
NF == 5 { print $1,$2,$3,$4,$5 } ' > $CP.fbm
#
rm x z
echo "$CP's FBMs DB saved to /opt/ac/$CP.fbm. DONE!"
