#!/bin/ksh
#
#ident	"@(#)form.eng	1.16	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
act=			# action to be taken
prgdir=prgs		# program directory
bname=`basename $0`	# shell name
gotocgi="http://$HTTP_HOST/cgi-bin/$hctsdir/$prgdir/$bname"	# program name
mdir=menu		# menu directory
rdir=results		# results directory
ffile=$2		# form filename
jpage=$3		# jump to page
var0=			# field array
inline=			# input value
i=0			# loop counter
x=0			# loop counter
z=0			# loop counter
tfile=form.tmp		# tmp file
ch=			# char
radio=0			# radio flag
rval=			# radio value
lpart=			# label part
fpart=			# field part
jargs=$4		# jump args
hour=           # run time hour
minute=         # run time minute
tmphour=         # temp variable used to analyse hour
tmpminute=         # temp variable used to analyse minute

# functions

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

### MAIN ###

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

# parse input 
if [ -n "$CONTENT_LENGTH" ]
then
	eval `$prgdir/pq.p`
	CONTENT_LENGTH=
fi

# display form
if [ -z "$act" ]
then
	if [ -z "$ffile" ] || [ ! -a $ffile ]
	then
		go_fun err_pg "File $ffile not found."
	elif [ ! -s $ffile ]
	then
		go_fun err_pg "Empty FORM."
	else
		echo '<P><FONT COLOR="#FF0000"><B>RED</B></FONT> fields are REQUIRED and must be entered.'
		echo '<FORM ACTION="'$gotocgi'" METHOD="POST">'
		echo '<TABLE WIDTH="100%">'
		i=0
		cat $ffile | while read inline
		do
			ch=`echo "$inline" | cut -f1 -d':'`
			if [ $ch != r ] && [ $radio -ne 0 ]
			then
				((i=i+1))
				radio=0
			fi
			lpart=`echo "$inline" | cut -f2 -d':'`
			fpart=`echo "$inline" | cut -f3- -d':'`
			case $ch in
			t|T)											
				if [ $ch = T ]
				then
					echo "<TR><TD>"
					if [ -z "$fpart" ]
					then
						echo "<FONT COLOR='#FF0000'>$lpart</FONT>"
					else
						echo "$lpart"
					fi
					echo "</TD>"
				else
					echo "<TR><TD>$lpart</TD>"
				fi				
				echo '<TD><INPUT NAME="'var$i'" VALUE="'$fpart'"></TD></TR>'
				((i=i+1))				
			;;
			c)
				echo "<TR><TD>$lpart</TD>"
				if [ "$fpart" = "Yes" ]
				then
					echo '<TD><INPUT TYPE="checkbox" NAME="'var$i'" VALUE="Yes" CHECKED> Yes'
				else
					echo '<TD><INPUT TYPE="checkbox" NAME="'var$i'" VALUE="Yes"> Yes'
				fi
				echo "</TD></TR>"
				((i=i+1))
			;;
			r)
				echo "<TR><TD>$lpart</TD>"
				if [ "$fpart" = "Yes" ]
				then
					echo '<TD><INPUT TYPE="radio" NAME="'var$i'" VALUE="'$radio'" CHECKED> Yes'
				else
					echo '<TD><INPUT TYPE="radio" NAME="'var$i'" VALUE="'$radio'"> Yes'
				fi
				echo "</TD></TR>"
				((radio=radio+1))
			;;
			h)
				echo '<TR><TD>'$lpart'</TD>'
				echo '<TD><INPUT TYPE="hidden" NAME="'var$i'" VALUE="'$fpart'"></TD></TR>'
				((i=i+1))
			;;
			n)
				echo "<TR><TD>"
				echo "$lpart" | cut -f1 -d'!'
				x=`echo "$lpart" | cut -f2 -d'!'`
				z=`echo "$lpart" | cut -f3 -d'!'`
				((rval=(z-x)+1))
				if [ $rval -le 20 ]
				then
					echo '</TD><TD><SELECT NAME="'var$i'" SIZE=1>'
				else
					echo '</TD><TD><SELECT NAME="'var$i'" SIZE=3>'
				fi
				while [ $x -le $z ]
				do
					if [ $fpart -eq $x ]
					then
						echo "<OPTION SELECTED>$x"
					else
						echo "<OPTION>$x"
					fi
					((x=x+1))
				done
				echo "</SELECT></TD></TR>"
				((i=i+1))
			;;
			x|X)
				echo '<TD><INPUT TYPE="hidden" NAME="'var$i'" VALUE="'$fpart'"></TD></TR>'
				((i=i+1))
                        ;;
                        esac
		done
		echo "</TABLE>"
		echo '<INPUT TYPE="hidden" NAME="act" VALUE="process">'	
		echo '<INPUT TYPE="hidden" NAME="hname" VALUE="'$hname'">'
		echo '<INPUT TYPE="hidden" NAME="ffile" VALUE="'$ffile'">'
		if [ -n "$jpage" ]
		then
			echo '<INPUT TYPE="hidden" NAME="jpage" VALUE="'$jpage'">'
			echo '<INPUT TYPE="hidden" NAME="jargs" VALUE="'$jargs'">'
		fi
		echo '<P><INPUT TYPE="SUBMIT" VALUE="SAVE Form">'	
		echo '<INPUT TYPE="reset" VALUE="Reset Form">'
		echo "</FORM>"
	fi
	go_fun ehtml
	exit 0
fi

# process form content
if [ $act = process ]
then
	i=0 
	rval=0
	radio=0
	cat $ffile | while read inline
	do
		ch=`echo "$inline" | cut -f1 -d':'`
		lpart=`echo "$inline" | cut -f1-2 -d':'`
		if [ $ch = r ]
		then			
			if [ $radio -eq 0 ]
			then
				eval rval="\$var$i"
				((i=i+1))	
			fi
			if [ $radio -eq $rval ]
			then 
				echo "$lpart:Yes" 
			else
				echo "$lpart:"
			fi
			((radio=radio+1))
		elif [ -n "$lpart" ]
		then
			eval x="\$var$i"
			if [ -z "$x" ]
			then
				echo "$lpart:"
			else
				echo "$lpart:$x"
			fi
			# fix 5005399, give error message if both hour and minute set to zero
			tmphour=`echo $lpart | grep -i hour`
			tmpminute=`echo $lpart | grep -i minute`
			if [ -n "$tmphour" ]
			then
				hour=$x
			elif [ -n "$tmpminute" ]
			then
				minute=$x
			fi

			((i=i+1))
			if [ $radio -ne 0 ]
			then
				radio=0
			fi
		fi
	done > $rdir/$hname/$tfile 2>/dev/null

	# fix 5005399, give error message if both hour and minute set to zero
	if [ $minute -eq 0 ] && [ $hour -eq 0 ]
	then
		go_fun bhtml $hname $0
		go_fun err_pg "Run Time Hour(s) and Run Time Minute(s) both cannot be set to zero at the same time."
		go_fun ehtml
		exit 1
	fi

	mv $rdir/$hname/$tfile $ffile
	exec $jpage $jargs
fi
