#!/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:  Start ds_system\n"
	$echo "ds_system can be solaris, suse64, hpux ou nt"
	exit 1
fi

SERV_NAME=devTest
case $1 in
hpux )
	BASE_DIR=/segfs/tango/tools/test_system/ref/device/bin/hpux10.2
	DIR=hpux10.2
	TEST_BASE=/segfs/tango/tools/test_system/ref/test-hpux
	;;
#
solaris )
	BASE_DIR=/segfs/tango/tools/test_system/ref/device/bin/solaris
	DIR=solaris
	TEST_BASE=/segfs/tango/tools/test_system/ref/test-solaris
	;;
#
suse64 )
	BASE_DIR=/segfs/tango/tools/test_system/ref/device/bin/suse64
	DIR=suse64
	TEST_BASE=/segfs/tango/tools/test_system/ref/test-linux
	;;
#
nt )
	BASE_DIR=/segfs/tango/tools/test_system/ref/device/bin/solaris
	DIR=solaris
	TEST_BASE=/segfs/tango/tools/test_system/ref/test-solaris
	;;
#
* )
	$echo "\nunknown device server system"
	exit 1
	;;
esac

SERV_PATH=$BASE_DIR/$SERV_NAME

$echo "Testing server startup"
#
# Starting a server without instance name
#

$echo "   Starting a server without instance name --> \c"
$SERV_PATH >/tmp/ds.out 2>&1
if test $? -eq 0
then
	$echo "Failed"
	exit 1
else
	$echo "OK!"
fi

#
# Starting a server without TANGO_HOST being defined
#

$echo "   Starting a server without TANGO_HOST being defined --> \c"
unset TANGO_HOST
$SERV_PATH api >>/tmp/ds.out 2>&1
if test $? -eq 0
then
	$echo "Failed"
	exit 1
else
	$echo "OK!"
fi

#
# Starting a server with an incorrect TANGO_HOST
#

$echo "   Starting a server with a wrong TANGO_HOST --> \c"
export TANGO_HOST=tango
$SERV_PATH api >>/tmp/ds.out 2>&1
if test $? -eq 0
then
	$echo "Failed"
	exit 1
fi

export TANGO_HOST=20000
$SERV_PATH api >>/tmp/ds.out 2>&1
if test $? -eq 0
then
	$echo "Failed"
	exit 1
else
	$echo "OK!"
fi

#
# Starting a server with TANGO_HOST set to a host without db server
#

$echo "   Starting a server with no database server --> \c"
export TANGO_HOST=truc:20000
$SERV_PATH api >>/tmp/ds.out 2>&1
if test $? -eq 0
then
	$echo "Failed"
	exit 1
else
	$echo "OK!"
fi

#
# Starting a server with an undefined instance name
#

$echo "   Starting a server an undefined instance name --> \c"
export TANGO_HOST=tango:20000
$SERV_PATH to >>/tmp/ds.out 2>&1
if test $? -eq 0
then
	$echo "Failed"
	exit 1
else
	$echo "OK!"
fi

#
# Starting two times the same server
#

$echo "   Starting two times the same server --> \c"
export TANGO_HOST=tango:20000
$SERV_PATH api >/dev/null 2>&1 &
PID=`ps -e|grep $SERV_NAME|awk '{print $1}'`
sleep 7
$SERV_PATH api >>/tmp/ds.out 2>&1
if test $? -eq 0
then
	$echo "Failed"
	exit 1
else
	$echo "OK!"
fi

#
# Starting two times the same server with one blocked
#

$echo "   Starting two times the same server with one being blocked --> \c"
$TEST_BASE ServSleep.tst 1>/dev/null 2>&1 &
sleep 7
$SERV_PATH api >>/tmp/ds.out 2>&1
if test $? -eq 0
then
	$echo "Failed"
	exit 1
else
	$echo "OK!"
fi

#
# Awfully killed the server
#

kill -9 $PID

#
# Restart the server
#

$echo "   Restart a server after a kill -9 --> \c"
$SERV_PATH api >>/tmp/ds.out 2>&1 &
$echo "OK!"
sleep 3

#
# Compare output file
#

$echo "   Checking servers output messages --> \c"
rm -f /tmp/difflog
THEO_FILE=$DIR/Start.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 1
fi	

#
#
#

PID=`ps -e|grep $SERV_NAME|awk '{print $1}'`
kill $PID
