#!/bin/sh
##
## This script sets up the envirnoment as needed.
## So, run this script instead of running these tests directly.
##
## To run a subset of tests, pass them as script arguments.
##
## This script creates temporary files in a subdirectory of
## $TMPDIR (defaulting to /tmp if $TMPDIR is unset).
##

# The bulk of this file is based on the "check-TESTS" target
# in a Makefile.in generated by automake, which carries the
# following two notices:

# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
# 2003, 2004, 2005  Free Software Foundation, Inc.
# This Makefile.in is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.

# End of legal notices

if (/sbin/lsmod | grep '^blcr ' > /dev/null 2>&1); then
  true
else
  echo   '#############################################################'
  echo   '#### BLCR modules are not loaded.  Cannot run the tests! ####'
  echo   '#### You must insmod/modprobe the following modules as   ####'
  echo   '#### root (in order) before you can run the test suite.  ####'
  echo   '####    blcr_imports   blcr_vmadump   blcr               ####'
  echo   '#############################################################'
  exit 1
fi
#
init_dir=`pwd`
cr_testsdir=`dirname $0`
case "$cr_testsdir" in
 /*) ;;
 *) cr_testsdir="$init_dir/$cr_testsdir";;
esac
export cr_testsdir
tmp_dir=`mktemp -d "${TMPDIR:-/tmp}/blcrtests.XXXXXXXXXX"` || exit 1
cd $tmp_dir
#
export LIBCR_DISABLE_NSCD=1
top_srcdir="INVALID"; export top_srcdir
cr_checkpoint="/usr/bin/cr_checkpoint"; export cr_checkpoint
cr_restart="/usr/bin/cr_restart"; export cr_restart
cr_run="/usr/bin/cr_run"; export cr_run
cr_pwd="/bin/pwd"; export cr_pwd
count=0; FAIL=0; PASS=0; SKIP=0;
if test $# -eq 0; then
  list="atomics bug2524 cr_run cr_targ cr_targ2 cr_omit dlopen bug2003 run_on save_exe save_priv save_share save_all reloc_exe reloc_file reloc_fifo reloc_dir reloc_all clobber stage0001.st stage0002.st stage0003.st stage0004.st critical_sections.st replace_cb.st failed_cb.st failed_cb2.st pid_in_use.st cs_enter_leave.st cs_enter_leave2.st cr_tryenter_cs.st stopped.st edeadlk.st pid_restore.st simple.ct simple_pthread.ct cwd.ct dup.ct filedescriptors.ct pipe.ct named_fifo.ct cloexec.ct get_info.ct orphan.ct overlap.ct child.ct mmaps.ct hugetlbfs.ct readdir.ct dev_null.ct cr_signal.ct linked_fifo.ct sigpending.ct dpipe.ct forward.ct hooks.ct math.ct sigaltstack.ct prctl.ct lam.ct nscd.ct"
else
  list="$*"
fi
#
for tst in $list; do
  $cr_testsdir/$tst
  case $? in
    0)  result=PASS; count=`expr $count + 1`;;
    77) result=SKIP;;
    *)  result=FAIL; count=`expr $count + 1`;;
  esac
  echo "${result}: $tst"
  eval "$result=\`expr \$$result + 1\`"
done
if test $FAIL -eq 0; then
  banner="All $count tests passed"
else
  banner="$FAIL of $count tests failed"
fi
dashes="$banner"
if test $SKIP -ne 0; then
  skipped="($SKIP tests were not run)"
  if test `echo "$banner"|wc -c` -lt `echo "$skipped"|wc -c`; then
    dashes="$skipped"
  fi
fi
dashes=`echo "$dashes" | sed s/./=/g`
echo "$dashes"
echo "$banner"
if test "$SKIP" -ne 0; then
  echo "$skipped"
fi
echo "$dashes"
cd $init_dir
sleep 2 # avoid NFS "issues"
rm -Rf $tmp_dir
test $FAIL -eq 0
exit $?
