#! /bin/sh

failed=0
all=0

debug='no'
testflags=''

if [ -z "$srcdir" ] ; then
  srcdir=`pwd`
fi

export srcdir

# When used in make rules, we sometimes get the filenames VPATH
# expanded, but usually not. 
find_program () {
    case "$1" in
	*/*)
	  echo "$1"
	  ;;
	*)
	  if [ -x "$1" ] ; then
	      echo "./$1"
	  else
	      echo "$srcdir/$1"
	  fi
	  ;;
    esac
}

env_program () {
  if [ -x "$1" ] ; then
    if "$1"; then : ; else
      echo FAIL: $1
      exit 1
    fi
  fi
}

test_program () {
  testname=`basename "$1" .exe`
  testname=`basename "$testname" -test`
  "$1" $testflags
  case "$?" in
      0)
    	echo PASS: $testname
	all=`expr $all + 1`
	;;
      77)
      	echo SKIP: $testname
      ;;
      *)
    	echo FAIL: $testname
	failed=`expr $failed + 1`
	all=`expr $all + 1`
	;;
  esac
}

env_program `find_program setup-env`

while test $# != 0
do
  case "$1" in
  --debug)
    debug=yes
    ;;
  -v)
    testflags='-v'
    ;;
  -*)
    echo >&2 'Unknown option `'"$1'" 
    exit 1
    ;;
  *)
    break
    ;;
  esac
  shift
done
  
if [ $# -eq 0 ] ; then
  for f in *-test; do test_program "./$f"; done
else
  for f in "$@" ; do test_program `find_program "$f"`; done
fi

if [ $failed -eq 0 ] ; then
  banner="All $all tests passed"
else
  banner="$failed of $all tests failed"
fi
dashes=`echo "$banner" | sed s/./=/g`
echo "$dashes"
echo "$banner"
echo "$dashes"

if [ "x$debug" = xno ] ; then
  env_program `find_program teardown-env`
fi

[ "$failed" -eq 0 ]

