#!/bin/sh

# Script to setup environment correctly to run test suites
# Configure if not previously done
#
# Christopher Yeoh, IBM
#
# This is $Revision: 1.20 $
# 
# $Log: run_tests,v $
# Revision 1.20  2003/11/11 06:53:54  cyeoh
# Adds generic capability to have runtime parameterisations
#
# Revision 1.19  2003/04/14 05:08:53  cyeoh
# Which linker is required is now setup during the build of the test suites
#
# Revision 1.18  2003/04/14 01:17:04  cyeoh
# Make the block special filename configurable from run_tests
#
# Revision 1.17  2003/04/10 07:03:41  cyeoh
# do better job of finding which test suite to run
#
# Revision 1.16  2003/04/10 00:26:14  cyeoh
# improve error message to be cross platform correct
#
# Revision 1.15  2003/01/10 05:15:36  cyeoh
# Add linker check for ppc, ppc64, s390 and zSeries
#
# Revision 1.14  2002/12/03 04:53:27  cyeoh
# Add hook to do once-off configuration
#
# Revision 1.13  2002/11/21 05:57:28  cyeoh
# be more precise in grepping for config in case another parameter
# exists which is a superset of what we are looking for
#
# Revision 1.12  2002/11/15 03:03:54  cyeoh
# Fix typo in script
#
# Revision 1.11  2002/07/11 01:07:51  cyeoh
# quote variable correctly
#
# Revision 1.10  2002/06/13 00:43:52  cyeoh
# Fixes for running on ia64
#
# Revision 1.9  2002/06/12 00:48:24  cyeoh
# Fix cut and paste bug
#
# Revision 1.8  2002/04/07 13:26:14  cyeoh
# Adds better error checking/reporting to script
#
# Revision 1.7  2002/03/12 04:07:02  cyeoh
# Add check for ld-lsb.so.1 before attempting to run the test suites
#
# Revision 1.6  2002/03/12 03:07:46  cyeoh
# No longer need to remove these files as removal is now correctly
# handled by the test suite itself
#
# Revision 1.5  2002/03/12 02:51:48  cyeoh
# Add marker to represent version number of binary build
#
# Revision 1.4  2002/03/11 02:23:54  cyeoh
# Adds handle to journal file to recognise test suite runs from rpm binary
#
# Revision 1.3  2002/02/12 06:30:49  cyeoh
# Add removal of temp files ocassionally left behind by test suites
#
# Revision 1.2  2001/12/13 06:07:49  cyeoh
# reask FHS questions as the answers are dependent on the system being tested
# and need to be checked manually
#
# Revision 1.1  2001/11/30 02:44:52  cyeoh
# Initial checkin
#
#

# Template marker for version in journal file
BUILD_VERSION="2.0-2 (i386)"

# Useful functions
#----------------------------------------------------------------------
# Run testset specific config scripts
function run_testset_config_script()
{
  # Test argument count
  if test $# -lt 1 
  then
    echo "$func_name: Missing argument"; return 1 # unresolved
  fi

  for i in scripts/*; do
    if [ -x $i/$1.sh ]; 
      then $i/$1.sh $2;
      if [ $? -ne 0 ]; then
        echo "Aborting run"
	exit 1
      fi
    fi;
  done;
}

function config_param()
{

DEFAULT=`grep "^$1=" TESTROOT/tetexec.cfg | cut -d= -f2`

printf "$2 ("

if [ -n "$DEFAULT" ]; 
then
  printf "$DEFAULT"
fi

printf ")? "
read cmd

if [ -z "$cmd" ]; then
  cmd=$DEFAULT
fi

# Write to temporary file
echo "$1=$cmd" >> /tmp/newparam.$$

}


# Do run time configuration
function config_run()
{
  rm -f /tmp/newparam.$$
  config_param VSX_OPER "Name of person running tests"
  config_param VSX_ORG  "Organisation"
  config_param VSX_SYS "Test System"
  config_param VSX_BLKDEV_FILE "Block special filename"
  echo "VSX_NAME=LSB Certification Version $BUILD_VERSION" >> /tmp/newparam.$$

	run_testset_config_script runtime-parameterisation /tmp/newparam.$$

  mv /home/tet/test_sets/TESTROOT/tetexec.cfg /home/tet/test_sets/TESTROOT/tetexec.cfg.old
  cat /home/tet/test_sets/TESTROOT/tetexec.cfg.old | /home/tet/bin/editcfg.sh /tmp/newparam.$$ > /home/tet/test_sets/TESTROOT/tetexec.cfg

  # Do test set specific configuration
  # This is for configurations which only need to be done once per
  # installation
  run_testset_config_script runtime-config

  touch .configured
}

#----------------------------------------------------------------------
# Main bit

# Check that we have the TET Environment initialised
if [ -z "$TET_ROOT" ]; then
  echo
  echo "TET/VSXgen environment is not initialised. "
  echo "You may need to execute the profile file first."
  exit 1
fi

# (Optional) Configuration of test suite
if [ $# -gt 0 ] || ! [ -f .configured ]
then 
  config_run
fi

LDSO=/lib/ld-lsb.so.2

# Check for the lsb linker
if [ ! -f $LDSO ]; then
  echo The LSB loader $LDSO does not exist. The
  echo test suites will not run without it. If you are running this
  echo on a non LSB compliant system and still wish to execute the 
  echo test suites then creating a symlink for $LDSO to
  echo /lib/ld-linux.so.2 before re-running this program is most
  echo likely the correct thing to do.
  echo
  exit 1
fi

# Clean up locks (need directory set first!)
if test ! -d "scripts/locks"
then
    mkdir scripts/locks
else
    rm -f scripts/locks/*
fi

# Do any test set specific setups
run_testset_config_script resetup

# Run the test suites
echo "----------------------------------------------------------------------"
echo "Executing the test suites"
echo "It is not unusual for these test suites to take several hours to run."
echo 
echo


tcc -p -e -s $HOME/scen.exec test_sets

if [ $? -ne 0 ]; then
  echo "Failed to execute the test suites correctly."
  echo "Either they are not installed properly or you may"
  echo "be missing the lsb linker $LDSO on your system."
  echo 
  exit 1
fi

echo ""

# Produce a report of the results
REPORT_NAME=report.`date  +"%Y%m%d%H%m"`
(cd results; awk -f `which vres` `ls -d 0* | tail -1`/journal)
echo
echo "Producing formal report ($HOME/results/$REPORT_NAME)"
(cd results; vrpt `ls -d 0* | tail -1`/journal >$HOME/results/$REPORT_NAME)

echo

echo "Finished."

