#! /bin/sh

# Description {{{
#
# This script is part of TESS, the (Te)st (S)ystem for (S)-Lang.
# It is intended to simplify the invocation, typically within a
# Makefile, of TESS-based automated regression suites.
# 
# Copyright (C) 2004 Massachusetts Institute of Technology 
# Michael S. Noble <mnoble@space.mit.edu> }}}

# Initialization  {{{

slsh_local() # {{{ Allows TESS to be exercised locally, before install
{
   slsh - <<EOT
prepend_to_slang_load_path("..");
set_import_module_path("..");
require("tess");
Component = path_sans_extname(path_basename("$1"));
evalfile("$1");
exit(0);
EOT
} # }}}

instruct() # {{{
{
   echo "tessrun: convenience script for running TESS scripts en masse"
   echo ""
   echo "It is intended to simplify the invocation, typically within a"
   echo "Makefile, of automated regression test suites for TESS, the"
   echo "(Te)st (S)ystem for (S)-Lang.  Each test (marked by a .t suffix)"
   echo "in the current directory will be automatically loaded into the"
   echo "S-Lang interpreter (within slsh, by default) and executed."
   echo ""
   echo "Returns 1 if any tests fail, otherwise 0."
   echo ""
   echo "Options:"
   echo "  -h           this help text"
   echo "  -l           supports local execution of examples (before install)"
   echo "  -v           verbose mode"
   exit 0
} # }}}

Silencer="2>/dev/null"
while getopts hlv opt 2>/dev/null ; do
  case $opt in
	h) instruct
	   ;;
	l) App=slsh_local
	   ;;
	v) Silencer=""
	   ;;
  esac
done
shift `expr $OPTIND - 1`


if [ -z "$App" ] ; then
   if [ $# -gt 0 ] ; then
	App="$*"
   else
	App=slsh
   fi
fi
# }}}

Outcome=0
for test in *.t ; do

   if [ "$test" = "*.t" ] ; then break ; fi

   eval $App $test $Silencer
   Code=$?
   if test $Code -ne 0 ; then
	echo "Error: $test failed in <$App> with error code <$Code>"
	Outcome=1
   fi
done

exit $Outcome
