#!/bin/csh -f

# Usage: test [-s <smlimage] [-G gooddir] [-B baddir] [-T testdir]

#
# defaults
#
set CPULIMIT 		= 900
set CMD 		= "test: "
set SML 		= /usr/local/bin/sml
set TEST_DIR		= bugs
set GOOD_DIR 		= ""
set BAD_DIR		= ""
set TSML		= ./tsml
set BIN_DIR		= ./bin
set MKTSML		= $BIN_DIR/mktsml
set errors 		= 0
set TMP_FILE		= $TSML.tmp
set DEPOSIT		= ""

#
# Process command-line options
#
while ($#argv != 0)
   switch ($argv[1])
   case -c: 
        if ($#argv < 2) then
            echo $CMD Must specify cpulimit 
	    set errors = 1
	else
	    shift
	    set CPULIMIT = $1
        endif
	breaksw

   case -s: 
	if ($#argv < 2) then
	    echo $CMD Must specify sml binary to use
	    set errors = 1
	else
	    shift
	    set SML = $1
        endif
        breaksw

   case	-G:
	if ($#argv < 2) then
	    echo $CMD Must specify directory where expected results are found
	    set errors = 1
        else
	    shift
	    set GOOD_DIR = $1
	endif
        breaksw

   case	-B:
	if ($#argv < 2) then
	    echo $CMD Must specify directory where bad results are found
	    set errors = 1
        else
	    shift
	    set BAD_DIR = $1
	endif
        breaksw

   case -T:
        if ($#argv < 2) then
	    echo Must specify directory of test files
            set errors = 1
        else
            shift
            set TEST_DIR = $1
        endif
        breaksw

   case -E:
	if ($#argv < 2) then
	   echo Did not specify name of temporary sml executable
           echo Using ./tsml
        else
           shift
           set TSML = $1
	   set TMP_FILE = $TSML.tmp
        endif
        breaksw

   case -D:	
	if ($#argv < 2) then
	   echo Did not specify directory to deposit compiler output
	   set errors = 1
        else
           shift
           set DEPOSIT = $1
        endif
        breaksw

   default:
	echo $CMD Bad option $1
	set errors = 1
   endsw
   shift
end 


#
# Abort if errors
#
if ($errors) then
   echo $CMD errors ...
   exit(1)
endif

#
# Does test and bin directory exist.
#
if (!(-d $TEST_DIR)) then
   	echo Not a directory $TEST_DIR
   	exit(2)
endif

if (!(-d $BIN_DIR)) then
	echo Not a directory $BIN_DIR
	exit(2)
endif

if (!(-e $SML)) then
	echo Error: $SML does not exist
	exit(2)
endif


#
# Build a version of the compiler without the banner and 
# gc messages turned off. New compiler is called tsml
#
echo Building $TSML ...
$MKTSML $SML $TSML

#
# Check if depositing new files
#
test -z "$DEPOSIT"
if ($status != 0) then
	echo "                Generating compiler output ..."

	if (!(-d $DEPOSIT) ) then
		echo Not a directory $DEPOSIT
		exit(2)
	endif	

    	set nfiles = `ls $DEPOSIT | wc -w`
    	if ($nfiles == 0) then
       		echo Directory $DEPOSIT is empty
		exit(3)
	else
	        foreach f ($DEPOSIT/*.out)
			set resultfilename = $f:t
	           	set srcfilename = $resultfilename:r.sml
	           	set srcfile = $TEST_DIR/$srcfilename

        	   	echo -n  Creating: $resultfilename ...
	           	if (! (-e $srcfile)) then
	               		echo -n "    Huh: Test file does not exist"
	           	else
        	       		/bin/csh -f -s << yyy >& $TMP_FILE
	               		limit cputime $CPULIMIT
	               		cat $srcfile | $TSML
yyy
				rm -rf $f
				mv $TMP_FILE $f
				echo done
			endif
		end
	endif
	goto cleanup
endif

		
# Test for regression!
#

#
# Set good directory.
#
test -z "$GOOD_DIR"
if ($status == 0) then
   	set GOOD_DIR = $TEST_DIR:h/good
endif

if (-d $GOOD_DIR) then

    	echo "               Testing for regression ..."

    	set nfiles = `ls $GOOD_DIR | wc -w`
    	if ($nfiles == 0) then
       		echo $GOOD_DIR is empty
	else
	        foreach f ($GOOD_DIR/*.out)
			set resultfilename = $f:t
	           	set srcfilename = $resultfilename:r.sml
	           	set srcfile = $TEST_DIR/$srcfilename

        	   	echo -n  Testing: $srcfilename ...
	           	if (! (-e $srcfile)) then
	               		echo "    Huh: Test file does not exist"
	           	else
        	       		/bin/csh -f -s << yyy >& $TMP_FILE
	               		limit cputime $CPULIMIT
	               		cat $srcfile | $TSML
yyy
               			diff $f $TMP_FILE >& /dev/null
				if ($status == 0) then  
	       				echo "   -- ok"
				else 
       	           			echo "   ** Possible regression"
                  			echo diff $f output
                   			diff -b $f $TMP_FILE
				endif	
	           	endif
	        end
	endif
endif

#
# Check if any bugs have been fixed.
#

#
# Set bad directory name
#
test -z "$BAD_DIR"
if ($status == 0) then
   	set BAD_DIR = $TEST_DIR:h/bad
endif

if (-d $BAD_DIR) then

    	echo "               Testing for bugs fixed ..."

	set nfiles = `ls $BAD_DIR | wc -w`
    	if ($nfiles == 0) then
		echo $BAD_DIR is empty
    	else
        	foreach f ($BAD_DIR/*)

	           	set resultfilename = $f:t
	   		set srcfilename = $resultfilename:r.sml
           		set srcfile = $TEST_DIR/$resultfilename:r.sml

           		echo -n Testing: $srcfilename ...
        
	   		if (! (-e $srcfile)) then
 		              echo "    Huh: Test file does not exist"
		        else

               			/bin/csh -f -s << yyy >& $TMP_FILE
               			limit cputime $CPULIMIT
               			cat $srcfile | $TSML
yyy
               			diff $f $TMP_FILE > /dev/null
       	       			if ($status == 0) then
               	  			echo "   -- no change"
       				else 
               				echo "   ** Possible bug, fixed"
               				echo diff $f output
                  			diff -b $f $TMP_FILE
				endif	
           		endif
        	end
    	endif
endif




#
# Test for files not categorized
#

echo "               Testing for uncategorized files ..."

foreach  f ($TEST_DIR/*)
	set rf = $f:t
   	set of = $rf:r.out
   	if ((-d $GOOD_DIR) && !(-e $GOOD_DIR/$of)) then
		if ((-d $BAD_DIR) && !(-e $BAD_DIR/$of)) then
       			echo Not categorized: $rf
      		endif
	endif
end




#
# Perform sanity check
#

if (-d $GOOD_DIR && -d $BAD_DIR) then 
    
	echo "               Testing for duplicates ..."
    	foreach f ($GOOD_DIR/*)
		if (-e $BAD_DIR/$f:t) then
       			echo Duplicates: $f:t 
       		endif
	end
endif



#
# Clean up
#
cleanup:
rm -f $TSML $TMP_FILE

exit(0)


