#!/bin/sh
#
# $Id: run-tests.in,v 1.31 1999/07/25 00:34:05 lha Exp $
#
srcdir=@srcdir@
objdir=`pwd`
SHELL=/bin/sh
VERSION=@VERSION@
PACKAGE=@PACKAGE@
host=@host@

AFSROOT=${AFSROOT-/afs}
export AFSROOT

ARLA_TESTS="creat1 mkdir1 mkdir2 symlink1 hardlink1 hardlink2 touch1 write1 write2 append1 rename1 rename2 mkdir-lnk dd exec readfile-wo-create mountpoint strange-characters strange-other-characters write-ro-file hello-world read-vs-mmap read-vs-mmap2 mmap-and-read dir-size-mismatch build-gdb large-dir-16384 large-dir-extra find-and-cat-netbsd test-gunzip-gnu-mirror untar-emacs build-emacs copy-and-diff-gnu-mirror setpag discon-create discon-echo discon-touch1 discon-touch2 discon-mkdir discon-mkdir2 discon-tar1 discon-tar2 fcachesize-dir fcachesize-file-small"
# you need to be root to run this: setgroups

MILKO_TESTS="mkdir many-dirs many-files many-symlinks many-files-with-content many-stores many-fetchs deep-tree deep-tree2 boot-strap-arla"

TESTS="$ARLA_TESTS"
TEST_MODE="arla"

linebreak=":-------------------------------;"

PARALLELL=
FAST=
PRINT_CACHESIZE=
usage="Usage: $0 [-arla|-milko] [-all] [-fast] [-j] [-verbose] [-x] tests ..."
while true
do
  case $1 in
  -all) ALL=yes;;
  -fast) FAST=yes;;
  -j) PARALLELL="&";;
  -verbose) VERBOSE=yes;;
  -x) SHELLVERBOSE="-x";;
  -p) PRINT_CACHESIZE="yes";;
  -arla) TESTS="$ARLA_TESTS";TEST_MODE="arla";;
  -milko) TESTS="$MILKO_TESTS";TEST_MODE="milko";;
  -help|--help) echo $usage; 
	echo "arla tests available: $linebreak";echo "$ARLA_TESTS"; 
	echo "milko tests available: $linebreak";echo "$MILKO_TESTS"; 
	exit 0;;
  -version|--version) echo "$0 $Id: run-tests.in,v 1.31 1999/07/25 00:34:05 lha Exp $"; exit 0;;
  -*) echo "$0: Bad option $1"; echo $usage; exit 1;;
  *) break;;
  esac
  shift
done

if test "X$WORKDIR" = "X";then
	echo "WORKDIR=workdir $0 $* or env WORKDIR=workdir $0 $*"; exit 1;
fi

RUNTESTS=
if test "X$ALL" != "X" ; then
    RUNTESTS="$TESTS"
elif test $# -lt 1; then
    echo $usage; exit
else
    RUNTESTS=$*
fi

# these are variables exported to the scripts

export FAST
export VERBOSE
export SHELLVERBOSE

# and file descriptors

# 3 - progress
# 4 - details

if test "$VERBOSE" = "yes"; then
  exec 3>/dev/null
  exec 4>&1
else
  exec 3>&1
  exec 4>/dev/null
fi

# Find out where we really are

srcdir=`cd $srcdir; pwd`
objdir=`cd $objdir; pwd`

export srcdir
export objdir

echo "-------------------------------------------------"
echo "$PACKAGE-$VERSION"
echo "hosttype $host"
echo "${SHELL},${SHELLVERBOSE},${VERBOSE},${PARALLELL},${FAST}"
echo "testmode ${TEST_MODE}"
echo "${srcdir}"
echo "${objdir}"
echo "${WORKDIR}"
date
echo "-------------------------------------------------"

test "X$VERBOSE" != "X" && echo "Running tests"

FAILEDTESTS=

for a in $RUNTESTS; do
  #
  # XXX Test if binary in $srcdir, shellscript in $srcdir else
  # its a binary in objdir
  #
  if test -x ${srcdir}/$a ; then
    b="${srcdir}/$a"
  elif test -f ${srcdir}/$a ; then
    b="${SHELL} ${SHELLVERBOSE} ${srcdir}/$a"
  else
    b="${objdir}/$a"
  fi
  echo "Running $a"
  test "X$VERBOSE" != "X" && echo "Running test $a ($b)."
  tmpdir="`hostname`-$a-$$"
  cd $WORKDIR && mkdir $tmpdir && (cd $tmpdir && $b ${PARALLELL})
  saved_res=$?
  test "X$VERBOSE" != "X" && echo "Saved res = $saved_res"
  if test "X${PARALLELL}" = "X" ;then
     if test $saved_res != 0 ; then 
	echo "Test $a FAILED"
        FAILEDTESTS="${FAILEDTESTS} $a"; 
     else
	test "X$VERBOSE" != "X" && echo "Test $a succeeded, tmpdir is removed"
	rm -rf $tmpdir
     fi
     test "X$VERBOSE" != "X" && echo "Done test $a."
  fi
  test "X${PRINT_CACHESIZE}" = Xyes && $objdir/../appl/fs calculate
done

wait

if test "$FAILEDTESTS"; then
  echo "-----------------------------------------------------------"
  echo "Failed test(s) were: $FAILEDTESTS"
else
  echo "All test(s) were succesful!"
fi
