#!/bin/sh
#
# RUNTESTS [-h]...
#
# RETURNS:	Number of failed tests.
#
# CALLS: eval_oneprogram.sh [-h][-lk] <program>
#
# SOURCES: TESTCONF.sh
#
#

								USAGE_LONG='
#
# HOW TO ENTER A NEW TEST
#
# Copy the template file (T2.sh) XXX to the tests directory, and have
# the file begin with a 'T'.  All files in the tests directory
# begining with a 'T' are assumed to be a test.  See the file for
# further instructions
#
# HOW TESTS ARE RUN AND EVALUATED
#
# ...
# 
'

#
# Suggested improvement(s):
#	Run a given test against a running agent.
#	Display errors
#	interactively pick tests to run.
#	options arguments to pick tests to run.
#
#
# Variables:  (* = exported)
#  *SNMP_BASEDIR:  	  the source base directory for tests
#  *SNMP_UPDIR:  	  directory above where the test binaries live (-D option)

# Usage mess.  (No, it works.)
#
USAGE="Usage: `basename $0` [-h] [-i] [-v] [-V] [-s] [-T TESTNUMS] [-D bindir]"

usage() { 
    echo; 
    echo $USAGE;
    #cat <<BLIK | sed 's/^#//' | sed '1d' | $PAGER
    #$USAGE_LONG
    #BLIK
    exit 0
}

# defaults
# Fix path starting from this script's base.
SNMP_BASEDIR=`dirname $0` ; export SNMP_BASEDIR
cd $SNMP_BASEDIR
SNMP_UPDIR=..
interactive=no

while [ -n "$1" ]; do
    case "$1" in
	-h)
	    usage
	    exit
	    ;;

	-i)
	    interactive="yes"
	    ;;
	-v)
	    SNMP_VERBOSE=1
	    export SNMP_VERBOSE
	    ;;
	-V)
	    SNMP_VERBOSE=2
	    export SNMP_VERBOSE
	    ;;
	-s)
	    SNMP_SAVE_TMPDIR="yes"
	    export SNMP_SAVE_TMPDIR
	    ;;
	-D)
	    shift
	    SNMP_UPDIR="$1"
	    ;;
	-T)
	    shift
	    test_nums="$1"
	    ;;
	-a)
	    test_nums="all"
	    ;;

	-A)
	    shift
	    AGENT_FLAGS="$1"
	    export AGENT_FLAGS
	    ;;

    esac

    shift
done


PATH=.:..:${SNMP_UPDIR}/apps:${SNMP_UPDIR}/agent:$PATH
export PATH
#echo $PATH
SNMP_PATH=yes

#
# Source the testing configuration file
#

. TESTCONF.sh

# Hack: the above created a directory, now we have to nuke it and
# forget about it...  All for the convenience of the test writer.
rmdir $SNMP_TMPDIR
unset SNMP_TMPDIR
export SNMP_TMPDIR

#
# Switch to the testing directory, for ease of the client test packages.
#
cd ./tests

#------------------------------------ -o- 
# Globals.
#
PROGRAM=
ARGUMENTS="$*"

TMPFILE=$SNMP_TMPDIR/eval_suite.sh$$

testname=

success_count=0
failed_count=0

if [ "x$do_tests" = "x" ]; then
    #
    # List the tests in question
    #
    SNMP_HEADERONLY=yes
    export SNMP_HEADERONLY
    num=0
    for testfile in T*; do

	case $testfile in
	    # Skip backup files, and the like.
	    *~)     ;;
	    *.bak)  ;;
	    *.orig) ;;
	    *.rej)  ;;

	    # Do the rest
	    *)
		num=`expr $num + 1`
		if [ "x$interactive" != "xyes" -a "x$test_nums" = "x" ]; then
		    ECHO "$num:  "
		    eval_onescript.sh $testfile
		fi
		all_tests="$all_tests $num"
		all_files="$all_files $testfile"
		;;
	esac
    done
    unset SNMP_HEADERONLY
    export SNMP_HEADERONLY

    #
    # TODO: allow user to interactively pick the list of tests to run.
    #

    if [ "x$interactive" != "xyes" ]; then
	if [ "x$test_nums" = "x" ]; then
	    ECHO "Enter test numbers [all]: "
	    read inp
	else
	    if [ "x$test_nums" = "xall" ]; then
		inp=""
	    else
		inp="$test_nums"
	    fi
	fi
	if [ "x$inp" = "x" ]; then
	    do_tests="$all_files"
	else
	    a=1
	    set $all_files
	    while [ $a -le $num ]; do
		if echo " $inp " | grep " $a " > /dev/null; then
		    do_tests="$do_tests $1"
		fi
		shift
		a=`expr $a + 1`
	    done
	fi
    fi

    #echo Starting: Running tests $do_tests
fi

#
# Run the tests
#
num=1
for testfile in $do_tests; do
    dothisone="yes";
    if [ "x$interactive" = "xyes" ]; then
	SNMP_HEADERONLY=yes
	export SNMP_HEADERONLY

        ECHO "$num:  "
	eval_onescript.sh $testfile

	unset SNMP_HEADERONLY
	export SNMP_HEADERONLY

	ECHO "  Run test #$num (y/n) [y]? "
	read inp
	if [ "x$inp" = "xn" ]; then
	    dothisone=no
	fi
    fi
  
    if [ "x$dothisone" = "xyes" ]; then
	ECHO "  $num:  "
	eval_onescript.sh $testfile
	if [ $? = 0 ]; then
	    success_count=`expr $success_count + 1`
	else
	    failed_count=`expr $failed_count + 1`
	fi
    fi
    num=`expr $num + 1`
done

echo Summary: $success_count / `expr $failed_count + $success_count` succeeded.

exit $failed_count
