#! /bin/sh
#
# Copyright (c) 2003 by Intel Corp.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  This
# file and program are licensed under a BSD style license.  See
# the Copying file included with the OpenHPI distribution for
# full licensing terms.
#
# Authors:
#     Kevin Gao <kevin.gao@intel.com>

BASEDIR=src
LOGFILE=log/logfile
CURLOG=log/current_log
TMPLOG=log/hpi_log.tmp
Verbose=0
DIFF=0

usage()
{
    cat <<EOF 
Usage: $0 [OPTION] [init|session|resource|eventlog|event|
		   rdr|sensor|control|inventory|watchdogtimer|
		   hotswap|config|reset|all|help]

Mandatory arguments to long options are mandatory for short options too.
-v, --verbose	   Give the detailed test information.
-d, --diff	   Give the different info between new, and old log info.
	
EOF
}

runtests()
{
	for test in `ls -d $1`; do
		POSIX_TARGET=$test make build-tests VERBOSE=$Verbose
		POSIX_TARGET=$test make run-tests VERBOSE=$Verbose
	done
}
echo -e "\n****************Start testing****************" >> $LOGFILE

if [ -z $1 ]; then
	usage
	exit 1
fi

CMD=$1
if [ $CMD = "-d" ] || [ $CMD = "--diff" ]; then
	CMD=$2
	cp $CURLOG $TMPLOG > /dev/null 2>&1
	DIFF=1
fi

if [ $CMD = "-v" ] || [ $CMD = "--verbose" ]; then
	CMD=$2
	Verbose=1
fi

case $CMD in
init)
	echo "Executing init tests"
	if [ $DIFF -eq 1 ]; then
	        runtests "$BASEDIR/init" > $CURLOG
        else
                runtests "$BASEDIR/init" | tee $CURLOG
        fi

	;;

session)
	echo "Executing session management tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/session_open" > $CURLOG
        else
                runtests "$BASEDIR/session_open" | tee $CURLOG
        fi

	;;

resource)
	echo "Executing resource management tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/resource_discover" > $CURLOG
                runtests "$BASEDIR/rpt_entry_process" >> $CURLOG
                runtests "$BASEDIR/self_resourceID_get" >> $CURLOG
                runtests "$BASEDIR/entity_schema_get" >> $CURLOG
        else
                runtests "$BASEDIR/resource_discover" | tee $CURLOG
                runtests "$BASEDIR/rpt_entry_process" | tee -a $CURLOG
                runtests "$BASEDIR/self_resourceID_get" | tee -a $CURLOG
                runtests "$BASEDIR/entity_schema_get" | tee -a $CURLOG
        fi

	;;

eventlog)
	echo "Excuting event log management tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/event_log*" > $CURLOG
        else
                runtests "$BASEDIR/event_log*" | tee $CURLOG
        fi
	;;

event)
	echo "Excuting events tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/events" > $CURLOG
        else
                runtests "$BASEDIR/events" | tee $CURLOG
        fi
	;;

rdr)
	echo "Excuting resource data record(RDR) repository management tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/rdr_get" > $CURLOG
        else
                runtests "$BASEDIR/rdr_get" | tee $CURLOG
        fi
	;;

sensor)
	echo "Excuting sensor function tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/sensor*" > $CURLOG
        else
                runtests "$BASEDIR/sensor*" | tee $CURLOG
        fi
	;;

control)
	echo "Excuting controls tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/control*" > $CURLOG
        else
                runtests "$BASEDIR/control*" | tee $CURLOG
        fi
	;;

inventory)
	echo "Excuting entity inventory data tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/entity_inventory_data" > $CURLOG
        else
                runtests "$BASEDIR/entity_inventory_data" | tee $CURLOG
        fi
	;;

watchdogtimer)
	echo "Excuting watch timer tests" 
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/watchdog_timer" > $CURLOG
        else
                runtests "$BASEDIR/watchdog_timer" | tee $CURLOG
        fi
	;;

hotswap)
	echo "Excuting managed hot swap tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/hotswap*" > $CURLOG
                runtests "$BASEDIR/resource_active" >> $CURLOG
                runtests "$BASEDIR/auto*" >> $CURLOG
                runtests "$BASEDIR/resource_power_state_process" >> $CURLOG
        else
                runtests "$BASEDIR/hotswap*" | tee $CURLOG
                runtests "$BASEDIR/resource_active" | tee -a $CURLOG
                runtests "$BASEDIR/auto*" | tee -a $CURLOG
                runtests "$BASEDIR/resource_power_state_process" | tee -a $CURLOG
        fi	
	;;

config)
	echo "Excuting configuration tests"
        if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/parm_control" > $CURLOG
        else
                runtests "$BASEDIR/parm_control" | tee $CURLOG
        fi
	;;

reset)
	echo "Excuting reset tests"
	if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR/resource_reset_state_process" > $CURLOG
        else
                runtests "$BASEDIR/resource_reset_state_process" | tee $CURLOG
        fi

	;;

all)	echo "Executing all tests"
 
        if [ $DIFF -eq 1 ]; then
                runtests "$BASEDIR" > $CURLOG
        else
                runtests "$BASEDIR" | tee $CURLOG
        fi
	;;

*)	usage
	rm -f $TMPLOG > /dev/null 2>&1
	exit 1
	;;
esac

if [ $DIFF -eq 1 ]; then
	diff -Nur $TMPLOG $CURLOG
	rm -f $TMPLOG > /dev/null 2>&1
	exit 0
fi

echo "****Tests Complete****" 
echo "****************Finish testing****************" >> $LOGFILE
