#!/bin/sh

DIR=`dirname $0`
ENGINE=`grep "^NET_ENGINE" ./$DIR/../../config.mak | awk '{print $3}'`
export CSCOMPILE="$ENGINE ../../ncc/out.stage3/ncc.exe";
export TEST_RUNTIME=$ENGINE

directory=
if [ "$#" -gt "0" ]; then 
  directory=$1
else
  directory=tests
fi

cp -f Nemerle.dll $directory/

for i in ./$directory/*.n; do
  name=`echo $i | sed 's/\.n//g'`;
  options=`sed -n 's,^// Compiler options:,,p' $i`; \
  case $options in *-t:library*) ext=dll ;; *-t:module*) ext=netmodule ;; *) ext=exe ;; esac; \
  testlogfile="$name.log" ; \
  echo "*** $CSCOMPILE" "$options -out:$name.$ext $i" > $testlogfile ; \
        if $CSCOMPILE $options -q -lib $directory/ -out:$name.$ext $i >> $testlogfile 2>&1 ; then \
          if test -f $name.exe; then \
            echo "*** $TEST_RUNTIME -O=-all ./$name.exe" >> $testlogfile ; \
              if $TEST_RUNTIME -O=-all ./$name.exe >> $testlogfile 2>&1 ; then \
                  echo "PASS: $name"; \
                  rm -f $testlogfile;  \
              else \
                echo "Exit code: $?" >> $testlogfile ; \
                echo "FAIL: $name runtime"; \
              fi ; \
          else \
            echo "PASS: $name: compilation"; \
            rm -f $testlogfile ; \
          fi ; \
        else \
          echo "Exit code: $?" >> $testlogfile ; \
          echo "FAIL: $name: compilation"; \
        fi ; \
#        if test ! -f $testlogfile ; then :; else cat $testlogfile; fi
done;

rm -f *xml