#!/bin/ksh
#
#pragma ident   "@(#)please_wait 1.8     05/06/21 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#


hctsdir=SUNWhcts            # base directory
prgdir=prgs             # program directory
act=                  # action to be taken
bname=`basename $0`     # shell name
gotocgi="http://$HTTP_HOST/cgi-bin/$hctsdir/`basename $0`"      # program name
i=0                     # loop counter
mdir=menu               # menu directory
rdir=results            # results directory
hname=$1                  # test system
option=$2               # option selected
fdir=forms              # forms directory
dname=                  # display system
trigger=.trig.tmp	# flag file for STAF start

technology=$3
disks=$4
res_disks=

lvfile=/tmp/level.dat
stfile=/tmp/suite.dat

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

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


# functions

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



do_button()
{
        echo '<FORM METHOD="POST" ACTION="'$gotocgi'">'
                echo '<UL><INPUT TYPE="submit" VALUE="'$2'"></UL>'
                echo '<INPUT TYPE="hidden" NAME="act" VALUE="'$1'">'
                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>"
}


### 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

if [ -z "$act" ]
then
       go_fun bhtml $hname $0 $technology
	echo '<TABLE WIDTH="100%"><TR><TD WIDTH="100%" ALIGN="CENTER">'
	echo '<FONT SIZE="+2">Hard Disk Confirmation</FONT></TD>'
	echo '</FORM></TD></TR></TABLE>'
        echo '<FORM METHOD="POST" ACTION="'$gotocgi'">'
		echo '<br><br>'
		res_disks=""
		format_disks=""
		for i in ${disks}
		do
			$prgdir/slice_opts.sh $hname -r $i
			if [ $? -eq 0 ] #reserved
			then
				res_disks=${res_disks}" "$i
			else
				format_disks=${format_disks}" "$i
			fi
		done
		echo '<p>Read the following information carefully.<br>'
		echo 'To confirm that you want to perform the following operations, click<br>'
		echo 'the <b>Continue</b> button below.<br><br></p>'
		if [ -n "${format_disks}" ]
		then
			echo '<p>Format the following devices for testing<br><br>'
			for i in ${format_disks}
			do
				i=`echo $i | sed -e "s;s.*;p0;"`
				echo "<b>/dev/dsk/${i}</b><br>"
			done
			echo '<br></p>'
		fi

		if [ -n "${res_disks}" ] || [ -n "${format_disks}" ]
		then
			echo '<p>Run test on the following disks<br><br>'
			for i in ${res_disks}
			do
				mp=`$prgdir/slice_opts.sh $hname -f $i`
				i=`echo $i | sed -e "s;s.*;p0;"`
				echo "<b>/dev/dsk/${i} (mount point $mp )</b><br>"
			done
			for i in ${format_disks}
			do
				i=`echo $i | sed -e "s;s.*;p0;"`
				echo "<b>/dev/dsk/${i}</b><br>"
			done
			echo '</p>'
		fi

		echo '<br><table border="1" cellpadding="6"><tr><td align="center">'
		echo '<p><b><i>Note: All data of the harddisks you select to format will be lost!</i></b></p>'
		echo '</tr></td></table>'
                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 '<INPUT TYPE="hidden" NAME="disks" VALUE="'$format_disks'">'
                echo '<INPUT TYPE="hidden" NAME="res_disks" VALUE="'$res_disks'">'
		echo '<br><br>'
                echo '<INPUT TYPE="submit" name="act" VALUE="Cancel">&nbsp;&nbsp;'
                echo '<INPUT TYPE="submit" name="act" VALUE="Continue">'
        echo "</FORM>"
       go_fun ehtml
else
	hname=$hostn
	option=$opt
	technology=$tech
	
	if [ $act = "Continue" ]; then
                ret=`$prgdir/slice_opts.sh $hname -u "${disks}"`
		if [ $? -ne 0 ]
		then
      			go_fun bhtml $hname $0 $technology
			echo $ret
			go_fun ehtml
			exit 1
		fi

               	ret=`$prgdir/slice_opts.sh $hname -s "${disks}"`
		if [ $? -ne 0 ]
		then
      			go_fun bhtml $hname $0 $technology
			echo $ret
			go_fun ehtml
			exit 1
		fi

               	ret=`$prgdir/slice_opts.sh $hname -t "${res_disks}"`
		if [ $? -ne 0 ]
		then
      			go_fun bhtml $hname $0 $technology
			echo $ret
			go_fun ehtml
			exit 1
		fi

		if [ -n "${disks}" ]
		then
			$prgdir/make_slice.sh $hname >/dev/null 2>&1
			if [ $? -ne 0 ]
			then
      				go_fun bhtml $hname $0 $technology
				echo "!ERROR: Can't make slices. Please make sure you can remote execute commands using rsh(1)."
				go_fun ehtml
				exit 1
			fi
      			go_fun bhtml $hname $0 $technology
			echo '<TABLE WIDTH="100%"><TR><TD WIDTH="100%" ALIGN="CENTER">'
			echo '<FONT SIZE="+2">Please stand by ...</FONT></TD>'
			echo '</FORM></TD></TR></TABLE>'
			echo '<script type="text/javascript">'
			echo 'function refr() {'
			echo '	document.sliceform.submit();'
			echo '}'
			echo 'window.onload=refr'
			echo '</script>'
	        	echo '<FORM NAME="sliceform" METHOD="post" ACTION="'$gotocgi'">'
	                echo '<INPUT TYPE="hidden" NAME="hostn" VALUE="'$hname'">'
	                echo '<INPUT TYPE="hidden" NAME="opt" VALUE="'$option'">'
	                echo '<INPUT TYPE="hidden" name="act" VALUE="monitor">'
	                echo '<INPUT TYPE="hidden" NAME="tech" VALUE="'$technology'">'
			echo 'The process of making slices will take a while, please stand by ...'
			echo '<br>'
			echo '<br>'
			echo '<br>'
			echo '<br>'
			echo '<br>'
	        	echo '</FORM>'
			go_fun ehtml
		else
			exec ./test_opt $hname $option $technology
		fi
	elif [ $act = "Cancel" ]; then
		exec ./slice_conf $hname $option $technology
	elif [ $act = "done" ]; then
		exec ./test_opt $hname $option $technology
	elif [ $act = "monitor" ]; then
	      	go_fun bhtml $hname $0 $technology
		echo '<TABLE WIDTH="100%"><TR><TD WIDTH="100%" ALIGN="CENTER">'
		echo '<FONT SIZE="+2">Please stand by ...</FONT></TD>'
		echo '</FORM></TD></TR></TABLE>'
		echo '<script type="text/javascript">'
		echo 'function refr() {'
		echo 'theMinutes="1"; //Input the minutes'
		echo 'setTimeout("document.sliceform.submit()",theMinutes*1000*5)'
		echo '}'
		echo 'function refresh_right_now() {'
		echo '	document.sliceform.submit();'
		echo '}'
		echo '</script>'
	        echo '<FORM NAME="sliceform" METHOD="post" ACTION="'$gotocgi'">'
	        echo '<INPUT TYPE="hidden" NAME="hostn" VALUE="'$hname'">'
	        echo '<INPUT TYPE="hidden" NAME="opt" VALUE="'$option'">'
	        echo '<INPUT TYPE="hidden" NAME="tech" VALUE="'$technology'">'
		output=`$prgdir/monitor_slice.sh`
		rev=$?
		if [ $rev -eq 1 ]
		then
			echo 'The process of making slices will take a while, please stand by ...<br>'
	                echo '<INPUT TYPE="hidden" name="act" VALUE="monitor">'
			echo '<script type="text/javascript">'
			echo 'window.onload=refr'
			echo '</script>'
		elif [ $rev -eq 0 ]
		then
			echo 'The process of making slices will take a while, please stand by ...<br>'
	                echo '<INPUT TYPE="hidden" name="act" VALUE="done">'
			echo '<script type="text/javascript">'
			echo 'window.onload=refresh_right_now()'
			echo '</script>'
		elif [ $rev -eq 2 ]
		then
			echo '<p><b>!Error: The following error occurred when making slices.</b>'
		fi
		echo "$output"
	        echo '</FORM>'
		go_fun ehtml
	fi
fi


