#!/bin/ksh
#
#pragma ident   "@(#)slice_conf 1.11     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
count=

technology=$3

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
}

note()
{
	echo '<ol>'
	if [ "$1" == "usbdisk" ]
	then
		echo '<li><p>Select one hard disk from the list below.</p></li>'
	elif [ "$1" == "scsi" ] || [ "$1" == "raid" ] || [ "$1" == "fibre" ] || [ "$1" == "sata" ]
	then
		echo '<li><p>Select at least one, and at most three hard disks from the list below.</p></li>'
	fi
	echo '<li><p>Click the <b>Continue</b> button.</p></li>'
	echo '</ol>'

	echo '<p>To test large files, select disks that have a capacity of at '
	echo 'least 20&nbsp;Gbytes.<br>Disks that have any / '
	echo '(root directory), /usr, /opt, /var, or /export/home<br>mount '
	echo ' point must have at least 5&nbsp;Gbytes of free space.</p>'
}


### 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
	if [ "$option" == "system" ] || [ "$option" == "motherboard" ]
	then
		ret=`/opt/SUNWhcts/prgs/slice_opts.sh $hname -c scsi`
	else
		ret=`/opt/SUNWhcts/prgs/slice_opts.sh $hname -c $option`
	fi

	rev=$?
       	#	go_fun bhtml $hname $0 $technology
	#	echo "rev = $rev"
       	#	go_fun ehtml
	#if [ -z "$ret" ] # Everything is okay.
	#then
	#	exec ./test_opt $hname $option $technology
	if [ $rev -eq 2 ]
	then
       		go_fun bhtml $hname $0 $technology
		if [ "$option" == "fibre" ] || [ "$option" == "scsi" ] || [ "$option" == "raid" ] || [ "$option" == "sata" ]
		then
			echo "<p><b>!Error: The system being certified must have at least one hard disks that have Solaris partition.</b>"
		else
			echo "<p><b>!Error: No available USB hard drives found. The USB hard drive must be at least 20 Gbytes.</b>"
		fi

       		go_fun ehtml
	elif [ $rev -eq 0 ]
	then
       		go_fun bhtml $hname $0 $technology
		echo '<TABLE WIDTH="100%"><TR><TD WIDTH="100%" ALIGN="CENTER">'
		echo '<FONT SIZE="+2">Hard Disk Selection Screen</FONT></TD>'
		echo '</FORM></TD></TR></TABLE>' 
	        echo "<form 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\">"

       		echo "<P><B>Hard Disk List</B>"
       		echo "<br>"
		note $option

		if [ "$option" == "system" ] || [ "$option" == "motherboard" ]
		then
			disks=`rsh -l root $hname "/opt/SUNWhcts/prgs/slice_opts.sh $hname -a scsi"`
		else
			disks=`rsh -l root $hname "/opt/SUNWhcts/prgs/slice_opts.sh $hname -a $option"`
		fi
		echo '<table border="1" width="50%">'
		echo '<tr>'
		echo '<td>&nbsp;</td>'
		echo '<td>Device Name</td>'
		echo '<td>Mount Point</td>'
		echo '<td>Capacity</td>'
		echo '</tr>'
		j=0
		for i in $disks
		do
			i=`echo $i | sed -e "s;\/dev\/.*dsk\/;;" `
			mnt=`/opt/SUNWhcts/prgs/slice_opts.sh $hname -m $i`
			sz=`rsh -l root $hname "/opt/SUNWhcts/prgs/slice_opts.sh $hname -g $i"`
			echo "<tr>"
			echo "<td><input type=\"checkbox\" name=\"device_$j\" value=\"$i\"</td>"
			echo "<td>$i</td>"
			echo "<td>$mnt</td>"
			echo "<td>$sz</td>"
			echo "</tr><tr></tr>"
			let "j = $j + 1"
		done
		echo '</table>'
		echo "<input type=\"hidden\" name=\"count\" value=\"$j\">"

		echo '<br><table border="1" cellpadding="6"><tr><td align="left">'
		echo '<p align="center"><b><i>Warning</i></b></p>'
		echo '<p>Data on all partitions of the selected disks will be lost except for<br>'
		echo 'hard disks under the following mount points: /, /opt, /var, /usr, and<br>'
		echo '/export/home.<br>'
		echo 'Make sure the disks have no important data in any of their partitions.</p>'
		echo '</tr></td></table>'
		echo "<br>"
		echo "<input type=\"submit\" name=\"act\" value=\"Continue\">"
		echo "</form>"
       		go_fun ehtml
	fi
else
	hname=$hostn
	option=$opt
	technology=$tech
	disk_array=""
	if [ $act = "Continue" ]
	then
		j=0
		disk_count=0
		while [ $j -lt $count ]
		do
			temp="\${device_$j}"
			value=`eval "echo $temp"`
			if [ ! -z "$value" ]
			then
				disk_array="${disk_array} $value"
				let "disk_count = ${disk_count} + 1"
			fi
			let "j = j + 1"
		done

		if [ "$option" != "usbdisk" ] && ([ ${disk_count} -lt 1 ] || [ ${disk_count} -gt 3 ])
		then
       			go_fun bhtml $hname $0 $technology
			echo "<p><b>!Error: Select at least One (1), and at most three (3) hard disks. To select a disk, click your mouse pointer in the checkbox to the left of the device name in the list.</b>"
			go_fun ehtml
		elif [ "$option" == "usbdisk" ] && [ ${disk_count} -ne 1 ]
		then
       			go_fun bhtml $hname $0 $technology
			echo "<p><b>!Error: Select at least One (1) hard disks. To select a disk, click your mouse pointer in the checkbox to the left of the device name in the list.</b>"
			go_fun ehtml
		else
			exec ./please_wait $hname $option $technology "${disk_array}"
		fi
	else
       			go_fun bhtml $hname $0 $technology
			echo "Unknown option!"
       			go_fun ehtml
	fi
	#exec $mdir/$act $hname $option $technology
		
fi
