#!/bin/ksh
#
#ident	"@(#)menu10	1.43	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Original Author: Michael Koller
# Modified By: Alex Kaplan
# Modified By: Bill Yan
#
# This script sets up the web page for starting
# the certification test run.
# 

# 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
prgdir=prgs			# program directory
i=0				# loop counter
dls=				# data list
cfile=DVT.cmd			# dvt form command file

tmpf=.form.tmp			# temp form
hour=				# run time hour
minute=				# run time minute
trigger=.trig.tmp		# flag file for STAF start
stafdir=/opt/SUNWstaf		# STAF directory
status=0			# test status
nfile=nethost.dat  		# network client file
nets=				# network client hostname
dname=				# display system
option=$2
technology=$3
usbdiskfile=/tmp/usbdisk.dat

sysdir=/opt/SUNWstaf/prgs/si
probe_file=/tmp/probe_cd_flp	#To store cdrom and floppy device info.
cdromflg=0
flpflg=0

lvfile=/tmp/level.dat
stfile=/tmp/suite.dat
ctagfile=/tmp/ctag.dat  # add by Matthew to identify standard or custom test
csfile=/tmp/clientstatus.dat #clients listed in it should be displaied in status bar

i6file=/tmp/ipv6.dat	#if this file exists, then means ipv6 test enabled. by bill

if_no=		#number of port on network card for multi port certification
mul_s_1=	#hostname for first port 
mul_s_2=
mul_s_3=
mul_s_4=
mul_c_1=	#first port client hostname 
mul_c_2=
mul_c_3=
mul_c_4=
multiportfile=/tmp/multiportfile # for CC multiport certification #yxy

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

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

tmpdir=/tmp		# test directory

# 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


# Run Stress Test

if [[ -z "$act" && ($technology = network || $suite = SC) ]]
then	
		go_fun bhtml $hname $0
		echo '<CENTER><FONT SIZE="+2">Certification Stress Tests Screen</FONT></CENTER>'
		rm -f $multiportfile >/dev/null 2>&1
		$sysdir/nic_check.sh
		check_code=$?

		if [ "$check_code" = 11 ]
		then
			echo '<FORM ACTION="'$gotocgi'" METHOD="POST" onSubmit="return checkHostname(this.nets.value);">'
			echo "<P>Enter Network Client Hostname:"
			if [ -s $tmpdir/$nfile ]
			then
				cat $tmpdir/$nfile | read nets
			fi
			echo '<INPUT NAME="nets" VALUE='$nets'>'

			if [ $suite = SC ]
			then
				echo "<br>"
				echo "<br>"
				echo '<input type="checkbox" name="ipv6" value="ipv6" > Enable IPv6 Certification<br><br>'
			fi

			echo "<br>The page will then change to the <i>Review Test Status Page</i> where you can view the"
			echo "associated reports and logs by using the <I>View File</I> button."
			echo "A shell window will also appear that displays realtime test module start up status.<HR>"
			echo '<P><INPUT TYPE="submit" VALUE="Begin Certification">'
			echo '<INPUT TYPE="hidden" NAME="act" VALUE="collect">'
			echo '<INPUT TYPE="hidden" NAME="hostn" VALUE="'$hname'">'
			echo '<INPUT TYPE="hidden" NAME="technology" VALUE="'$technology'">'
			echo '<INPUT TYPE="hidden" NAME="option" VALUE="'$option'">'
			if [ $suite = SC ]
			then
				echo '<BR><P>Note: HCTS requires about two minutes to complete the hardware check before starting the Certification. Please wait.'
			fi
			echo "</FORM>"
			go_fun ehtml
		elif [ "$check_code" = 12 ]
		then
			echo '<FORM ACTION="'$gotocgi'" METHOD="POST" onSubmit="return checkHostnames(new Array(this.mul_s_1.value, this.mul_c_1.value, this.mul_s_2.value, this.mul_c_2.value));">'
			echo "<P>Dual-Port network controller detected. Please enter the following information :"
			echo "<P>"
			echo "Port one information:"
			echo "<br>"
			echo "<br>"
			echo "&nbsp&nbsp hostname :&nbsp&nbsp&nbsp"
			echo '<INPUT NAME="mul_s_1" VALUE=''>'
			echo "<br>"
			echo "&nbsp&nbsp client name :"
			echo '<INPUT NAME="mul_c_1" VALUE=''>'
			echo "<br>"
			echo "<br>"
			echo "Port two information:"
			echo "<br>"
			echo "<br>"
			echo "&nbsp&nbsp hostname :&nbsp&nbsp&nbsp"
			echo '<INPUT NAME="mul_s_2" VALUE=''>'
			echo "<br>"
			echo "&nbsp&nbsp client name :"
			echo '<INPUT NAME="mul_c_2" VALUE=''>'

			if [ $suite = SC ]
			then
				echo "<br>"
				echo "<br>"
				echo '<input type="checkbox" name="ipv6" value="ipv6" > Enable IPv6 Certification<br><br>'
			fi

			echo "<P><P><br>The page will then change to the <i>Review Test Status Page</i> where you can view the"
			echo "associated reports and logs by using the <I>View File</I> button."
			echo "A shell window will also appear that displays realtime test module start up status.<HR>"
			echo '<P><INPUT TYPE="submit" VALUE="Begin Certification">'
			echo '<INPUT TYPE="hidden" NAME="act" VALUE="collect">'
			echo '<INPUT TYPE="hidden" NAME="hostn" VALUE="'$hname'">'
			echo '<INPUT TYPE="hidden" NAME="technology" VALUE="'$technology'">'
			echo '<INPUT TYPE="hidden" NAME="option" VALUE="'$option'">'
			echo '<INPUT TYPE="hidden" NAME="if_no" VALUE="'2'">'
			echo "</FORM>"
			go_fun ehtml
		elif [ "$check_code" = 14 ]
		then
			echo '<FORM ACTION="'$gotocgi'" METHOD="POST" onSubmit="return checkHostnames(new Array(this.mul_s_1.value, this.mul_c_1.value, this.mul_s_2.value, this.mul_c_2.value, this.mul_s_3.value, this.mul_c_3.value, this.mul_s_4.value, this.mul_c_4.value));">'
			echo "<P>Quad-Port network controller detected. Please enter the following information :"
			echo "<P>"
			echo "Port one information:"
			echo "<br>"
			echo "<br>"
			echo "&nbsp&nbsp hostname :&nbsp&nbsp&nbsp"
			echo '<INPUT NAME="mul_s_1" VALUE=''>'
			echo "<br>"
			echo "&nbsp&nbsp client name :"
			echo '<INPUT NAME="mul_c_1" VALUE=''>'
			echo "<br>"
			echo "<br>"
			echo "Port two information:"
			echo "<br>"
			echo "<br>"
			echo "&nbsp&nbsp hostname :&nbsp&nbsp&nbsp"
			echo '<INPUT NAME="mul_s_2" VALUE=''>'
			echo "<br>"
			echo "&nbsp&nbsp client name :"
			echo '<INPUT NAME="mul_c_2" VALUE=''>'
			echo "<br>"
			echo "<br>"
			echo "Port three information:"
			echo "<br>"
			echo "<br>"
			echo "&nbsp&nbsp hostname :&nbsp&nbsp&nbsp"
			echo '<INPUT NAME="mul_s_3" VALUE=''>'
			echo "<br>"
			echo "&nbsp&nbsp client name :"
			echo '<INPUT NAME="mul_c_3" VALUE=''>'
			echo "<br>"
			echo "<br>"
			echo "Port four information:"
			echo "<br>"
			echo "<br>"
			echo "&nbsp&nbsp hostname :&nbsp&nbsp&nbsp"
			echo '<INPUT NAME="mul_s_4" VALUE=''>'
			echo "<br>"
			echo "&nbsp&nbsp client name :"
			echo '<INPUT NAME="mul_c_4" VALUE=''>'
			if [ $suite = SC ]
			then
				echo "<br>"
				echo "<br>"
				echo '<input type="checkbox" name="ipv6" value="ipv6" > Enable IPv6 Certification<br><br>'
			fi
			echo "<P><P><br>The page will then change to the <i>Review Test Status Page</i> where you can view the"
			echo "associated reports and logs by using the <I>View File</I> button."
			echo "A shell window will also appear that displays realtime test module start up status.<HR>"
			echo '<P><INPUT TYPE="submit" VALUE="Begin Certification">'
			echo '<INPUT TYPE="hidden" NAME="act" VALUE="collect">'
			echo '<INPUT TYPE="hidden" NAME="hostn" VALUE="'$hname'">'
			echo '<INPUT TYPE="hidden" NAME="technology" VALUE="'$technology'">'
			echo '<INPUT TYPE="hidden" NAME="option" VALUE="'$option'">'
			echo '<INPUT TYPE="hidden" NAME="if_no" VALUE="'4'">'
			echo "</FORM>"
			go_fun ehtml
		else
			if [ "$check_code" = 10 ]
			then
				go_fun err_pg "!ERROR: No network interface detected. Network controller certification requires at least one network interface."
				go_fun ehtml
			fi
			if [ "$check_code" = 99 ]
			then
				go_fun err_pg "!ERROR: Logical or virtual interfaces are detected or multiple network cards are detected. Unplumb all logical and virtual interfaces and all network cards except for the one network card under test."
				go_fun ehtml
			fi
		fi

	exit 0
fi

if [[ -z "$act" && ($option = usbdisk && $technology = storage && $suite = CC) ]]
then
                go_fun bhtml $hname $0
                echo '<CENTER><FONT SIZE="+2">Certification Stress Tests Screen</FONT></CENTER>'
                echo '<FORM ACTION="'$gotocgi'" METHOD="POST">'

                echo "<P>Enter size of USB disk drive:<br>"
                device=""
                size=""
                if [ -s $usbdiskfile ]
                then
                    while read i
                    do
                        #if [ `expr match $i $keyworda` -eq ${#keyworda} ]
                        echo $i | grep "device" >/dev/null
                        if [ $? -eq 0 ]
                        then
                            device=`echo $i | sed 's/^device : //g'`
                        fi
                        #if [ `expr match $i $keywordb` -eq ${#keywordb} ]
                        echo $i | grep "size" >/dev/null
                        if [ $? -eq 0 ]
                        then
                            size=`echo $i | sed 's/^size : //g'`
                        fi
                    done <$usbdiskfile
                fi
                for j in `$stafdir/$prgdir/sliceinfo | awk '{print $1}' | sed 's/..$/p0/g' | sed 's/\/.*\/.*\///g' | sort | uniq `
                do
                    echo "<B>$j</B>"
                    echo "<INPUT TYPE=\"hidden\" NAME=\"device\" VALUE=\"$j\"><br>"
                done
                echo '<br><INPUT TYPE="text" NAME="size" VALUE='$size'>'
                echo '(For example, 60K, 500M, 40G)<br>'

                        echo "<br>The page will then change to the <i>Review Test Status Page</i> where you can view the"
                        echo "associated reports and logs by using the <I>View File</I> button."
                        echo "A shell window will also appear that displays realtime test module start up status.<HR>"
                        echo '<P><INPUT TYPE="submit" VALUE="Begin Certification">'
                        echo '<INPUT TYPE="hidden" NAME="act" VALUE="collect">'
                        echo '<INPUT TYPE="hidden" NAME="hostn" VALUE="'$hname'">'
                        echo '<INPUT TYPE="hidden" NAME="technology" VALUE="'$technology'">'
                        echo '<INPUT TYPE="hidden" NAME="option" VALUE="'$option'">'
                echo "</FORM>"
                go_fun ehtml

        exit 0
fi

# COLLECT TESTS
###############
# This is where you come after hitting the "Begin Certification" button.
# We will build the command file.
#

if [ $suite = SC ]
then
	if [ -e $i6file ]
	then
		rm $i6file > $tmpdir/errors 2>&1
		if [ $? -ne 0 ]; then
			go_fun bhtml $hname $0
			go_fun err_pg
			cat $tmpdir/errors
			rm $tmpdir/errors
			go_fun ehtml
			exit 1
		fi
	fi

	if [ ! -z $ipv6 ]
	then
		if [ $ipv6 = ipv6 ]
		then
			touch $i6file
		fi
	fi
fi


if [ $act = collect ] || [ $technology = storage ] || [ $technology = serial ]
then

	if [ -n "$hostn" ]
	then
		hname=$hostn
	fi
	
	# Check $cfile and $nets
	rm -f $rdir/$hname/$cfile >/dev/null 2>&1
	
        if [[ -n "$nets" && ($technology = network || $suite = SC) ]]
        then
                echo "$nets" > $csfile
        fi

	if [[ -n "$if_no" && ($technology = network || $suite = SC) ]]
	then
		if [ $if_no = 2 ]
		then
			if [[ -z "$mul_s_1" || -z "$mul_c_1" || -z "$mul_s_2" || -z "$mul_c_2" ]]
			then
				go_fun bhtml $hname $0
				go_fun err_pg "Some required fields have no data. Go back and fill out all the fields."
				go_fun ehtml
				exit 1
			else
				rm -f $multiportfile >/dev/null 2>&1
				nets=`echo "$mul_c_1"`
				echo "$mul_s_1;$mul_c_1" >> $multiportfile
				echo "$mul_s_2;$mul_c_2" >> $multiportfile
				echo "$mul_c_1;$mul_c_2" > $csfile
			fi
		fi
		if [ $if_no = 4 ]
		then
			if [[ -z "$mul_s_1" || -z "$mul_c_1" || -z "$mul_s_2" || -z "$mul_c_2" || -z "$mul_s_3" || -z "$mul_c_3" || -z "$mul_s_4" || -z "$mul_c_4" ]]
			then
				go_fun bhtml $hname $0
				go_fun err_pg "Some required fields have no data. Go back and fill out all the fields."
				go_fun ehtml
				exit 1
			else
				rm -f $multiportfile >/dev/null 2>&1
				nets=`echo "$mul_c_1"`
				echo "$mul_s_1;$mul_c_1" >> $multiportfile
				echo "$mul_s_2;$mul_c_2" >> $multiportfile
				echo "$mul_s_3;$mul_c_3" >> $multiportfile
				echo "$mul_s_4;$mul_c_4" >> $multiportfile
				echo "$mul_c_1;$mul_c_2;$mul_c_3;$mul_c_4" > $csfile
			fi
		fi

	fi

	if [[ -n "$nets" && ($technology = network || $suite = SC) ]]
	then
		echo "$nets" > $tmpdir/$nfile
		#added for checking the validity of network connection.
		err=`/usr/sbin/ping $nets 2>&1`
		if [ $? -ne 0 ]
		then
			go_fun bhtml $hname $0
                	go_fun err_pg "!ERROR: Remote machine $nets is unreachable."
			go_fun ehtml
                	exit 1
		fi
		#end added

	elif [[ -z "$nets" && ($technology = network || $suite = SC) ]]
	then
		go_fun bhtml $hname $0
		go_fun err_pg "Enter a Network Client Hostname."
		go_fun ehtml
		exit 1
	fi

        if [[ $suite = CC && $technology = storage && $option = usbdisk && -n "$device" && -n "$size" ]]
        then
            echo $size | grep "^[0-9][0-9]*\.[0-9][0-9]*[GMKB]$" > /dev/null
            result1=$?
            echo $size | grep "^[0-9][0-9]*[GMKB]$" > /dev/null
            result2=$?

            if [[ ${result1} -eq 0 || ${result2} -eq 0 ]]
            then
                echo "device : $device" >$usbdiskfile
                echo "size : $size" >> $usbdiskfile
            else
                go_fun bhtml $hname $0
                go_fun err_pg "The disk size entered is invalid."
                go_fun ehtml
                exit 1
            fi
        elif [[ $option = usbdisk && $technology = storage && $suite = CC ]]
        then
                go_fun bhtml $hname $0
                go_fun err_pg "Enter a USB hard drive size."
                go_fun ehtml
                exit 1
        fi

# added by phost, autocheck whether the cd is in the cdrom drive, only for scsi at Level 2
	if [[ $option = scsi && $technology = storage && $suite = CC && $level = L2 ]]
	then
        	$sysdir/probe_dev.sh

        	#detect if the media are there.
		if [ -f $probe_file ]
		then
			if [ -n "`grep cdrom $probe_file`" ]
			then
				if [ -z "`ps -e | grep vold`" ]
				then
					cdtrg=`cat /etc/mnttab | grep "/cdrom/" | awk '{print $2}' | sort +1n | sed -n '1p'`
				else
					volcheck
					cdtrg=`df -kl | grep "/cdrom/" | awk '{print $6}' | sort +1n | sed -n '1p'`
				fi
				if [ -z "$cdtrg" ]
				then
					#No CDROM found
					go_fun bhtml $hname $0
					go_fun err_pg  "!ERROR: CD not found. Make sure a CD disc is inserted into the CD-ROM drive."
                			go_fun ehtml
					exit 1
				fi
			fi
		else
			go_fun bhtml $hname $0
			go_fun err_pg  "!ERROR: $probe_file is missing."
                	go_fun ehtml
			exit 1
		fi
	fi
#
#end

#bill add code here to auto check cdrom and floppy drive for SC test before certification carries on
#start
if [ $suite = SC -a $option = system ]
then
        if [ -n "$hostn" ]
        then
                hname=$hostn
        fi

        rsh -l root $hname $sysdir/probe_dev.sh

        #detect if the media are there.
	probe_file_flg=0
	if [ -f $probe_file ]
	then
		if [ -n "`grep cdrom $probe_file`" ]
		then
			if [ -z "`ps -e | grep vold`" ]
			then
				cdtrg=`cat /etc/mnttab | grep "/cdrom/" | awk '{print $2}' | sort +1n | sed -n '1p'`
			else
				volcheck
				cdtrg=`df -kl | grep "/cdrom/" | awk '{print $6}' | sort +1n | sed -n '1p'`
			fi
			if [ -z "$cdtrg" ]
			then
				#No CDROM found
				cdromflg=1
			fi
		fi
		if [ -n "`grep floppy $probe_file`" ]
		then
			fd_err=`fdformat -Ufqv 2>&1`' '
			diskin=` echo $fd_err | awk ' /^No such volume \(or no media/ { print "no-media" }
			/^fdformat: no diskette/        { print "no-media"}' `

			if [ "${diskin}" = "no-media" ]
			then
				#No Floppy Media Inserted
				flpflg=1
			fi
			if [ -n "`echo $fd_err | grep 'I/O error'`" ]
			then
				#Floppy is damaged
				flpflg=2
			fi
			if [ -n "`echo $fd_err | grep 'write protected'`" ]
			then
				#Floppy is write protected
				flpflg=3
			fi
			if [ -n "`echo $fd_err | grep 'Permission denied'`" ]
			then
				#Floppy is write protected
				flpflg=3
			fi

		fi
	else
		probe_file_flg=1
	fi
	if [ $cdromflg -gt 0 ] || [ $flpflg -gt 0 ] || [ $probe_file_flg -gt 0 ]
	then
		go_fun bhtml $hname $0

		if [ $cdromflg -gt 0 ]
		then
			go_fun err_pg  "!ERROR: CD not found. Make sure a CD disc is inserted into the CD-ROM drive."
		fi
		if [ $flpflg = 1 ]
		 then
			go_fun err_pg  "!ERROR: Floppy disk not found. Make sure a floppy disk is inserted into the floppy drive."
		fi
		if [ $flpflg = 2 ]
		then
			go_fun err_pg  "!ERROR: Floppy disk is damaged, Replace the floppy disk with another floppy disk."
		fi
		if [ $flpflg = 3 ]
		then
			go_fun err_pg  "!ERROR: Floppy disk is write protected. Make the floppy disk writable."
		fi
		if [ $probe_file_flg = 1 ]
		then
			go_fun err_pg  "!ERROR: $probe_file is missing."
		fi
                go_fun ehtml
		exit 1
	fi
fi
#
#end

#
# Next, we build a command file for the staf test engine
# telling the engine which test modules to run.
# In the /opt/SUNWhcts/tests lookup file is parsed by
# /opt/SUNWhcts/prgs/lookup.sh script. This script builds command file
# depending on different options supplied to it.
#

	$prgdir/lookup.sh $hname $option $technology > /tmp/saip_errors
	if [ $? -eq 78 ]
	then 
		go_fun bhtml $hname $0
		go_fun err_pg 
		tail -1 /tmp/saip_errors
		go_fun ehtml
		exit 1
	fi
	

#####>>>>>>>>>>>>>>>>>>>>>>>>>  BIG IF THEN ELSE NESTED HEADACHE.
	if [ -a $rdir/$hname/$trigger ]
	then             #Then continue, else bypass all and display log viewing page.
            # Check video write permission
	    if [ "$suite" = SC ]
	    then
	            #DISPLAY=`echo "$hname:0"`
	            #export DISPLAY
		
		    # check whether Xserver has been started
     		    ps -ef | egrep '(\/bin\/X |Xsun|Xorg)' | egrep -v egrep > /dev/null
                    if [ $? -ne 0 ]
     		    then
			go_fun bhtml $hname $0
     		        go_fun err_pg "The system `hostname` is running the wrong environment!<BR>\
     		         		Start Xserver before you run the video tests."
			go_fun ehtml
     		        exit 1
     		    fi

     	  	    # check whether the user has logged on the system to be tested. If the end user start Xsession manually
     	   	    # then some errors appear, but it is the best way until now.
                    ps -ef | egrep '(Xsession)' | egrep -v egrep > /dev/null
     		    if [ $? -ne 0 ]
     		    then
			go_fun bhtml $hname $0
    			go_fun err_pg "The system `hostname` is running the wrong environment!<BR>\
   		        		Start Xwindows and log onto the system before you run the video tests."
			go_fun ehtml
    		        exit 1
		    else
	                usrid=`ps -ef|grep "\<Xsession\>" | grep -v grep | awk '{print $1}'`
		        if [ ! "$usrid" = "root"  ]
	    	    	then  # print video perms error and done.
				go_fun bhtml $hname $0
				go_fun err_pg "Log in as root on the system `hostname`.\
						You must be user root to run the HCTS. "
				go_fun ehtml
				exit 1
	    	        fi
 		    fi
		fi


		if [ -a $stafdir/staf.eng ]
		then   #Then continue, else issue error msg and abort.
			stafid=`ps -e | grep staf.eng`
			if [ -a $stafdir/reports/$hname/staf.rpt ] || [ -n "$stafid" ]
			then   #Then issue error msg, else continue with running HCTS.

				exec $mdir/menu13 $hname
			else					
				rm -f $rdir/$hname/$trigger
				echo $cfile | $prgdir/tap $hname 777 >/dev/null 2>&1	
				sleep 5
				if [ -z "`ls $stafdir/reports/$hname`" ]
				then
					go_fun bhtml $hname $0
					go_fun err_pg "STAF did not start.  Make 
					sure STAF is installed properly. <BR>If need 
					be, run /opt/SUNWstaf/staf.eng from a command
					line to test.<BR> You will need to provide a 
					sample staf command file on the command line.
					<BR> See the staf.eng script."
					go_fun ehtml
				else
					dname=`cat $rdir/$hname/.dname.dat`
					#/opt/SUNWstaf/prgs/load >/dev/null 2>&1 &
					sleep 15
					cat /dev/null > $ctagfile # add by Matthew to identify standard and custom tests
					exec $mdir/menu13 $hname menu13 $technology
				fi
			fi
		else
			go_fun bhtml $hname $0
			go_fun err_pg "Cannot find STAF engine $stafdir/staf.eng."
			go_fun ehtml
		fi
	else
		exec $mdir/menu13 $hname
	fi
fi

