#!/bin/csh -f 
#  argument -v : my verbose
#  argument -V : csh verbose
#
#  usage: 
# 		$0  {-help}|{-files files}|{-dirs dirs} [-all] [-stoponerror] [-answers] [-dbx]
#		abbreviations:	-h, -f, -file, -d, -dir, -a, -s, -stop, -ans.
#
#	Environment variables:
#	EVOLID = list of the volids to be used when the script's "session" 
#	            command	has no -v arguments.
#			8000, 8001, etc are data volumes
#	due to a problem with et, if you want to use temp volumes with all
#  the old scripts, you need to have the non-temp volumes all precede the
# 	temp volumes in the EVOLID list (because once et discovers a temp
#	volume, it quits committing the tx that creates files on volumes,
#	and if the script aborts its first tx, the files disappear.)
# 			7000, 7001, etc are temp volumes
#			Servers for the volumes are determined by the .sm_config file.
#		Usage: Usually we need to test 4 series of runs:
# setenv EVOLID "8000" for non-distr; runtests -all 
# setenv EVOLID "8000 8001 8002..." for as many servers as you have ; runtests -dir distr/ 
# setenv EVOLID "8000 7000 ... " for non-distr with temp files;  runtests -all 
# setenv EVOLID "8000 8001 7000 7001" for distr with temp files; runtests -dir distr/
#				
#	EDEBUG	if set, debugging junk is printed
#	COREDUMP if set, ET dumps core on error.
#	ERETRIES if set, tells # times to retry to find a server
#	EVOTE if set, lists votes to use, can be "ALL", 0 means "normal"
#	ECRASHLOC if set, lists crashlocs to test, can be "ALL", 0 means "normal"
#
#   To run tests quickly by having the server use different ports,
#	run:
#		wsm_server.single 9000
#		setenv EPORTS 9000,4
#		setenv EHOSTNAME caseus
#		runtests ...
#
#	ET will use access the server on ports 9000-9003 on caseus 
#   

if ($?EFORMATPATH) then
	set FORMATVOL = $EFORMATPATH/formatvol
else
	echo "You must set environment variable EFORMATPATH to indicate where"
	echo "to find the formatvol program and its .sm_config file."
	echo "This has to be reachable from this machine; i.e., over NFS."
	echo "OR you must edit this script's definition of FORMATVOL"
	echo "to accomplish the formatting any way you wish."
	exit
endif 
#
# End of info about how to use runtests.
#

set WHICHET = ./src/et
set WHICHDBG = gdb
# set WHICHDBG = dbx

#
# crashloc runs from 1->15
# servervote runs from 1->3 
# for both, 0 means "normal"
#
if ($?ECRASHLOC) then
	set ecrashloc = `echo $ECRASHLOC`
else
	set ecrashloc = 0
endif
if ($ecrashloc == "all") then
	set ecrashloc = ( 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 )
else if ($#ecrashloc == 0) then
	set ecrashloc = ( 0 )
endif

if ($?EVOTE) then
	set evote = `echo $EVOTE`
else
	set evote = 0
endif
if ($evote == "all") then
	set evote = ( 0 1 2 3 )
else if ($#evote == 0) then
	set evote = ( 0 )
endif

if ($?EVOLID) then
	set evolid = `echo $EVOLID`
else
	echo You must set environment variable EVOLID.
	exit
endif

set volumes
while ($#evolid > 0)
	set volumes = ($volumes -vol $evolid[1])
	shift evolid
end

set fileslist=()
set dirslist=()

while ($#argv > 0)
	switch ($1)
		case -v:
			set myverbose = 1
			breaksw

		case -V:
			set verbose = 1
			breaksw

		case -h:
		case -help:
			goto usage
			exit 0

		case -s:
		case -stop:
		case -stoponerror:
			set stoponerror
			breaksw

		case -a:
		case -all:
			set all 
			breaksw

		case -ans:
		case -answers:
			set makeanswers 
			breaksw

		case -dbx:
			set usedbx
			breaksw

		case -d:
		case -dir:
		case -dirs:
			set indirs
			unset infiles
			breaksw

		case -f:
		case -file:
		case -files:
			set infiles
			unset indirs
			breaksw

		default
			if ($?infiles) then
				set fileslist=($fileslist  $1)
			else if ($?indirs) then
				set dirslist=($dirslist  $1)
			else
				echo "Unrecognized argument : "$1
usage:
				echo $0 "{-help}|{-files files}|{-dirs dirs} [-all] [-stoponerror] [-answers] [-dbx]"
				exit
			endif
			breaksw
	endsw
	shift
end

set results = results
if (-e $results) then
	mv results results.bak
#	echo -n "file " '"'$results'"' " exists. Remove? (y/n) "
#	if ($< != "y") exit(1);
#	rm -f $results
endif

set sc=() 

if ($?all) then
	set sc = (`find . -name "*.et" -print` )
else 
	if ($#dirslist > 0) set sc = ( `find $dirslist -name "*.et" -print` )
	if ($#fileslist > 0) set sc = ($sc $fileslist)
	if ($#sc == 0) then
		echo "No tests to run."
		goto usage
	endif
endif

echo "TEST OF " $#sc " SCRIPTS: " > $results
echo "TEST OF " $#sc " SCRIPTS: "
unset prev
foreach c ( $sc )
	if ($?prev) then 
		if ($prev != $c:h) then
			echo  "}" >> $results
			echo  "}"
			echo -n  $c:h"/{" >>  $results
			echo -n  $c:h"/{" 
		else
			echo -n "," >> $results
			echo -n ","
		endif
	else
		echo -n  $c:h"/{" >>  $results
		echo -n  $c:h"/{" 
	endif
	set prev = $c:h
	echo -n $c:t >> $results
	echo -n $c:t
end
echo  "}" >> $results
echo  "}"

foreach c ( $ecrashloc )
	setenv ECRASHLOC $c
	foreach v ( $evote ) 
		setenv EVOTE $v
		foreach t ( $sc )
			set head = $t:h
			set type = $head
			set tail = $t:t
			set prefix = $tail:r

			# see if a larger (600 page) buffer pool is needed
			grep -s "EBUFPAGES.*600" $t
			if ($status == 0) then
				# use a 600 page buffer pool
				set bufpages = "-bufpages 625"
				if ($?myverbose) then
					echo "Using " $bufpages "page buffer pool." >> $results
					echo "Using " $bufpages "page buffer pool." 
				endif
			else
				set bufpages = "-bufpages 300"
				if ($?myverbose) then
					echo "Using bufpages from .sm_config ">> $results
					echo "Using bufpages from .sm_config "
				endif
			endif

			set nonomatch
			if ($?myverbose) then
				echo "Removing files:" *.ovf core
			endif
			/bin/rm -f *.ovf core

		#	Eventually ET will do the formatting. For the time begin,
		#   we reformat every volume every time. gag.
		#
			if ($?myverbose) then
				echo "FORMATTING volumes: " $volumes
			endif
			$FORMATVOL $volumes
			if ($status != 0) then
				echo "ERROR: FORMATTING" >> $results
				echo "ERROR: FORMATTING"
				exit 3
			endif

			echo "....TESTING" $t " (ECRASHLOC="$ECRASHLOC" EVOTE="$EVOTE")" >> $results
			echo "....TESTING" $t " (ECRASHLOC="$ECRASHLOC" EVOTE="$EVOTE")" 

			if ($?usedbx) then
				
				if ($?myverbose) then
					echo $WHICHDBG $WHICHET
				endif
				$WHICHDBG $WHICHET
				unset rmoutput
				continue
			else
				if ($?myverbose) then
					echo cat $t "|" $WHICHET $bufpages ">&" /tmp/$prefix.tmp
				endif
				cat $t | $WHICHET $bufpages >& /tmp/$prefix.tmp
				@ etstatus = $status
				if ($etstatus != 0) then
					echo "ERROR: ET exited with status " $etstatus "on" $t >> $results
					egrep "failed validation" /tmp/$prefix.tmp >> $results
					echo "ERROR: ET exited with status " $etstatus "on" $t
					if ($?stoponerror) then
						echo "stopping" >> $results
						echo "stopping"
						exit
					endif
					unset rmoutput
				else
					set rmoutput = 1
				endif
			endif

			if (-e core) then
				echo "ET dumped core" >> $results
				echo "ET dumped core"
				exit
			endif

			if ($etstatus == 0) then
				if ($?makeanswers) then
					tail -100 /tmp/$prefix.tmp > $type/answers/$prefix 
				else
					if (-e $type/answers/$prefix) then
						tail -100 /tmp/$prefix.tmp > /tmp/$prefix.tmp100
						diff /tmp/$prefix.tmp100  $type/answers/$prefix >> $results
						if ($status == 1) then
							echo ERROR: DIFF of answers for $t >> $results
							echo ERROR: DIFF of answers for $t 
						else
							echo OK $t >> $results
							echo OK $t 
						endif
					else
						echo "OK: (no answers to diff)" $t >> $results
						echo "OK: (no answers to diff)" $t 
					endif
				endif
			endif
			if ($?rmoutput) then
				rm /tmp/$prefix.tmp*
			endif
		end
	end
end
