#!/bin/ksh
#
#ident	"@(#)menu16	1.31	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Orig Author: Michael Koller
#  

# 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
inline=			# input value
files=
i=0			# loop counter
prgdir=prgs		# program directory
outdev=			# output device
tfile=report.out	# output file

outfile=		# output file
dls=			# data list
option=			# option selected
technology=

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

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

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

sfile=$suite.$level.summary		# summary file

# functions

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

parse()
{
	inline=`echo "$dls" | grep "$1"`
	i=`echo "$inline" | cut -f2 -d':'`
	echo "$i : \c"
	echo "$inline" | cut -f3- -d':'				
}

## VENDOR REPORT
# DO NOT add HTML, HEAD, or BODY tags to this section.
# The HTML generator for access1.sun.com requires as input,
# a "headless" HTML file that does not have HTML, HEAD, or BODY tags.
# Those tags will be inserted by both "nsift" report generator, and
# also the "makehtml" script for access1.sun.com

vendor_rep()
{
	echo '<FONT SIZE="+2"><B><U>Vendor and Model Information</U></B></FONT><P>'
	dls=`cat $rdir/vendor.frm`
	parse ":Manufacturer:"
	echo "<BR>"
	echo "Date of Report : "`/bin/date`"<P>"
	parse ":Contact Name:"
	echo "<BR>"
	parse ":Contact Phone Number:"
	echo "<BR>"
	parse ":Contact Email:"
	echo "<BR>"
	parse ":Driver Being Tested:"
	echo "<P>"
	parse ":Driver Version:"
	echo "<BR>"
	parse ":Controller Being Tested:"
	echo "<BR>"
	parse ":Controller BIOS Version:"
	echo "<P>"
}

## GENERATE REPORT
# DO NOT add HTML, HEAD, or BODY tags to this section.
# The HTML generator for access1.sun.com requires as input,
# a "headless" HTML file that does not have HTML, HEAD, or BODY tags.
# Those tags will be inserted by both "nsift" report generator, and
# also the "makehtml" script for access1.sun.com

generate_rep()
{

	(if [ $suite = CC ]
	then
		echo '<CENTER><FONT SIZE="+2"><B><U>Controller Certification Report</U></B></FONT></CENTER><P>'
	elif [ $suite = SC ]
	then
		echo '<CENTER><FONT SIZE="+2"><B><U>Hardware Certification Report</U></B></FONT></CENTER><P>'
	fi
	
#	vendor_rep
	echo '<FONT SIZE="+2"><B><U>'"$level"' Test Results</U></B></FONT><P>'
	go_fun dis_ver
#	echo "<BR>"
#	cat $rdir/res.txt
	echo "<BR>"
	grep "Processing Start Time:" $rdir/*.PROSUM
	echo "<BR>"
	grep "Processing Stop Time:" $rdir/*.PROSUM
	echo "<BR>"
	grep "Elapsed Time:" $rdir/*.PROSUM
	echo "</PRE><P>"
	for i in `echo "$files"`
	do
		cat $rdir/$i | while read inline
		do
			ch=`echo "$inline" | cut -f1 -d':'`
			lpart=`echo "$inline" | cut -f2 -d':'`
			fpart=`echo "$inline" | cut -f3- -d':'`
			case $ch in
			h)
				if [ -n "$fpart" ]
				then
					echo " "
					echo "<P>"
					echo "$lpart<P>" | sed -e s/"Edit"//
				fi
			;;
			n)
				lpart=`echo "$lpart" | cut -f1 -d'!'`
				echo "$lpart : $fpart<BR>"
			;;
			c|r)
				if [ -z "$fpart" ]
				then
					fpart=No
				fi
				echo "$lpart : $fpart<BR>"
			;;
			t|T)
				echo "$lpart : $fpart<BR>"
			;;
			esac
		done
	done
	echo '<P><FONT SIZE="+2"><B><U>STAF Report</U></B></FONT>'
	echo "<PRE>"
	cat $rdir/*.PROSUM
	
	if [ -f $rdir/bonnie.results ]
	then
		echo '<P><FONT SIZE="+2"><B><U>Bonnie Results</U></B></FONT>'
		echo "<PRE>"
		cat $rdir/bonnie.results
		rm $rdir/bonnie.results
	fi

	echo "</PRE>" ) > $rdir/$tfile
}

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

# prompt for device
if [ -z "$act" ]
then
#	dls=`$mdir/menu9 $hname`
#	if [ $? -ne 0 ]
#	then
#		echo "$dls"
#	else
		go_fun bhtml $hname $0
		echo '<CENTER><FONT SIZE="+2">Copy Test Results Screen</FONT></CENTER>'
		echo "<P>This action creates an output file"
		echo "that can be written to a diskette or to a directory of your"
		echo "choice. The file will be in tar and compressed format."
		echo '<FORM ACTION="'$gotocgi'" METHOD="POST">'				
			echo "Choose output device: "
			echo '<SELECT NAME="outdev">' 
				echo '<OPTION VALUE="/floppy/floppy0">floppy0 (A:)'
				echo '<OPTION VALUE="/floppy/floppy1">floppy1 (B:)'
			echo "</SELECT>"
			echo "<P>or<br>" 
			echo "type full path:"
			echo '<INPUT NAME="outfile" SIZE="30">'
			echo '<P><INPUT TYPE="radio" NAME="option" VALUE=" copy" CHECKED> Copy'		
			echo '<BR><INPUT TYPE="radio" NAME="option" VALUE=" preview"> Preview'		
			echo '<INPUT TYPE="hidden" NAME="act" VALUE="process">'
			echo '<INPUT TYPE="hidden" NAME="hname" VALUE="'$hname'">'
			echo '<P><INPUT TYPE="submit" VALUE="Process Report">'
		echo "</FORM>"
		go_fun ehtml
#	fi
	exit 0
fi

# Process Test Results
if [ $act = process ]
then	
	go_fun bhtml $hname $0
	cp /opt/SUNWhcts/$rdir/$hname/* /opt/SUNWhcts/$rdir
	dls=`ls -t /opt/SUNWstaf/reports/$hname/*.PROSUM | head -1`
	qual=`echo $dls | sed 's/\./ /' | awk '{print $1}'`

	# add by Matthew, fix 4997040, get suite and level from log file generated by staf.eng
	logs=`ls -at /opt/SUNWstaf/reports/$hname/.*.SUITE_LEVEL.log 2>/dev/null | head -1`
	if [ -n "$logs" ]
	then
		suite=`head -1 $logs`
		level=`head -2 $logs | tail -1`
		sfile=$suite.$level.summary
	fi

	logs=`ls $qual*LOG*`
	if [ -n "$logs" ]
	then
		for i in $logs
		do
			cp $i /opt/SUNWhcts/$rdir
		done
	fi
	cp $dls /opt/SUNWhcts/$rdir	
	generate_rep
	if [ $option = preview ]
	then
		cat $rdir/$tfile	
	elif [ $option = copy ]
	then
		cksum_file=`ls -at /opt/SUNWstaf/reports/$hname/.*.cks 2>/dev/null | head -1`
		(cd /opt/SUNWhcts; cp $cksum_file $rdir/file.cks ; tar cfp $rdir/$sfile.tar $rdir; compress $rdir/$sfile.tar ; rm $rdir/file.cks) >/dev/null 2>&1 
		if [ -z "$outfile" ]
		then
			if [ -z "`ps -e | grep vold`" ]
			then
				go_fun err_pg "Volume Manager must be running to copy to this device."
				go_fun ehtml
				rm -f results/*
				exit 0	
			fi
			volcheck
			sleep 5
			
			rfile=$sfile.tar.Z
			
			cp $rdir/$sfile.tar.Z $outdev/$rfile
			
			if [ $? -ne 0 ]
			then
				go_fun err_pg "Could not write results $rfile to $outdev ."
				go_fun ehtml
				rm -f results/*
				eject -p
				exit 0	
			fi
			echo '<CENTER><FONT SIZE="+2">Copy Test Results Screen</FONT></CENTER>'
			echo "<P><B>Copy was successful for: $outdev/$rfile</B>"
			eject -p
		else
			dls=$outfile
			if [ ! -d $dls ]
			then
				mkdir -p $dls
			fi
			
			if [ ! -w $dls ]
			then
				go_fun err_pg "Could not write results $sfile.tar.Z to $dls/$sfile.tar.Z "
				echo "no write permissions on $dls. Change permissions to world writable "
				echo "and try again."
				go_fun ehtml
				exit 0
			fi

			
			rfile=$sfile.tar.Z
				
			cp $rdir/$sfile.tar.Z $dls/$rfile

			if [ $? -ne 0 ]
			then
				go_fun err_pg "Could not write results $sfile.tar.Z to $dls/$rfile ."
				go_fun ehtml
				rm -f results/*
				exit 0	
			fi
			echo '<CENTER><FONT SIZE="+2">Copy Test Results Screen</FONT></CENTER>'
			echo "<P><B>Copy was successful for: $dls/$rfile</B>"
		fi
	fi
	rm -f results/*
	go_fun ehtml
fi

