#!/bin/ksh
#
#ident	"@(#)menu13	1.18	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Original Author: Michael Koller
# Delete option added by Andrew McCarthy (08 Sept 1999)

# variables
hctsdir=SUNWhcts			# base directory
hname=$1			# test system
mdir=menu			# menu directory
act=				# action to be taken
bname=`basename $0`		# shell name
gotocgi="http://$HTTP_HOST/cgi-bin/$hctsdir/$mdir/$bname"	# program name
prgdir=prgs			# program directory
dls=				# data list
stafrdir=/opt/SUNWstaf/reports	# STAF base directory
inline=				# input value
vfile=				# file to view
refresh=$2
technology=$3
running=                        # test running status

# functions

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

### MAIN ###

running=`ps -e | grep staf.eng`

# 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


# Review Test Status
if [ -z "$act" ]
then
	go_fun bhtml $hname menu13
	if [[ $refresh = menu13  && ($technology = storage  || $technology = serial) ]]
	then
		refresh=noref
		echo '<META http-equiv=Refresh content="1; URL='$gotocgi?$hname+$refresh'">'
	fi
	echo '<CENTER><FONT SIZE="+2">Review Test Status Screen</FONT></CENTER>'
	# list reports and logs
	if [ -d $stafrdir/$hname ]
	then
		dls=`cd $stafrdir/$hname; ls -t *`
		if [ -n "$dls" ]
		then      		
       			echo "<P><B><U>Select Report or Log File to View:</U></B>"
			echo '<FORM ACTION="'$gotocgi'" METHOD="POST">'
			echo '<P><SELECT NAME="vfile">'
			echo "$dls" | while read inline
			do
				echo "<OPTION>$inline"
			done
			echo "</SELECT>"
			echo '<P><INPUT TYPE="submit" VALUE="View File">'
			echo '<INPUT TYPE="hidden" NAME="hname" VALUE="'$hname'">'               
			echo '<INPUT TYPE="hidden" NAME="act" VALUE="view">'		
	      		echo "</FORM>"
			if [ -z "$running" ]
			then
			    echo '<P><FORM ACTION="'$gotocgi'" METHOD="POST">'
			    echo '<P><INPUT TYPE="submit" VALUE="Delete All Logs">'
			    echo '<INPUT TYPE="hidden" NAME="act" VALUE="delete">'
			    echo '<INPUT TYPE="hidden" NAME="hname" VALUE="'$hname'">'
			    echo "</FORM>"
			    echo '<P><FORM ACTION="'http://$HTTP_HOST/cgi-bin/$hctsdir/$mdir/menu16'" METHOD="POST">'
			    echo '<P><INPUT TYPE="submit" VALUE="Copy Test Results">'
			    echo '<INPUT TYPE="hidden" NAME="hname" VALUE="'$hname'">'
			    echo "</FORM>"
			fi
		else
			go_fun err_pg "NO test is running and NO reports or log files exist."
		fi
	else
		go_fun err_pg "NO test is running and NO reports or log files exist."
	fi
	go_fun ehtml
	exit 0
fi

# view report/log file
if [ $act = view ]
then
	go_fun bhtml $hname $0
	echo '<CENTER><FONT SIZE="+2">Review Test Status Screen</FONT></CENTER>'
	echo "<P><B>$vfile</B><HR>"
	if [ -a $stafrdir/$hname/$vfile ]
	then
		echo "<PRE>"
		cat $stafrdir/$hname/$vfile
		echo "</PRE>"
	else
		echo "<P>EMPTY FILE, reload previous page."
	fi	
	go_fun ehtml
fi

# Delete logs
if [ $act = delete ]
then
	go_fun bhtml $hname $0
	if [ -d $stafrdir/$hname ]
	then
		stafid=`ps -e | grep staf.eng`
	
		if [ -z "$stafid" ]
		then
			dls=`ls -t $stafrdir/$hname/*`
			if [ -n "$dls" ]
			then
				rm -f $stafrdir/$hname/*
				if [ $? -eq 0 ]
				then
					go_fun err_pg "All logs are deleted."
				else
					go_fun err_pg "Delete failed. Check file permissions."
				fi
			else
				go_fun err_pg "NO test is running and NO reports or log files exist."
			fi
		else
			
			go_fun err_pg "STAF is running. <BR> Deleting logs while STAF is running will 
		        	       result in unpredicted behavior. If you still want to delete logs, 
				       stop all tests and staf.eng and then  select the Delete All Logs
		             	       button."
			go_fun ehtml
			exit 0

		fi

	fi
	act=
	go_fun ehtml
fi

