#!/bin/sh
#set -x

case "$top_srcdir" in
  '')
    echo '$top_srcdir must be set in environment'
    exit 1
  ;;
esac


########################################################################

test=`basename $1`
testdir=`dirname $1`
if ! test -f $1 ; then 
  testdir=`dirname $1`/tests
fi
testdir=`cd $testdir ; pwd`
if ! test -f $testdir/$test ; then
  if test "$test" = "check-prereqs" ; then
    exit 0 # hack to get Makefile dependencies for check
  else
    echo INTERNAL ERROR: cannot find $testdir/$test
    exit 1
  fi
fi

out=out/$test.out
ok=`cd $testdir/../ok ; pwd `/$test.out
if test ! -f "$ok" ; then
  ok=/dev/null # default is no output
fi

if test "`basename $test -sg.java`" = "$test" ; then
  # by default, we compile with -noinfer-sglobal for legacy reasons
  SGLOBAL_FLAGS=-noinfer-sglobal
else
  # PR 764 - tests ending in -sg.java are compiled with sglobal inference enabled
  SGLOBAL_FLAGS=-infer-sglobal
fi

########################################################################


[ -d out ] || mkdir out
rm -f core

# kill the compiler if it runs for more than 5 minutes without output
WATCHDOG="./TestSuite/bin/watchdog 300 500"

# Run test and filter output to remove $testdir/ prefix from file names in
# error messages, to remove column numbers, and to canonicalize "syntax error"
# to "parse error".
CLASSLIB=$testdir:$top_srcdir/tlib $WATCHDOG ../tc -fonly $SGLOBAL_FLAGS -nomain $MANUAL_TCFLAGS $testdir/$test 2>&1 | sed -e "s:^$testdir/::" \
       -e 's/syntax error$/parse error/' \
       -e 's/^\([^ :]*:[0-9]*:\)[0-9]*:/\1/' > $out

# Filter standard expected outputs similarly (no $testdir filtering needed,
# though) and compare.
cat $ok  \
| sed  -e 's/syntax error$/parse error/' \
       -e 's/^\([^ :]*:[0-9]*:\)[0-9]*:/\1/' \
| diff -u - $out \
    && [ ! -r core ]
