#! /bin/sh
# @(#)LOAD version 3.2 created 6/19/96
# @(#)Copyright (c), 1987, 1996 StatSci, Inc.  All rights reserved.
# Loop through key tests.

tmpdir=${TMPDIR:-/tmp}
TEMP=$tmpdir/LOAD.$$
MACHINE=${MACHINE-`$SHOME/cmd/HOSTNAME`}
PLATFORM=`$SHOME/cmd/PLATFORM`
DATA=.Data.$MACHINE
LOAD_DIR=./loop.tests
TEST_LIST=ALLFILES
S_SILENT_STARTUP=yes
program=Sqpe
export LOAD_DIR
export S_SILENT_STARTUP

while test $# != 0
do
    DIRS="$DIRS $1"
    shift
done
if test x"$DIRS" = x; then
    echo "Usage: LOAD dir1 [dir2 ... dirn]"
    exit 1
fi

# Set the Make flags properly for the MIPS machines
case $PLATFORM in
  DECSTATION|IRIS4D) 
    CFLAGS="-G 0"
    FFLAGS="-G 0"
    export CFLAGS FFLAGS
    # echo "I set CFLAGS=$CFLAGS and FFLAGS=$FFLAGS for MIPS ($PLATFORM)"
    ;;
  *) ;;
esac

echo; echo "========== Running dynamic load verification"
echo "           `date`"

TEST_FUNS_DIR=`pwd`/test_funs.$PLATFORM
export TEST_FUNS_DIR

# if [ ! -d $TEST_FUNS_DIR ]; then
#  echo Making utility functions...

mkdir $TEST_FUNS_DIR 2>/dev/null
S_WORK=$TEST_FUNS_DIR Splus < ./test_fns.q
rm -f $TEST_FUNS_DIR/.Audit $TEST_FUNS_DIR/_audit

#  echo End making utility functions.
# fi

TARGET_DATA_DIR=`pwd`/loop.tests/stat.att/Targets.$PLATFORM   # target fits
export TARGET_DATA_DIR
# if [ ! -d $TARGET_DATA_DIR ]; then
#  echo Loading target fits for comparison on the fly
    mkdir $TARGET_DATA_DIR 2>/dev/null
    (cd  $LOAD_DIR/stat.att/perm_src;
     S_WORK=$TARGET_DATA_DIR Splus < ./perm_src.q)
    rm -f $TARGET_DATA_DIR/.Audit $TARGET_DATA_DIR/_audit
#  echo Targets Loaded.
# fi

status=0
for DIR in $DIRS
do
    if [ ! -d $LOAD_DIR/$DIR ]; then
        echo Cannot find directory $LOAD_DIR/$DIR >&2
        exit 1
    fi

    test_status=0

    for file in `cat $LOAD_DIR/$DIR/$TEST_LIST`
    do
        # Make sure we have a clean .Data for each run.
        rm -rf $DATA
        mkdir $DATA
        cat << EOF > $TEMP
invisible(assign("test.ok", T, frame=0))
options(warn=1) # immediate
options(error=function() {print(traceback()); assign("test.ok", F, frame=0)})
attach("$TEST_FUNS_DIR", pos=100)
do.test.ssi("$LOAD_DIR/$DIR/$file")
if (! get("test.ok",frame=0)) q(1) else q()
EOF
        echo "---------Testing $file------------"
		this_status=0
        S_WORK=$DATA Splus $program 2>&1 < $TEMP || this_status=1
		if [ $this_status -ne 0 ]
		then echo "Test $file FAILED!"; test_status=1
		fi
    done

    if [ $test_status -ne 0 ]
    then echo "---------- A test in $DIR FAILED"; status=1
    fi
done
rm -f $TEMP
rm -f oldabc

echo
if [ $status -eq 0 ]
then echo "---------- Finished dynamic load tests: PASSED"
else echo "---------- Finished dynamic load tests: FAILED"
fi

exit $status
