#!/bin/ksh
#
#ident	"@(#)pcf.eng	1.9	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Original Author: Michael Koller
#
# This program takes a form file as input, and prints to
# a STAF command file as output.
# Input File = $ffile
# Output file = /opt/SUNWstaf/cmd-files/$hname/$cmdfile

# variables
hctsdir=SUNWhcts				# base directory
hname=$1				# test system
ffile=$2				# form file
rdir=results				# results directory
act=					# action to be taken
i=0					# loop counter
inline=					# input value
cmdfile=				# command file
ch=					# char var
lpart=					# label
fpart=					# field
prgdir=prgs				# program directory
stafcdir=/opt/SUNWstaf/cmd-files	# STAF command file directory

# functions

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

empar()
{
	i=1
	while [ ! -z "${cmdval[$i]}" ]
	do
		if [ $i -eq 1 ]
		then
			echo "${cmdval[$i]}:\c"
		else
			echo "${cmdval[$i]} \c"
		fi
		cmdval[$i]=""
		((i=i+1))
	done
	echo ""
}

### MAIN ###

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

# command form to command file
if [ ! -a $ffile ]
then
	go_fun err_pg "Command Form File $ffile not found."
else	
	cmdfile=`basename $ffile`
	cmdfile=`echo $cmdfile | sed -e s/.cfm/.cmd/`
	cmdfile=$stafcdir/$hname/$cmdfile
	if [ ! -d $stafcdir/$hname ]
	then
		mkdir -p $stafcdir/$hname
	fi
	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
		h)
			echo "# $lpart" | sed s/"<.>"//g | sed s/"<..>"//g			
		;;		
		x|X)
			if [ $lpart = wait ] || [ $lpart = "LOG=true" ]
			then
				empar
				if [ "$fpart" = "Yes" ]
				then
					echo "$lpart"
				elif [ $lpart == "LOG=true" ]
				then
					echo "LOG=false"
				fi
			else
				cmdval[$lpart]=$fpart
			fi
		;;
		esac
	done > $cmdfile
fi       
