#!/bin/ksh
#
#ident	"@(#)strip.eng	1.8	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Original Author: Michael Koller
#

# variables
hctsdir=SUNWhcts		# base directory
hname=$1		# test system
rdir=results		# results directory
prgdir=prgs		# program directory
ffile=$2		# form file
outfile=$3		# output file
i=0			# loop counter
dval=			# data value
inst=0			# number of instances
rfs=0			# number of required fields
lpart=			# label part
fpart=			# field part
tmpo=form.tmp		# temp form
tmpf=cmdform.tmp	# temp command form
tmpc=cmdfile.tmp	# temp command file

# functions

# do function
go_fun()
{
	$prgdir/funcs "$*" 2>&1
}

### MAIN ###

# change to base directory
cd `dirname $0`
while [ `basename $PWD` != $hctsdir ]
do
	cd ..
done

# check arg
if [ ! -a $ffile ]
then
	go_fun bhtml $hname
        go_fun err_pg "Command Form File $ffile not found."
	go_fun ehtml
else
# strip command form
	inst=0
	rm -f $rdir/$hname/$tmpo $rdir/$hname/$tmpc
	cat $ffile | while read inline
	do
		ch=`echo "$inline" | cut -f1 -d':'`
		lpart=`echo "$inline" | cut -f2 -d':'`
		fpart=`echo "$inline" | cut -f3- -d':'`
		case $ch in
		T)
			if [ -z "$fpart" ]
			then
				((rfs=rfs+1))
			fi
			echo "$inline" >> $rdir/$hname/$tmpo
		;;
		x)
			echo "$ch:$lpart:$dval" >> $rdir/$hname/$tmpo
			if [ $lpart = 3 ]
			then
				inst=$dval
			elif [ $lpart = wait ]
			then
				if [ $inst -gt 0 ] && [ $rfs -eq 0 ]
				then
					cat $rdir/$hname/$tmpo >> $rdir/$hname/$tmpc
				fi
				rm -f $rdir/$hname/$tmpo
				inst=0
				rfs=0
			fi
		;;
		*)
			echo "$inline" >> $rdir/$hname/$tmpo
		;;
		esac					
		dval=$fpart
	done
	if [ -s $rdir/$hname/$tmpc ]
	then
		if [ "$ffile" = "$outfile" ]
		then
			cat $rdir/$hname/$tmpc > $outfile
		else
			cat $rdir/$hname/$tmpc >> $outfile
		fi
	else
		rm -f $ffile $outfile
	fi
	rm -f $rdir/$hname/$tmpo $rdir/$hname/$tmpc $rdir/$hname/$tmpf
	if [ -s $outfile ]
	then
		$prgdir/pcf.eng $hname $outfile
	else
		go_fun bhtml $hname
		go_fun err_pg "Empty fields found or zero instances."
		go_fun ehtml
		exit 1
	fi
fi
