#!/bin/ksh
#
#ident	"@(#)funcs	1.57	05/04/30 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# variables
# Test Suite Version Number:
#	This is the master version number.  Change this by hand.
#	to increment the version of the package.  The master
#	makefile in ../pkg uses this number.  Do not change the
#	format of the line, or the case, name, or spacing around
#	the "version" variable. 
version="2.4.1"
hctsdir=SUNWhcts			# base directory
hour=0				# run time hour
minute=0			# run time minute
numi=1				# number of instances
snumi=1				# set number of instances
gotocgi="http://$HTTP_HOST/cgi-bin/$hctsdir"	# program name
prgdir=prgs			# program directory
hdir=help			# help directory
mdir=menu		        # menu directory
hname=				# hostname
mname=				# help item
technology=                     # type of technology for the driver
option=                         # option selected
client=                         # client hostname
running=                        # test running status
browser=                        # user browser, default is mozilla
runningdisabled=
stopdisabled=
input_style_run=
input_style_stop=
ctag=
console=                # address of user currently has control to HCTS server

lvfile=/tmp/level.dat
stfile=/tmp/suite.dat
nfile=/tmp/nethost.dat  	# network client file
rtfile=/tmp/runtime.dat         # file contains current run time
tcfile=/tmp/technology.dat      # technology file
opfile=/tmp/option.dat          # option file
ctagfile=/tmp/ctag.dat  # add by Matthew to identify standard or custom test
console_tag=/tmp/hctsconsole.dat     # log the address of user currently has control to HCTS server
csfile=/tmp/clientstatus.dat	#clients listed in it will be displied in status bar

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

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

if [ -s $rtfile ]
then
    run_time=`cat $rtfile`
fi

if [ -s $tcfile ]
then
    technology=`cat $tcfile`
fi

if [ -s $opfile ]
then
    option=`cat $opfile`
fi

if [ -s $ctagfile ]
then
	ctag=`cat $ctagfile`
fi

# do not display client for storage test
if [[ $suite == "SC" || $technology == "network" || $technology == "serial" ]] && [ -s $csfile ]
then
    client=`cat $csfile`
fi

browser=`echo $HTTP_USER_AGENT | grep -i "Mozilla/4"`
if [ -n "$browser" ]
then
    browser="netscape4"
else
    browser="mozilla"
fi

# to fix 5005395, will putback in 2.4
#if [ -s $console_tag ]
#then
#	console=`cat $console_tag`
#fi
#user_addr=`echo $REMOTE_ADDR`
## judge if visited from console. if $console is empty, user not enter from start page, not operation permit
#is_console=
#if [ -n "$console" ] && [ $console == "$user_addr" ]
#then
#	is_console="true"
#	touch $console_tag
#fi

running=`ps -e | grep staf.eng`
if [ -n "$running" ]
then
    runningdisabled="disabled"
    stopdisabled=
    input_style_run="lcdisabled1"
    input_style_stop="lcnormal1"
else
    runningdisabled=""
    stopdisabled="disabled"
    input_style_run="lcnormal1"
    input_style_stop="lcdisabled1"
fi
# to fix 5005395, will putback in 2.4
#if [ ! $is_console ]
#then
#    runningdisabled="disabled"
#    input_style_run="lcdisabled1"
#fi	

# functions

# create button
do_button()
{
	echo '<FORM METHOD="POST" ACTION="'$1'">'
	echo '<INPUT TYPE="submit" VALUE="'$2'" class="lcnormal1">'
	echo "</FORM>"
}

# HTML begin
bhtml()
{
	hname=$1
	mname=$2
#	technology=$3

	echo "Content-type: text/html\n"
	echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">'
	echo "<HTML><HEAD>"
	echo '<link rel="stylesheet" type="text/css" href="/common.css" title="default">'
	echo '<script language="JavaScript" SRC="/util.js"></script>'
	echo "<TITLE>Hardware Certification Test Suite $version</TITLE></HEAD>"

	echo '<BODY TEXT="#000000" BGCOLOR="#ffffff" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">'
	echo '<TABLE WIDTH="100%" CELLSPACING=0 CELLPADDING=0>'
	echo '<TR><TD WIDTH="50%" ALIGN=LEFT><IMG SRC="/icons/sun_logo_rgb.gif" ALT=""></TD>'
	echo '<TD ALIGN=RIGHT><CENTER><FONT COLOR="#E76F00" SIZE="+2">'

	echo '<B>Hardware Certification Test Suite </B></FONT><br>'
	echo '<FONT SIZE="-1">v'$version'</FONT>'

	echo "</CENTER></TD></TR></TABLE>"
	if [ -z "$hname" ]
	then
		echo "<HR>"
		exit 0
	fi
#	if [ $hname = _HOME ]
#	then
#		echo '<HR><TABLE WIDTH="100%"><TR><TD WIDTH="100%"></TD><TD>'
#		do_button $gotocgi/start "Start Page"
#		echo "</TD></TR></TABLE>"
#		exit 0
#	fi
	echo '<HR><TABLE WIDTH="100%" border="0"><TR>'

	# only display sun logo and HCTS banner, do not display test status and navigation buttons in help and about page.
	if [ "$mname" == "help" ]
	then
		exit 0
	fi

	if [ $hname != _NONE ]
	then
		echo '<TD width="50%" align="left" class="grey1"><I>Test System = '$hname'</I></TD>'
	fi

	# test status display, added by Matthew Zhang, Feb. 2004
	if [ -n "$running"  ]
	then
		echo "<TD width='15%' align='right' class='grey1'><I>"
		if [ $suite == CC ]
		then
		    echo "Controller Certification"
		elif [ $suite == SC ]
		then
		    echo "System Certification"
		fi
		echo "</I></TD>"
		if [ -n "$ctag" ]
		then
			echo "<TD width='5%' align='right' class='grey1'><I>$ctag</I></TD>"
		elif [ -n "$level" ]
		then
			echo "<TD width='5%' align='right' class='grey1'><I>$level</I></TD>"
		else
			echo "<TD width='5%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		fi
		if [ $suite == CC ]
		then
		    if [ -n "$technology" ]
		    then
			echo "<TD width='10%' align='right' class='grey1'><I>$technology</I></TD>"
		    else
			echo "<TD width='10%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		    fi
		else
		    echo "<TD width='10%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		fi
		if [ -n "$client" ] && [ -n "$running" ]
		then
		    echo "<TD width='10%' align='right' class='grey1'><I>client:$client</I></TD>"
		else
		    echo "<TD width='10%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		fi
		if [ -n "$running" ]
		then
		    if [ -z "$run_time" ]
		    then
			run_time="0:0"
		    fi
		    echo "<TD width='10%' align='right' class='grey1'><I>run time:$run_time</I></TD>"
		    if [ $browser == mozilla ]
		    then
			echo '<TD align="right"><FORM METHOD="POST"><INPUT TYPE="BUTTON" VALUE="Refresh" onClick="javascript:location.reload();" class="lcnormal1"/></FORM></TD>'
		    fi
		else
		    echo "<TD width='15%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		fi
	else
		echo "<TD width='15%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		echo "<TD width='5%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		echo "<TD width='10%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		echo "<TD width='10%' align='right' class='grey1'><I>&nbsp;</I></TD>"
		echo "<TD width='10%' align='right' class='grey1'><I>&nbsp;</I></TD>"
	fi
	echo "</TR>"
	if [ -n "$running" ] && [ $browser == netscape4 ]
	then
	    echo '<TR><TD COLSPAN="6" WIDTH="100%" ALIGN="RIGHT" CLASS="grey1">Click Reload button in toolbar to refresh status</TD></TR>'
	fi
	echo "</TABLE>"
	echo '<TABLE WIDTH="100%">'
	echo '<TR><TD WIDTH="100%">'
	# to fix 5005395, will putback in 2.4
	#if [ $is_console ]
	#then
		echo '&nbsp;'
	#else
	#	echo "The HCTS console is at $console now."
	#fi
	echo '</TD>'
	mname=`basename $mname`
#	if [ $mname = home ]
#	then
#		echo "<TD>"
#		do_button $gotocgi/start "Start Page"
#		echo "</TD>"
#	else
#		echo "<TD>"
#		do_button $gotocgi/home?$hname+home "Home Page"
#		echo "</TD>"
#	fi
	if [ $browser == netscape4 ]
	then
	    if [ -z "$running" ] #&& [ $is_console ] # to fix 5005395, will putback in 2.4
	    then
		echo "<TD>"
		echo '<FORM ACTION="'http://$HTTP_HOST/cgi-bin/$hctsdir/home?$hname+home'" METHOD="POST">'
		echo '<input type=hidden name=suite value=CC>'
		echo '<input type="submit" value="Controller Certification">'
		echo "</FORM>"
		echo "</TD>"
		echo '<TD>'
		echo '<FORM ACTION="'http://$HTTP_HOST/cgi-bin/$hctsdir/home?$hname+home'" METHOD="POST">'
		echo '<input type=hidden name=suite value=SC>'
		echo '<input type="submit" value="Systems Certification">'
		echo "</FORM>"
		echo "</TD>"
	    else
		echo "<TD>"
		do_button $gotocgi/$prgdir/stop?$hname+$mname+$technology "Stop Tests"
		echo "</TD>"
	    fi
	else
	    echo "<TD>"
	    echo '<FORM ACTION="'http://$HTTP_HOST/cgi-bin/$hctsdir/home?$hname+home'" METHOD="POST">'
	    echo '<input type=hidden name=suite value=CC>'
	    echo "<input type='submit' value='Controller Certification' class='$input_style_run' $runningdisabled >"
	    echo "</FORM>"
	    echo "</TD>"
	    echo '<TD>'
	    echo '<FORM ACTION="'http://$HTTP_HOST/cgi-bin/$hctsdir/home?$hname+home'" METHOD="POST">'
	    echo '<input type=hidden name=suite value=SC>'
	    echo "<input type='submit' value='Systems Certification' class='$input_style_run' $runningdisabled >"
	    echo "</FORM>"
	    echo "</TD>"
	    echo "<TD>"
	    echo '<FORM METHOD="POST" ACTION="'$gotocgi/$prgdir/stop?$hname+$mname+$technology'">'
	    echo "<INPUT TYPE='submit' VALUE='Stop Tests'class='$input_style_stop' $stopdisabled >"
	    echo "</FORM>"
	    echo "</TD>"
	fi
	echo "<TD>"
        echo '<FORM METHOD="POST" ACTION="'$gotocgi/test_opt'">'
                echo '<INPUT TYPE="submit" VALUE="'Review Test Status'" class="lcnormal1">'
                echo '<INPUT TYPE="hidden" NAME="act" VALUE="'menu13'">'
                echo '<INPUT TYPE="hidden" NAME="hostn" VALUE="'$hname'">'
                echo '<INPUT TYPE="hidden" NAME="opt" VALUE="'$option'">'
                echo '<INPUT TYPE="hidden" NAME="tech" VALUE="'$technology'">'
        echo "</FORM>"
	echo "</TD><TD>"
	echo '<FORM>'
	echo "<INPUT TYPE='button' VALUE='Help' class='lcnormal1' onClick=\"window.open('$gotocgi/$hdir/help.eng?$hname+$mname+$technology','Help','width='+screen.width/2+',height='+screen.height*0.8+',scrollbars=yes,toolbar=yes');\">"
	echo '</FORM>'
	echo "</TD></TR></TABLE>"
	echo "<HR>"
}

# error page
err_pg()
{
        echo "<P><B>$*</B>"
}

# HTML end
ehtml()
{
	echo '<CENTER><P><HR>'
	echo '<B>Copyright 2005 by Sun Microsystems, Inc. All rights reserved.</B>'
	echo '</CENTER></BODY></HTML>'
}

# runtime 
runt()
{
	if [ $# -eq 4 ]
	then
		hour=$1
		minute=$2
		numi=$3
		snumi=$4
	fi	
	echo "n:Run Time Hour(s)!0!99:$hour"
	echo "x:1:"	
	echo "n:Run Time Minute(s)!0!59:$minute"
	echo "x:2:"
	echo "n:Maximum number of instances!1!$numi:$snumi"
	echo "x:3:"
	echo "c:Save test log(s) when test(s) pass:Yes"
	echo "x:LOG=true:"
	echo "c:Wait for test(s) to complete:"
	echo "x:wait:"
}

# display HCTS version
dis_ver()
{
	echo "HCTS Version: $version"
}

### MAIN ###

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

if [ $# -eq 0 ]
then
	echo "\nUSAGE: $0 [option]\n"
	exit 0
else
	$*
fi

