#!/bin/csh
# a known restriction of this script is that the arguments to -xrm (or
# any other flag!) must contain no spaces.

# default vars

onintr Cleanup

set mode = manual

while ($#argv)
	switch ($argv[1])
		case -r*:
			set mode = record
			breaksw
		case -c*:
			set mode = compare
			breaksw
		case -d*:
			setenv DISPLAY $argv[2]
			shift
			breaksw
	endsw
	shift
end

echo "Mode is " $mode

# clean up from previous runs

if (-e ./RUN_failures) then
	echo "backing up RUN_failures file"
	mv RUN_failures RUN_failures.bak
endif

touch RUN_failures

if ($mode != manual) then
	if (-e ./RUN_output) then
       		echo "backing up output file"
		mv RUN_output RUN_output.bak
	endif
	touch RUN_output
endif

rm -f core

# noglob is needed to handle special cases

set noglob

# special cases. Specify ALL flags for tests which require them.
# remember all arguments MUST be a single word (ie no spaces)
# use single quotes around the right side.

# START CUSTOMIZE # 

setenv AUTOVPATH /net/roundel/u2/output/REL1.1.3/Toolkit/Manager
#setenv AUTOVPATH $cwd/output

set BBoard1 = 'BBoard1 -xrm *BulletinBoard3.shadowThickness:10'
set BBoard5a_1 = 'BBoard5 -u 0,10 -I BBoard5a.Dat -xrm *board.resizePolicy:XmRESIZE_ANY -S BBoard5a.Scr -O BBoard5a_1.prt -V BBoard5a_1.vis'
set BBoard5a_2 = 'BBoard5 -u 10,5  -I BBoard5a.Dat -xrm *board.resizePolicy:XmRESIZE_NONE -xrm *board.width:400 -xrm *board.height:400 -S BBoard5a.Scr -O BBoard5a_2.prt -V BBoard5a_2.vis'
set BBoard5a_3 = 'BBoard5 -u 20,20 -I BBoard5a.Dat -xrm *board.resizePolicy:XmRESIZE_NONE -S BBoard5a.Scr -O BBoard5a_3.prt -V BBoard5a_3.vis'
set DrawArea1a = 'DrawArea1 -u xmBulletinBoardWidgetClass -I DrawArea1a.Dat -S DrawArea1a.Scr -O DrawArea1a.prt -V DrawArea1a.vis'
set DrawArea1b = 'DrawArea1 -u xmFormWidgetClass -I DrawArea1b.Dat -S DrawArea1b.Scr -O DrawArea1b.prt -V DrawArea1b.vis'

foreach test ("$BBoard1" BBoard2 BBoard3 BBoard4 "$BBoard5a_1" \
	      "$BBoard5a_2" "$BBoard5a_3" \
	      BBoard6 BBoard7 "$DrawArea1a" "$DrawArea1b" DrawArea2 \
	      Frame1 Frame2 Frame3 Frame4 \
	      RowCol1 RowCol3 ) 

#RowCol1 must be manual
# END CUSTOMIZE #
	
	set test =($test)
	
	foreach word ($test)
		if ($?name_flag) then
			set test_output = $word
			unset name_flag
		endif
		if ("$word" =~ "-O") then
			set name_flag = 1
		endif
	end
		
	if ($mode != manual) then
		echo "" >> RUN_output
		echo "Beginning test " $test[1] >> RUN_output
		echo "Beginning test " $test[1]
	endif

	if ($?test_output && $mode != manual) then
		echo "Output redirected to " $test_output
		echo "Output redirected to " $test_output >> RUN_output
	endif

	if ($mode == record) then
		$test -r 
	else
		$test 
	endif

	if ($status != 0) then
		if (-e core) then
			echo "backing up core to "$test[1]".core"
			mv core $test[1].core
		endif
		echo "failure in test: "$test[1] >> RUN_failures
		if ($mode != manual) then
			echo "FAILURE IN TEST: "$test[1] >> RUN_output
		endif
		echo "FAILURE IN TEST: "$test[1] 
 	endif 

	if ($mode != manual) then
		if ($?test_output) then
			cat $test_output >> RUN_output
		else
	 		cat $test[1].prt >> RUN_output
		endif
        	echo "Ending test "$test[1]"" >> RUN_output
		echo "" >> RUN_output
	        echo "Ending test "$test[1]""
		echo ""
	endif
	unset test_output


end

unset noglob

	if ($mode == record) then
		foreach file (*.prt)
			mv $file $AUTOVPATH
		end
	endif	

	if ($mode == compare) then
#       	compare number of *.prt files in both places
		set prts_here = `ls -l ./*.prt | wc -l`
		set prts_there = `ls -l $AUTOVPATH/*.prt | wc -l`
		if ($prts_here > $prts_there) then
		   echo "There are .prt files missing in " $AUTOVPATH >> RUN_failures
		   echo "There are .prt files missing in " $AUTOVPATH 
		else if ($prts_there > $prts_here) then
		   echo "There are .prt files missing in " $cwd >> RUN_failures
		   echo "There are .prt files missing in " $cwd 
		endif
		foreach file ($AUTOVPATH/*.prt)
		   if (-e $cwd/$file:t) then
			diff -c $file $cwd/$file:t > $file:t.diff
			if ($status != 0) then
			  echo "Failure in "$file:t" differences" >> RUN_failures
			  echo "Failure in "$file:t" differences" >> RUN_output
			  echo "Failure in "$file:t" differences"
			  mv $file:t.diff $file:t.err
			else
			  rm $file:t.diff
			endif
		   else
			echo "No "$file:t".prt file found in AUTOVPATH" >> RUN_output
			echo "No "$file:t".prt file found in AUTOVPATH" 

		   endif
		end
	endif

Cleanup:
# remove empty .err files of no interest
# this counting first is to avoid a "foreach: no match" error

if ($mode != manual) then
   set num_errfiles = `ls -l *.err | wc -l`
   if ($num_errfiles) then
       foreach errfile (*.err)
	    sed -e '/^$/d' $errfile > $errfile.stp
	    rm $errfile
	    if !(-z $errfile.stp) then
		mv $errfile.stp $errfile
	    else
		rm $errfile.stp
	    endif
       end
    endif

    echo "Automated run of directory" $cwd:t " complete" >> RUN_output

endif

exit 0 
