#!/bin/ksh
#
#ident	"@(#)menu12	1.39	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Original Author: Michael Koller
# Modified by: Terry Hu jh148803@sun
#

# variables
hctsdir=SUNWhcts	# base directory
hname=$1		# test system
rdir=results		# results directory
act=			# action to be taken
mdir=menu		# menu directory
bname=`basename $0`	# shell name
gotocgi="http://$HTTP_HOST/cgi-bin/$hctsdir/$mdir/$bname"	# program name
cgiprg=$mdir/$bname	# cgi name
prgdir=prgs		# program directory
i=0			# loop counter
dls=			# data list
dls2=           # data list field 2
option=			# option selected 
testr=			# test to add
cmdf=			# command form file
cmdc=			# command file
dval=			# data value
flag=0			# flag var
lpart=			# label part
fpart=			# field part
tmpf=.form.tmp		# temp command form
text=                   # command file text
trigger=.trig12.tmp	# flag file for STAF start
stafdir=/opt/SUNWstaf	# STAF directory
stafcdir=$stafdir/cmd-files	# STAF command file directory
dname=			# display system
technology=
opt=$2
#added by Erica Zhang on Feb 6, 2004
rhost=                        # the ppp_client name

lvfile=/tmp/level.dat
stfile=/tmp/suite.dat
ctagfile=/tmp/ctag.dat  # add by Matthew to identify standard or custom test

i6file=/tmp/ipv6.dat    #if this file exists, then means ipv6 test enabled. by bill
l6file=/opt/SUNWhcts/tests/lookup.net6          #Lookup file for net6
tmpcmdf=/tmp/tmpcmdf.$$				#temp file
tmpcmdf1=/tmp/tmpcmdf1.$$			#temp file
line_curr=0					#temp variable for cmd file
line_find=0					#temp variable for cmd file
net6setf="net6set"				#script for setting up IPv6
custcase="CUSTCASE"				#command file name for ip6 

probe_file=/tmp/probe_cd_flp
multiportfile=/tmp/multiportfile

if [ -s $stfile ]
then
    suite=`cat $stfile`
fi

if [ -s $lvfile ]
then
    level=`cat $lvfile`
fi

tdir=tests/$suite/$level		# test directory
tfile=$tdir/test.lst			# test list
tfile_6=$tdir/test_6.lst		# test list for IPv6
tfile_mb=$tdir/test_mb.lst		# test list for motherboard certification 

# 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

# strip form
if [ "$2" = "strip" ]
then
	$prgdir/strip.eng $hname $4 $5
	if [ $? -ne 0 ]
	then
		rm -f $rdir/$hname/$tmpf
		exit 1
	fi
    if [ -n $6 ]; then
        opt=$6
    else
        opt=''
    fi
	rm -f $rdir/$hname/$tmpf
fi

# Run Stress Test
if [ -z "$act" ]
then
	technology=$3
		
	go_fun bhtml $hname $0 $technology
	echo '<CENTER><FONT SIZE="+2"><U>Custom Test Case Generation</U></FONT></CENTER>'
	echo '<P>This page is designed to help advanced users to test their driver '
	echo 'by allowing them to create custom test cases. These test cases will <b>NOT</b> '
	echo 'allow users to go through the Hardware Certification Program, but will provide them tools '
	echo 'to do more testing on their driver. '
	echo '<FORM ACTION="'$gotocgi'" METHOD="POST">'
		# list tests
		if [ -a $tfile ]
		then
		echo "<P><B><U>Select test to add to Command Form:</U></B>"
		echo '<P><SELECT NAME="testr">'
			
		if [ $technology = storage ]
		then
			#cat $tfile | grep -v "^#" | grep -v "testn" | while read inline
			cat $tfile | grep -v "^#" | grep -v "testn" | grep -v "testio"| while read inline
			do
                                testcase=`echo "$inline" | cut -f3 -d':'`
				dls=`echo "$inline" | cut -f2 -d':'`
				inline=`echo "$inline" | cut -f1 -d':'`
                                echo "$testcase" | grep $opt >/dev/null
                                if [[ -z $testcase || $? -eq 0 ]] && [ -n "$dls" ] && [ -n "$inline" ]
                                then
				    echo '<OPTION VALUE="'$dls'">'$inline''
                                fi
			done

		elif [ $technology = network ]
		then
			if [ -f $multiportfile ]
			then
				rsh -l root $hname rm -f $multiportfile
			fi
			if [ -e $i6file ]
			then
				cat $tfile $tfile_6 | grep -v "^#" | grep "testn" | while read inline
				do
					dls=`echo "$inline" | cut -f2 -d':'`
					inline=`echo "$inline" | cut -f1 -d':'`
				    if [ -n "$dls" ] && [ -n "$inline" ]
				    then
				       echo '<OPTION VALUE="'$dls'">'$inline''
				    fi
				done
			else
				cat $tfile | grep -v "^#" | grep "testn" | while read inline
				do
					dls=`echo "$inline" | cut -f2 -d':'`
					inline=`echo "$inline" | cut -f1 -d':'`
				    if [ -n "$dls" ] && [ -n "$inline" ]
				    then
				       echo '<OPTION VALUE="'$dls'">'$inline''
				    fi
				done
			fi
		elif [ $technology = serial ]
		then
		      cat $tfile | grep -v "^#" | grep -v "tests" | grep -v "testn"| while read inline
		      do
			       dls=`echo "$inline" | cut -f2 -d':'`
			       inline=`echo "$inline" | cut -f1 -d':'`
				   if [ -n "$dls" ] && [ -n "$inline" ]
				   then
				       echo '<OPTION VALUE="'$dls'">'$inline''
				   fi
		      done
		else
			#modified for motherboard certification 
		 	if [ $technology = "system" ];then
				if [ -f $probe_file ]
				then
					rsh -l root $hname rm -f $probe_file
				fi
				#cat $tfile | grep -v "^#" | while read inline
				cat $tfile | grep -v "^#" | grep -v "testio" | while read inline
				do
					dls=`echo "$inline" | cut -f2 -d':'`
					inline=`echo "$inline" | cut -f1 -d':'`
					if [ -n "$dls" ] && [ -n "$inline" ]
					then
				    		echo '<OPTION VALUE="'$dls'">'$inline''
					fi
				done
			elif [ $technology = "motherboard" ];then
				#cat $tfile | grep -v "^#" | while read inline
				cat $tfile_mb | grep -v "^#" | grep -v "testio" | while read inline
				do
					dls=`echo "$inline" | cut -f2 -d':'`
					inline=`echo "$inline" | cut -f1 -d':'`
					if [ -n "$dls" ] && [ -n "$inline" ]
					then
				    		echo '<OPTION VALUE="'$dls'">'$inline''
					fi
				done
			fi
			#end add 
		fi
				
		echo "</SELECT>"
		echo '<P><INPUT TYPE="radio" NAME="option" VALUE="create" CHECKED> CREATE'
		echo ' Command Form filename: <INPUT NAME="dls">'
	fi
	# list command forms
	dls=`cd $rdir/$hname; ls *.cfm 2>/dev/null`
	
	if [ -n "$dls" ]
	then
       		echo "<P><B><U>Select Command Form to Process:</U></B>"
		echo '<P><SELECT NAME="cmdf">'
		                	
                for i in `echo "$dls"`
		do
			i=`echo "$i" | cut -f1 -d'.'`
			echo "<OPTION>$i"
		done
		
	      	echo "</SELECT>"
		echo '<INPUT TYPE="radio" NAME="option" VALUE="add"> ADD'                	
		echo '<INPUT TYPE="radio" NAME="option" VALUE="edit"> EDIT'
		echo '<INPUT TYPE="radio" NAME="option" VALUE="delete"> DELETE'
		echo "<P><B><U>Select Command File Function:</U></B>"
		echo '<P><SELECT NAME="cmdc">'
		
                for i in `cd $stafcdir/$hname; ls *.cmd | grep -v DVT.cmd | grep -v $custcase.cmd`
		do
			i=`echo "$i" | cut -f1 -d'.'`
			echo "<OPTION>$i"
		done
		
	      	echo "</SELECT>"
		echo '<INPUT TYPE="radio" NAME="option" VALUE="tedit"> EDIT'
		echo '<INPUT TYPE="radio" NAME="option" VALUE="execute"> EXECUTE'
	fi
	echo '<P><INPUT TYPE="submit" VALUE="Continue">'
	echo '<INPUT TYPE="hidden" NAME="act" VALUE="process">'
	echo '<INPUT TYPE="hidden" NAME="hname" VALUE="'$hname'">'
	echo '<INPUT TYPE="hidden" NAME="tech" VALUE="'$technology'">'
	echo '<INPUT TYPE="hidden" NAME="opt" VALUE="'$opt'">'
	echo "</FORM>"
	go_fun ehtml
	touch $rdir/$hname/$trigger
	exit 0
fi

# command form processing
if [ $act = process ]
then

	technology=$tech

	if [ $option = create ]
	then
		dls2=`echo $dls | awk '{print $2}'`
		dls=`echo $dls | awk '{print $1}' | grep -i [0-9,a-z]`
		if [ -n "$dls" ] && [ -z "$dls2" ]
		then
			rm -f $rdir/$hname/$dls.cfm
			cmdf=$dls
			option=add
		else
			go_fun bhtml $hname $0 $technology
			go_fun err_pg "<font color='ff0000'>Enter a CREATE Command Form filename.</font> <br>  The filename must contain at least one letter or one digit and cannot contain any spaces."
			go_fun ehtml
			exit 0
		fi
	fi
	if [ $option = add ]
	then
		#go_fun bhtml $hname $0 $technology
		#text=`$tdir/$testr $hname`
		#if [ $? -eq 0 ]
		#then
		#	echo "$text" > $rdir/$hname/$tmpf
		#	exec $prgdir/form.eng $hname $rdir/$hname/$tmpf $cgiprg "$hname strip $technology $rdir/$hname/$tmpf $rdir/$hname/$cmdf.cfm"
		#else
		#	go_fun err_pg "$text"
		#	go_fun ehtml
		#fi
		if [ $technology = serial ]
		then
			rhost = `cat /tmp/pclient`

			go_fun bhtml $hname $0 $technology
			text=`$tdir/$testr $rhost`

			if [ $? -eq 0 ]
			then
				echo "$text" > $rdir/$hname/$tmpf
				exec $prgdir/form.eng $hname $rdir/$hname/$tmpf $cgiprg "$hname strip $technology $rdir/$hname/$tmpf $rdir/$hname/$cmdf.cfm"
			else
				go_fun err_pg "$text"
				go_fun ehtml
			fi		
		else
			go_fun bhtml $hname $0 $technology
			text=`$tdir/$testr $hname`
			if [ $? -eq 0 ]
			then
				echo "$text" > $rdir/$hname/$tmpf
				exec $prgdir/form.eng $hname $rdir/$hname/$tmpf $cgiprg "$hname strip $technology $rdir/$hname/$tmpf $rdir/$hname/$cmdf.cfm $opt"
			else
				go_fun err_pg "$text"
				go_fun ehtml
			fi
		fi
	fi
	if [ $option = delete ]
	then
		rm -f $rdir/$hname/$cmdf.cfm $stafcdir/$hname/$cmdf.cmd
                if [ $technology = storage ]; then
		    exec $cgiprg $hname $opt $technology
                else
		    exec $cgiprg $hname $option $technology
                fi
	fi
	if [ $option = edit ]
	then
		go_fun bhtml $hname $0 $technology
		$prgdir/form.eng $hname $rdir/$hname/$cmdf.cfm $cgiprg "$hname strip $technology $rdir/$hname/$cmdf.cfm $rdir/$hname/$cmdf.cfm $opt"
	fi
	if [ $option = tedit ]
	then		
        	go_fun bhtml $hname $0 $technology
        	if [ -a $stafcdir/$hname/$cmdc.cmd ]
        	then
        		echo '<CENTER><FONT SIZE="+2"><U>Edit '$cmdc' Command File:</U></FONT></CENTER>'
			echo '<FORM ACTION="'$gotocgi'" METHOD="POST">'
                		echo '<TEXTAREA NAME="text" ROWS="10" COLS="100" WRAP="physical">'
                			cat $stafcdir/$hname/$cmdc.cmd
				echo "</TEXTAREA>"
                		echo '<P><CENTER><INPUT TYPE="submit" VALUE="SAVE Command File"></CENTER>'
                		echo '<INPUT TYPE="hidden" NAME="dval" VALUE="'$cmdc.cmd'">'
                		echo '<INPUT TYPE="hidden" NAME="act" VALUE="save">'
                		echo '<INPUT TYPE="hidden" NAME="hname" VALUE="'$hname'">'
                		echo '<INPUT TYPE="hidden" NAME="option" VALUE="option">'
                		echo '<INPUT TYPE="hidden" NAME="opt" VALUE="'$opt'">'
                		echo '<INPUT TYPE="hidden" NAME="tech" VALUE="'$technology'">'
        		echo "</FORM>"
        	else
        		go_fun err_pg "Command File $cmdc not found."	
        	fi
        	go_fun ehtml
	fi
	if [ $option = execute ]
	then
		if [ -a $rdir/$hname/$trigger ]
		then
			if [ -a $stafdir/staf.eng ]
			then
				stafid=`ps -e | grep staf.eng`
				if [ -a $stafdir/reports/$hname/staf.rpt ] || [ -n "$stafid" ]
				then
					go_fun bhtml $hname $0 $technology
					go_fun err_pg "STAF already running on $hname."
					go_fun ehtml
				else					
					rm -f $rdir/$hname/$trigger
					#########
					#modified by bill for i6 custom case begin
					########
					if [[ ($technology = network) && -e $i6file ]] 
					then
						cat $stafcdir/$hname/$cmdc.cmd | while read inline
						do
							case `echo "$inline" | cut -b1` in
							[0-9])		
								tmptestname=`echo "$inline" | cut -f3 -d' '`
								if [ ! -z "$tmptestname" ]
								then
									tmptests=`grep $tmptestname $l6file | cut -f8 -d',' | sort | uniq`
									if [ -z "$tmptests" ]
									then
										echo $inline >> $tmpcmdf
									else
										echo "$tmptests" | while read inline_sub
										do
											if [ ! -z $inline_sub ]
											then
												echo `echo $inline | cut \
										         	-d' ' -f1,2`' '$inline_sub >> $tmpcmdf
											fi
										done	
										#find out the remote machine for IPv6 setup
										if [ -z "$rmthost" ]
										then
											if [ ! -z `echo $inline | cut -f4 -d' '` ]
											then
												case `echo $inline | cut -f4 -d' ' \
												 | cut -b1 ` in
												[0-9])
												;;
												/)
													if [ ! -z `echo $inline | \
														cut -f5 -d' ' | \
														cut -f1 -d':'` ]
													then
														rmthost=`echo $inline | \
														cut -f5 -d' ' | \
														cut -f1 -d':'`
													fi
												;;
												*)
													rmthost=`echo $inline | cut -f4 -d' '`	
												;;
												esac
											fi
										fi
									fi
								fi	
							;;
							*)
								echo $inline >> $tmpcmdf
							;;
							esac
						done	
						cmdc=$custcase
						if [ ! -z `grep -v ^# $tmpcmdf | /usr/xpg4/bin/grep -E 'net6|CON5_net_6'` ]
						then
							line_curr=0
							line_find=0
							rm -f $tmpcmdf1
							cat $tmpcmdf | while read inline
							do
								line_curr=`expr $line_curr + 1`
								case `echo "$inline" | cut -b1` in
								[0-9])
									if [ ! -z `echo "$inline" | /usr/xpg4/bin/grep -E 'net6|CON5_net_6'` ]
									then
										line_find=`expr $line_curr + 1`
										if [ `head -$line_find $tmpcmdf | tail -1 | cut -b1` != 'L' ]
										then
											line_find=`expr $line_find + 1`
											if [ `head -$line_find $tmpcmdf | tail -1 | cut -b1` = 'L' ]
											then
												echo $inline >> $tmpcmdf1
												head -$line_find $tmpcmdf | tail -1 >> $tmpcmdf1
											else
												echo $inline >> $tmpcmdf1
												echo "LOG=false" >> $tmpcmdf1
											fi
										else
											echo $inline >> $tmpcmdf1
										fi
									else
										echo $inline >> $tmpcmdf1
									fi
								;;
								*)
									echo $inline >> $tmpcmdf1
								;;
								esac
							done
							mv $tmpcmdf1 $tmpcmdf
							echo '#IPv6 set up' > $stafcdir/$hname/$cmdc.cmd
							grep $net6setf $l6file | head -1 | cut -f4,7,8 -d',' | sed 's/,/ /g' | \
							sed "s/HOSTNAME/$rmthost/g"  >> $stafcdir/$hname/$cmdc.cmd
							echo "LOG=false" >> $stafcdir/$hname/$cmdc.cmd
							echo "wait" >> $stafcdir/$hname/$cmdc.cmd
							cat $tmpcmdf | sed "s/HOSTNAME/$rmthost/g" >> $stafcdir/$hname/$cmdc.cmd
						else
							cat $tmpcmdf | sed "s/HOSTNAME/$rmthost/g" > $stafcdir/$hname/$cmdc.cmd
						fi	
						rm -f $tmpcmdf
					fi
					#########
					#modified by bill for i6 custom case end
					########
					echo $cmdc.cmd | $prgdir/tap $hname 777 >/dev/null 2>&1	
					sleep 5
					if [ -z "`ls $stafdir/reports/$hname`" ]
					then
						go_fun bhtml $hname $0 $technology
						go_fun err_pg "STAF did not start. Make sure STAF is installed properly."
						go_fun ehtml
					else
						dname=`cat $rdir/$hname/.dname.dat`
#						/usr/openwin/bin/cmdtool -display "$dname:0.0" -title "test system = $hname" -Wp 0 200 tail -f $stafdir/reports/$hname/staf.rpt >/dev/null 2>&1 &
						#/opt/SUNWstaf/prgs/load >/dev/null 2>&1 &
						sleep 10
						echo "Custom Test" > $ctagfile # add by Matthew to identify standard and custom tests
						exec $mdir/menu13 $hname
					fi
				fi
			else
				go_fun bhtml $hname $0 $technology
				go_fun err_pg "Cannot find STAF engine $stafdir/staf.eng ."
				go_fun ehtml
			fi
		else
			exec $mdir/menu13 $hname
		fi
	fi
	exit 0
fi

# save text command file
if [ $act = save ]
then
	technology=$tech
	echo "$text" | tr "\r" "\n" | sed -e '/^$/d' > $stafcdir/$hname/$dval
        if [ $technology = storage ]; then
            exec $cgiprg $hname $opt $technology
        else
            exec $cgiprg $hname $option $technology
        fi
fi
