#!/bin/sh
#
# Script to test sml2c compiler and pervasives
#

case $# in
0) echo "$0: must specify output file"; exit ;;
1) case $1 in
    -clean) rm -fr bin/* output/* src/*.c src/*.o
	    exit 0
           ;;
    *)
       OUT=$1;
       OUTDIR=output/$OUT;
       BINDIR=bin/$OUT;
       FLAGS="-ffunction-integration -fno-zero-check"
       ;;
   esac
   ;;
2) OUT=$1;
   OUTDIR=output/$OUT;
   BINDIR=bin/$OUT;
   FLAGS=$2 ;;
*) echo "$0: too many arguments" ;;
esac

# remove existing summary file and output directory, if they exist

if test -f $OUT
then
    rm -f $OUT
fi

if test -d $OUTDIR 
then
   rm -r $OUTDIR
fi

if test -d $BINDIR 
then
   rm -r $BINDIR
fi

# place date and description in summary file

date >>$OUT
echo "Summary of test results" >>$OUT

# create output directory

mkdir $OUTDIR || { echo "Cannot create output directory $OUTDIR"; exit 1; };

# create binary directory

mkdir $BINDIR || { echo "Cannot create binary directory $BINDIR"; exit 1; };

for PROG in test1 test2 test3 test4 test5 test6 test7 test8 test9 smlyacc lexgen
#            test10 test11

  do
     if sml2c $FLAGS -o $BINDIR/$PROG src/$PROG.sml >$OUTDIR/$PROG.comp 2>&1 \
        && test -f $BINDIR/$PROG
     then
        if
          case $PROG in
          lexgen)
             ln -s ../../input/ml.lex $OUTDIR
             $BINDIR/$PROG $OUTDIR/ml.lex >$OUTDIR/$PROG.out 2>&1 
             ;;
          smlyacc)
             ln -s ../../input/ml.grm $OUTDIR
             $BINDIR/$PROG $OUTDIR/ml.grm >$OUTDIR/$PROG.out 2>&1
             ;;
          test10|test11)
                if $BINDIR/$PROG -g 0 $BINDIR/$PROG-heap >/dev/null
                then
                   if $BINDIR/$PROG -g 0 -s $BINDIR/$PROG-heap \
                       <input/$PROG.in >$OUTDIR/$PROG.out 2>&1
                   then :
                   else
                      echo "Load of heap for $PROG failed." >>$OUT
                   fi
                else
                   echo "Creation of heap for $PROG failed." >>$OUT
                fi
             ;;
          *) if test -f input/$PROG.in
             then
               $BINDIR/$PROG -g 0 <input/$PROG.in >$OUTDIR/$PROG.out 2>&1
             else
               $BINDIR/$PROG -g 0 >$OUTDIR/$PROG.out 2>&1
             fi
             ;;
          esac
        then
           case $PROG in
           smlyacc)
              if
               diff $OUTDIR/ml.grm.sml canon/ml.grm.sml >>$OUTDIR/$PROG.diff
                then
                  rm -f $OUTDIR/$PROG.diff
                  echo "$PROG is ok" >>$OUT
                  
                else 
                  echo "$PROG produced different output" >>$OUT
              fi
              ;;
           lexgen)
              if
                diff $OUTDIR/ml.lex.sml canon/ml.lex.sml >>$OUTDIR/$PROG.diff;
                then
                   rm -f $OUTDIR/$PROG.diff
                   echo "$PROG is ok" >>$OUT
                else
                   echo "$PROG produced different output" >>$OUT
              fi
              ;;
           *)
               if diff $OUTDIR/$PROG.out canon/$PROG.out >$OUTDIR/$PROG.diff
                  then
                    rm -f $OUTDIR/$PROG.diff
                    echo "$PROG is ok" >>$OUT
                  else
                     echo "$PROG produced different output" >>$OUT
               fi
              ;;
           esac
        else
             echo "$PROG did not successfully execute" >>$OUT
        fi
     else
        echo "$PROG did not compile with flags $FLAGS" >>$OUT
     fi
done   
