#!/bin/sh
#
# $Id: run-tests.in,v 1.66 2000/07/01 14:45:28 assar Exp $
#
srcdir=@srcdir@
objdir=`pwd`
SHELL=/bin/sh
VERSION=@VERSION@
PACKAGE=@PACKAGE@
host=@host@

AFSROOT=${AFSROOT-/afs}
export AFSROOT

ARLA_TESTS="creat1			\
	    mkdir1			\
	    mkdir2 			\
	    symlink 			\
	    hardlink1			\
	    hardlink2			\
	    touch1			\
	    write1			\
	    write2			\
	    append1			\
	    rename1			\
	    rename2			\
	    rename3			\
	    rename4			\
	    rename5			\
	    rename-under-feet		\
	    mkdir-lnk			\
	    mkm-rmm			\
	    fs-sa-la			\
	    dd				\
	    exec			\
	    ga-test			\
	    kotest			\
	    readfile-wo-create		\
	    mountpoint			\
	    strange-characters		\
	    strange-characters-c	\
	    strange-other-characters	\
	    pine			\
	    write-ro-file		\
	    hello-world			\
	    write-ucc			\
	    utime-dir			\
	    append-over-page		\
	    mmap-shared-write		\
	    read-vs-mmap		\
	    read-vs-mmap2		\
	    mmap-vs-read		\
	    mmap-vs-read2		\
	    read-write			\
	    mmap-and-read		\
	    checkpwd			\
	    exit-wo-close		\
	    dir-size-mismatch		\
	    parallel1			\
	    getdents-and-unlink1	\
	    getdents-and-unlink2	\
	    getdents-and-unlink3	\
	    build-gdb			\
	    large-dir-16384		\
	    large-dir-extra		\
	    find-linux			\
	    find-and-cat-netbsd		\
	    test-gunzip-gnu-mirror	\
	    build-emacs			\
	    build-emacs-j		\
	    untar-emacs			\
	    copy-and-diff-gnu-mirror	\
	    setpag			\
	    create-remove-files		\
	    create-remove-dirs		\
	    fcachesize-dir		\
	    fcachesize-file-small	\
	    compare-inums		\
	    compare-inum-mp"

#	    visit-volumes		\


#	    discon-create		\
#	    discon-echo			\
#	    discon-touch1		\
#	    discon-touch2		\
#	    discon-mkdir		\
#	    discon-mkdir2		\
#	    discon-tar1			\
#	    discon-tar2			\

# 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"; for a in "$ARLA_TESTS"; do echo $a ; done;
	echo "milko tests available: $linebreak"; for a in "$MILKO_TESTS"; do echo $a ; done;
	exit 0;;
  -version|--version) echo "$0 $Id: run-tests.in,v 1.66 2000/07/01 14:45:28 assar 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=
exitval=0

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-`date +%Y-%m-%d-%H-%M-%S`-$$"
  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"; 
        exitval=$savedres
     else
	test "X$VERBOSE" != "X" && echo "Test $a succeeded, tmpdir is removed"
	${objdir}/rm-rf $tmpdir
     fi
     test "X$VERBOSE" != "X" && echo "Done test $a."
  fi
  test "X${PRINT_CACHESIZE}" = Xyes && $objdir/../appl/fs/fs calculate
done

wait
date

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

exit $exitval