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

set Button1a = 'Button1 -I Button1a.Dat -S Button1a.Scr -V Button1a.vis -O Button1a.prt'
set Button1b = 'Button1 -I Button1b.Dat -S Button1b.Scr -V Button1b.vis -O Button1b.prt'

setenv AUTOVPATH /net/roundel/u2/output/REL1.1.3/Toolkit/Buttons

foreach ttest (ArrowBGad1 ArrowBGad2 ArrowBGad3 ArrowBGad4 ArrowBGad5 \
	      ArrowBtn1 ArrowBtn2 ArrowBtn3 ArrowBtn4 "$Button1a" "$Button1b" \
              DrawnBtn2 PushBGad1 \
              PushBGad2 PushBGad3 PushBGad4 PushBtn1 PushBtn2 ToggleBGad1 \
              ToggleBGad2 ToggleBGad3 ToggleBGad4 ToggleBtn1 ToggleBtn2)

# END CUSTOMIZE #
	set test =($ttest)

        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 ($mode == record) then
		$test -r 
	else
		$test 
	endif
	echo "status is " $status

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

   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

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