#! /bin/sh
#
# Run some of the tests.  If any arguments are provided, pass them to the
# test programs.
#
# -mvhome is needed for the ANL SP, and is ignored by others
args=
device=
top_srcdir=..
srcdir=../profile
MPICH_VERSION=
STOPFILE=${MPITEST_STOPTEST:-"$HOME/.stopmpichtests"}
mpirun=${MPIRUN:-"mpirun"}
MAKE="make"
MPIRUNMVBACK=""
#
# Load basic procedures
. ${top_srcdir}/runbase
#
# Set mpirun to the name/path of the mpirun program
FindMPIRUN
if [ -z "$mpirun" ] ; then
    echo "No mpirun in path.  Testing cannot proceed."
    exit 1
fi
#
# If the programs are not available, run make.
runtests=1
makeeach=0
writesummaryfile=no
quiet=0
check_at_once=1
MAKE="make"
for arg in "$@" ; do
    case $arg in 
	-checkonly)
	runtests=0
	;;
        -margs=*)
	margs=`echo $arg | sed 's/-margs=//'`
	args="$args $margs"
	;;
	-summaryfile=*)
	writesummaryfile=yes
	summaryfile=`echo A$arg | sed 's/A-summaryfile=//'`
	;;
	-echo)
	set -x
	;;
	-small)
	makeeach=1
	;;
	-quiet)
	shift
	quiet=1
	;;
	-atend)
	check_at_once=0
	;;
	-help|-u)
	echo "runtests [-checkonly] [-margs='...'] [-atend]"
	echo "run tests in this directory.  If -checkonly set, just run"
	echo "the differences check (do NO rerun the test programs)."
	echo "If -margs is used, these options are passed to mpirun."
	echo "If -small is used, the examples are built, run, and deleted."
	echo "If -atend is used, the success of the tests is checked only"
	echo "at the end of the test rather than also after each test."
	exit 1
	;;
	*)
	if test -n "$arg" ; then
	    echo "Passing remaining arguments to programs ($*)"
	    break
        fi
	;;
    esac
done

# If the programs are not available, run make.
if [ ! -x sendrecv -a $makeeach = 0 -a $runtests = 1 ] ; then
    $MAKE default
fi

testfiles=""
if [ $runtests = 1 ] ; then
echo '**** Testing MPI Profiling routines ****'

RunTest ptest 1 "**** Testing MPI_Pcontrol ****"

RunTest colluses 4 "**** Testing for PMPI in Allreduce ****"

else
    # Just run checks
    testfiles=`echo *.out`
    if test "$testfiles" = "*.out" ; then
	echo "No output files remain from previous test!"
	exit 1
    fi
fi
#
echo '*** Checking for differences from expected output ***'
CheckAllOutput profile.diff
exit 0



