#!/bin/sh

# This shell script is a helper script to the sysbem balance test's run
# script.  It is necessary to capture the exit status of the test being
# run.  A wait cannot be used by run to capture the exit status because
# there are multiple background jobs being waited on.  This script captures
# the exit status of a single test and writes it to file descriptor 3, which
# has been redirected by run to a temporary file.

# Script entry: $1=test, $2=path, $3=numNodes, $4=partition

exitCode=0                     # Successful termination
testError=1                    # Test failed to terminate correctly
miscError=2                    # SAT or run script failures

# check for valid arguements being passed in

if test $# -ne 4
then
   echo Invalid arguements passed to $0 1>&2

   echo $miscError 1>&3

   exit $miscError
fi

# make sure arg 2 is a valid directory

if test ! -d $2
then
   echo Unable to change to directory \"$2\" 1>&2

   echo $miscError 1>&3

   exit $miscError
fi

cd $2

# check for a run script in specified directory

if test -x $2/run
then
   $2/run $4

   echo $? 1>&3

   exit $exitCode
else
   echo run file not found for test \"$1\" in \"$2\"

   echo $miscError 1>&3

   exit $miscError
fi

exit $exitCode
