#!/bin/sh
#
#   get_fbm    (version 1.7)
#
# Usage:  get_fbm filename
# where  filename = PLANT.ICC or PLANT.DBV
#
# By:  Angel Corbera, TSID1, Refinery Isla, Curacao, N.A.
# Comments to:  corbera@rocketmail.com
#
# Purpose: Create an FBM Database with the following fields:
#   CP Cmpd Blk BlkType FBMlbug PNT_NO PNT2 PNT3 PNT4 FBMtype
# from file PLANT.ICC (or PLANT.DBV) created with geticc (or
# dbv2rows) utility, and system configuration data from IIF.pkg.
#
# The output file FBMS.TXT can be imported by Excel using "!"
# as delimiter.
#
#                        NOTES:
# - FBMs without FBMtype: FBM was NOT installed via System Cfg,
#    or the block is taking data from Gateways.
# - PLB blocks show their FBM Letterbug only, NOT the points used!
# - AINR and AOUTR list only their -MAIN- FBM Lbug.
# - MOVLV, MTR, and GDEV show their points in the same row!.
#
# Enjoy it!
#

if [ $# != 1 ]
    then
        echo "\nUsage: $0 filename"
        echo "where  filename = PLANT.ICC or PLANT.DBV"
    exit 1
fi
#
INP_FILE=$1
#
echo "CP!Compound!Block!BlkType!IOM_ID!PNT_NO!PNT2!PNT3!PNT4!FBMtype" > FBMS.TXT
echo "\nExtracting FBM parameters from $1 ...\c"
cd /opt/ac
cat $1 | awk ' 
	$1 == "NAME"   { printf "\n%0s!",$3 }
	$1 == "TYPE"   { printf "%0s!",$3 }
	$1 == "IOM_ID" { printf "%0s!",$3 }
	$1 == "PNT_NO" { printf "%0s!",$3 }
	$1 == "LM1_PT" { printf "%0s!",$3 }
	$1 == "LM2_PT" { printf "%0s!",$3 }
	$1 == "CO1_PT" { printf "%0s!",$3 }
	$1 == "CO2_PT" { printf "%0s!",$3 }' > tmp1
echo " formatting output ...\c"
# Delete trailing !s
sed -e 's/\!$//' tmp1 > tmp2
# Converting semi-colons (:) to !
tr : "\!" < tmp2 > tmp1
#
cat tmp1 | awk -F! ' 
NF==9 { printf "%0s!%0s!%0s!%0s!%0s!%0s!%0s!%0s!%0s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9 }
NF==8 { printf "%0s!%0s!%0s!%0s!%0s!%0s!%0s!%0s!\n",$1,$2,$3,$4,$5,$6,$7,$8 }
NF==7 { printf "%0s!%0s!%0s!%0s!%0s!%0s!%0s!!\n",$1,$2,$3,$4,$5,$6,$7 }
NF==6 { printf "%0s!%0s!%0s!%0s!%0s!%0s!!!\n",$1,$2,$3,$4,$5,$6 }
NF==5 { printf "%0s!%0s!%0s!%0s!%0s!!!!\n",$1,$2,$3,$4,$5 }' > tmp2
#
if [ $INP_FILE = "PLANT.DBV" ]
then
  awk -F! '{
   if ( $4 == "GDEV" || $4 == "MOVLV" || $4 == "MTR" || $4 == "VLV" )
      { printf "%0s!%0s!%0s!%0s!%0s!%0s!%0s!%0s!%0s\n",$1,$2,$3,$4,$7,$5,$6,$8,$9 }
   else
      { print $0 }
   }' tmp2 > FBMS
else
  cat tmp2 > FBMS
fi
#
echo ".. Done!"
#
#
echo "Extracting FBM Types from /usr/fox/sp/IIF.pkg ...\c"
grep FBM /usr/fox/sp/IIF.pkg | awk '{print $3,$5}' > FBM_TYPE
N=`wc -l FBM_TYPE | awk '{print $1}'`
echo " $N FBMs found ... Done!"
#
echo "Combining both files...\c"
awk -F! ' 
   FILENAME == "FBM_TYPE" {
	split($0,entry," ")
	fbms[entry[1]]=entry[2]
	next
		}
$0 ~ /./ { 
	for ( x in fbms )
		if ( $5 == x ) {
		a=fbms[x]
				}
	}
{
printf "%0s!%0s!%0s!%0s!%0s!%0s!%0s!%0s!%0s!%0s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,a
a=X
}' FBM_TYPE FBMS > tmp2

#Additional string (???)  for PLB blocks
awk -F! ' {
	if ( $4=="PLB" )
	 {printf "%0s!%0s!%0s!%0s!%0s!???!%0s!%0s!%0s!%0s\n",$1,$2,$3,$4,$5,$7,$8,$9,$10}
	else
	 {print $0}
}' tmp2 >> FBMS.TXT

echo "Done!.\n"
echo "FBMS.TXT file is ready to be taken to Excel!."
#
# Additional check for PLB blocks.
PLB=`grep "\!PLB\!" FBMS.TXT | wc -l | awk '{print $1}'`
if [ $PLB -gt 0 ]
  then
    echo " ***** Remember to check which points are used in your $PLB PLB blocks! *****"
  else
    echo " No PLB blocks found!"
fi
#
# Additional check for Redundant FBMS:  AOUTR blocks
AINR=`grep "\!AINR\!" FBMS.TXT | wc -l | awk '{print $1}'`
AOUTR=`grep "\!AOUTR\!" FBMS.TXT | wc -l | awk '{print $1}'`
if [ $AINR -gt 0 ] || [ $AOUTR -gt 0 ]
  then
    echo " Remember: Your $AINR AINR and $AOUTR AOUTR blocks show points from their MAIN FBM only!\n"
  else
    echo " No AINR/AOUTR blocks found!\n"
fi
# Cleaning up
rm tmp1 tmp2 FBMS FBM_TYPE > /dev/null 2>&1
1