#!/bin/sh

# This script runs the regression tests in src/regression.
# It also compiles the tests in benchmark/tests

name=`basename $0`

function usage() {
	echo >&2 "usage: $name"
	exit 1
}

case "$#" in
0)
	;;
*)
	usage
	;;
esac

dir=`dirname $0`
root=`cd $dir/../.. && pwd`
src="$root/src"
lib="$root/lib"
bin="$root/bin"
mlton="$bin/mlton"
thread='thread0.sml thread1.sml thread2.sml mutex.sml prodcons.sml'
world='world1.sml world2.sml world3.sml world4.sml world5.sml world6.sml'
tmp=/tmp/z.regression.$$
log=$src/regression/log
PATH=.:$PATH

(
	cd $src/regression
	for f in `ls *.sml`
	do
		f=`basename $f .sml`
		case "$f" in
		cobol|serialize|testdyn2)
			echo "skipping $f"
		;;
		*)
 			echo "testing $f"
			case "$f" in
			exnHistory*)
				flags='-exn-history true'
			;;
			*)
			;;
			esac
			$mlton -type-check true $flags $f.sml
			if [ $? -ne 0 ]; then
				echo "compilation of $f failed"
			elif [ ! -r $f.nonterm ]; then
				$f >$tmp 2>&1
				if [ -r $f.ok ]; then
					diff $f.ok $tmp
				fi
			fi
			rm -f $f $f.cps $f.c
		;;
		esac
	done 
	make clean > /dev/null
        cd $src/benchmark/tests
	for f in `ls *.sml`
	do
		f=`basename $f .sml`
		tmpf=/tmp/$f.$$
		case "$f" in
		fxp)
			echo "skipping $f"
		;;
		*)
 			echo "testing $f"
			echo "val _ = Main.doit ()" | cat $f.sml - > $tmpf.sml
			$mlton -type-check true $tmpf.sml
			if [ $? -ne 0 ]; then
				echo "compilation of $f failed"
			fi
			rm -f $tmpf $tmpf.sml
		;;
		esac
	done 
	make clean > /dev/null
	cd $src
	for f in mllex mlyacc mlprof
	do
	    tmpf=/tmp/$f.$$
	    cd $src/$f
	    case "$f" in
	    foobar)
		    echo "skipping $f"
	    ;;
	    *)
		    echo "testing $f"
		    make -W $f > /dev/null
		    $mlton -type-check true -o $tmpf $f.cm
		    if [ $? -ne 0 ]; then
				echo "compilation of $f failed"
		    fi
		    rm -f $tmpf
	    ;;
	    esac
      done
) 2>&1 | tee $log

rm -f $tmp
