#! /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="-pg -mvhome"
#
# Some people don't include "." in their path (! in case an ls trojan horse,
# I guess - if someone does that to you, you have bigger problems).  This
# code tests to see if you have a path to mpirun; if not, it tries ./mpirun.
IFS="${IFS= 	}"; saveifs="$IFS"; IFS="${IFS}:"
for dir in $PATH ; do 
    if [ -x $dir/mpirun ] ; then
        mpirun="mpirun"
        break
    fi
done
IFS="$saveifs"
if [ -z "$mpirun" -a -x "./mpirun" ] ; then
    mpirun=./mpirun
fi
if [ -z "$mpirun" ] ; then
    echo "No mpirun in path.  Testing can not proceed."
    exit 1
fi
#
# If the programs are not available, run make.
#
runtests=1
makeeach=0
quiet=0
MAKE="make"
for arg in "$@" ; do
    case $arg in 
	-checkonly )
	runtests=0
	;;
        -margs=*)
	margs=`echo $arg | sed 's/-margs=//'`
	args="$args $margs"
	;;
	-small)
	makeeach=1
	;;
	-quiet)
	shift
	quiet=1
	;;
	-help|-u)
	echo "runtests [-checkonly] [-margs='...'] [-small]"
	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."
	exit 1
	;;
	*)
	if test -n "$arg" ; then
   	    echo "runtests: Unknown argument ($arg)"
	    exit 1
        fi
	;;
    esac
done

MakeExe() {
    if [ ! -x $1 ] ; then
	$MAKE $1
        if [ ! -x $1 ] ; then 
	    echo "Could not build executable $1; aborting tests"
	    exit 1
        fi
    fi
}
CleanExe() {
    if [ $makeeach = 1 ] ; then
	/bin/rm -f $1 $1.o
    fi
}
# Output marker
OutTime() {
    if [ $quiet = 0 ] ; then
	if [ -z "$hostname" ] ; then
	    hostname=`hostname`
	fi
	d=`date`
	echo "$hostname : $d"
    fi
}

if [ ! -x cart -a $makeeach = 0 ] ; then
    $MAKE default
fi

testfiles=""
if [ $runtests = 1 ] ; then

OutTime
testfiles="$testfiles cart.out"
/bin/rm -f cart.out
MakeExe cart
echo '**** Testing MPI_Cart_create etc ****' >> cart.out
echo '**** Testing MPI_Cart_create etc ****' 
$mpirun $args -np 4 cart $* >> cart.out 2>&1
echo '**** Testing MPI_Cart_create etc ****' >> cart.out
CleanExe cart
#
OutTime
testfiles="$testfiles cart2.out"
/bin/rm -f cart2.out
MakeExe cart2
echo '**** Testing MPI_Cart_coords etc ****' >> cart2.out
echo '**** Testing MPI_Cart_coords etc ****' 
$mpirun $args -np 4 cart2 $* >> cart2.out 2>&1
echo '**** Testing MPI_Cart_coords etc ****' >> cart2.out
CleanExe cart2
#
OutTime
testfiles="$testfiles cartmap.out"
/bin/rm -f cartmap.out
MakeExe cartmap
echo '**** Testing MPI_Cart_map etc ****' >> cartmap.out
echo '**** Testing MPI_Cart_map etc ****' 
$mpirun $args -np 4 cartmap $* >> cartmap.out 2>&1
echo '**** Testing MPI_Cart_map etc ****' >> cartmap.out
CleanExe cartmap
#
OutTime
testfiles="$testfiles cartorder.out"
/bin/rm -f cartorder.out
MakeExe cartorder
echo '**** Testing MPI_Cart_create etc (2) ****' >> cartorder.out
echo '**** Testing MPI_Cart_create etc (2) ****' 
$mpirun $args -np 4 cartorder $* >> cartorder.out 2>&1
echo '**** Testing MPI_Cart_create etc (2) ****' >> cartorder.out
CleanExe cartorder
#
OutTime
testfiles="$testfiles graphtest.out"
/bin/rm -f graphtest.out
MakeExe graphtest
echo '**** Testing MPI_Graph_create etc ****' >> graphtest.out
echo '**** Testing MPI_Graph_create etc ****' 
$mpirun $args -np 4 graphtest $* >> graphtest.out 2>&1
echo '**** Testing MPI_Graph_create etc ****' >> graphtest.out
CleanExe graphtest
#
# Run Fortran tests ONLY if Fortran available
if [ 1 = 1 ] ; then 
    echo "FORTRAN TESTS"
    #
    #
    OutTime
    testfiles="$testfiles cartf.out"
    /bin/rm -f cartf.out
    MakeExe cartf
    echo '*** Testing cart from Fortran ***'
    echo '*** Testing cart from Fortran ***' >> cartf.out
    $mpirun $args -np 4 cartf "$@" >> cartf.out 2>&1
    echo '*** Testing cart from Fortran ***' >> cartf.out
    CleanExe
    #
    OutTime
    testfiles="$testfiles cart1f.out"
    /bin/rm -f cart1f.out
    MakeExe cart1f
    echo '*** Testing cart1 from Fortran ***'
    echo '*** Testing cart1 from Fortran ***' >> cart1f.out
    $mpirun $args -np 4 cart1f "$@" >> cart1f.out 2>&1
    echo '*** Testing cart1 from Fortran ***' >> cart1f.out
    CleanExe
    #
    OutTime
    testfiles="$testfiles cart2f.out"
    /bin/rm -f cart2f.out
    MakeExe cart2f
    echo '*** Testing cart2 from Fortran ***'
    echo '*** Testing cart2 from Fortran ***' >> cart2f.out
    $mpirun $args -np 4 cart2f "$@" >> cart2f.out 2>&1
    echo '*** Testing cart2 from Fortran ***' >> cart2f.out
    CleanExe
    #
    OutTime
    testfiles="$testfiles twod.out"
    /bin/rm -f twod.out
    MakeExe twod
    echo '*** Testing twod from Fortran ***'
    echo '*** Testing twod from Fortran ***' >> twod.out
    $mpirun $args -np 2 twod "$@" >> twod.out 2>&1
    echo '*** Testing twod from Fortran ***' >> twod.out
    CleanExe
fi
else
    # Just run checks
    testfiles=*.out
fi
#
echo '*** Checking for differences from expected output ***'
/bin/rm -f topol.diff
nodiff=1
for file in $testfiles ; do
    stdfile=`basename $file .out`.std
    if [ -s $stdfile ] ; then
        if diff -b $file `basename $file .out`.std > /dev/null ; then
	    true
        else
	    echo "Differences in `basename $file .out`" >> topol.diff
	    diff -b $file `basename $file .out`.std >> topol.diff
	    nodiff=0
	fi
    else
        echo "Can not find file $stdfile to compare against for test `basename $file .out`"
    fi
done
if [ -s topol.diff ] ; then
   cat topol.diff
elif [ $nodiff = 1 ] ; then
   echo "-- No differences found; test successful"
fi
exit 0
