#!/bin/ksh

#
# remove different echo command behaviour on different OS
#
if test "`echo -e xxx`" = "xxx"
then
    echo="echo -e"
else
    echo=echo
fi

if [ $# = 0 ]
then
	$echo "\nusage:  Tango_test ds_system\n"
	$echo "ds_system can be solaris, suse64, hpux ou nt"
	exit 1
fi
#
#
# 
case $1 in
hpux )
	DIR=hpux10.2
	;;
#
solaris )
	DIR=solaris
	;;
#
suse64 )
	DIR=suse64
	;;
#
nt )
	DIR=nt
	;;
#
* )
	$echo "\nunknown device server system"
	exit 1
	;;
esac

case `/bin/uname` in
Linux )
	TEST_BASE=/segfs/tango/tools/test_system/ref/test-linux
	;;
SunOS )
	TEST_BASE=/segfs/tango/tools/test_system/ref/test-solaris
	;;
HP-UX )
	TEST_BASE=/segfs/tango/tools/test_system/ref/test-hpux
	;;
esac


F_NAME_BASE=`date '+%Y-%m-%d'`.html
F_ERR=/tmp/tst.err
SERV_NAME=devTest

#
# The function executing the work.
# There is one parameter which is the sequence file name
#

run_test () {
	rm -f $F_ERR 2>/dev/null
	$TEST_BASE $1.tst 1>/dev/null 2>$F_ERR

	if test -s $F_ERR
	then
		$echo "Failed !!"
		$echo "Syntax error in test sequence file : $1"
		cat $F_ERR
		rm -f $F_ERR
		exit
	fi
	rm -f $F_ERR 2>/dev/null

	F_REPORT=$1-$F_NAME_BASE
	grep FAIL $F_REPORT 1>/dev/null
	if test $? -eq 0
	then
		$echo "Failed !!"
		$echo "Check output file $F_REPORT"
		exit
	else
		$echo "OK!"
	fi
}

run_compare_test () {
	rm -f $F_ERR
	$TEST_BASE $1.tst 1>/dev/null 2>$F_ERR

	if test -s $F_ERR
	then
		$echo "Failed !!"
		$echo "Syntax error in test sequence file : $1"
		cat $F_ERR
		rm -f $F_ERR
		exit
	fi
	rm -f $F_ERR 2>/dev/null

	F_REPORT=$1-$F_NAME_BASE
	grep FAIL $F_REPORT 1>/dev/null
	if test $? -eq 0
	then
		$echo "Failed !!"
		$echo "Check output file $F_REPORT"
		exit
	else
		rm -f /tmp/difflog
		THEO_FILE=$1.out
		diff $THEO_FILE /tmp/ds.out > /tmp/difflog
		if test $? -eq 0
		then
			$echo "OK!"
			rm -f /tmp/ds.out
			rm -f /tmp/difflog
		else
			$echo "Failed!"
			$echo "Server outputs are different than theoritical outputs"
			$echo "Server outputs file = /tmp/ds.out"
			$echo "Theoritical output file = $THEO_FILE"
			exit
		fi
	fi
}

$echo "Testing device server features"

#
# First, test types
#

$echo "   Testing Tango data types --> \c"
run_test Types

#
# Test miscellaneous features
#

$echo "   Testing Tango device miscellaneous feature --> \c"
run_test $DIR/Misc

#
# Test miscellaneous features
#

$echo "   Testing Tango command query features --> \c"
run_test CmdQuery

#
# Test blackbox
#

$echo "   Testing Tango device black box --> \c"
run_test $DIR/Blackbox

#
# Test the allways_hook method execution
#

$echo "   Testing execution of the allways_executed method --> \c"
run_compare_test AllwaysHook

#
# Some exception cases
#

$echo "   Testing some Tango exception cases --> \c"
run_test Exception

#
# Test miscellaneous features of the admin. device
#

$echo "   Testing Tango administration device miscellaneous feature --> \c"
run_test $DIR/DServMisc

#
# Test admin. device command
#

$echo "   Testing Tango administration device commands --> \c"
run_compare_test DServCmd

#
# Test device signal features
#

$echo "   Testing device signal features --> \c"
run_test Signal1
PID=`ps -e|grep $SERV_NAME|awk '{print $1}'`
#
#   For suse, send the signal only to the signal thread
#
if [ $DIR = suse64 ]
then
	x=0
	for i in $PID
	do
		let x=x+1
		if [ $x = 3 ]
		then
			kill -14 $i
		fi
	done
else
	kill -14 $PID
fi
$echo "   Checking device signal handler execution --> \c"
run_compare_test Signal2

#
# Test signal within their own handler (only for Linux)
#
if [ $DIR = suse64 ]
then
	$echo "   Testing device signal within their own signal handler --> \c"
	run_test $DIR/SignalOwn1
	x=0
	for i in $PID
	do
		let x=x+1
		if [ $x = 1 ]
		then
			kill -14 $i
		fi
		if [ $x = 3 ] 
		then
			kill -14 $i
		fi
	done
	$echo "   Checking device signal handler execution --> \c"
	run_compare_test $DIR/SignalOwn2  
fi
 
#
# Test class signal features
#

$echo "   Testing class signal features --> \c"
run_test ClassSignal1
PID=`ps -e|grep $SERV_NAME|awk '{print $1}'`
#
#   For suse, send the signal only to the signal thread
#
if [ $DIR = suse64 ]
then
	x=0
	for i in $PID
	do
		let x=x+1
		if [ $x = 3 ]
		then
			kill -14 $i
		fi
	done
else
	kill -14 $PID
fi
$echo "   Checking class signal handler execution --> \c"
run_compare_test ClassSignal2

#
# Test class and device signal features
#

$echo "   Testing class and device signal features --> \c"
run_test ClassDevSignal1
PID=`ps -e|grep $SERV_NAME|awk '{print $1}'`
#
#   For suse, send the signal only to the signal thread
#
if [ $DIR = suse64 ]
then
	x=0
	for i in $PID
	do
		let x=x+1
		if [ $x = 3 ]
		then
			kill -14 $i
		fi
	done
else
	kill -14 $PID
fi
$echo "   Checking class and device signal handler execution --> \c"
run_compare_test ClassDevSignal2

#
# Test commands implemented using template method
#

$echo "   Testing commands implemented using template method --> \c"
run_test TemplCmd

#
# Test commands implemented using template method
#

$echo "   Testing miscellaneous utilities of the Tango::Util class --> \c"
run_test MiscUtil

#
# Test attributes
#

$echo "   Testing attributes data transfer --> \c"
run_test Attr

#
# Test attributes
#

$echo "   Testing attributes features --> \c"
run_test AttrMisc

#
# Kill the server
#

PID=`ps -e|grep $SERV_NAME|awk '{print $1}'`
if [ $DIR = suse64 ]
then
	x=0
	for i in $PID
	do
		let x=x+1
		if [ $x = 3 ]
		then
			kill $i
		fi
	done
else
	kill $PID
fi

