:	'(c) Copyright	Empress Software Inc.	1983, 2003'

USAGE="

*** Usage ***	$0 [options] [codeset] [major [minor [item ...]]]
		$0 [options] [codeset] [major/minor[/item]]

Options are:

-help		print this help message
-abort		abort the runtest if there is a failure
-clean		clean up and ignore the test suite in testwork directory
-chk		run sh -x on dotest shell script (for debug runtest)
-eng[lish]	set MSLANG to english
-forcefail	same as savetest
-jap[anese]	set MSLANG to japanese
-keepcore	keep the core file when the test fails
-long		run tests which are labelled as long
-loop		use _iteration setting (useful for running 1 test)
-nloop N	run the test N times. It will ignore _iteration
		file unless -loop is used after this option.
		You may want to use this with -abort
-noloop		ignore the _iteration setting
-notify		print a message to the tty device if a test fails
-save[test]	save the test into testwork even if it runs successfully
-rcc TAERGET
-remote_cc TARGET
		use remote compiler to compile. TARGET is the xtarget.
		 e.g.	runtest -rcc arm-linux ms ql count
		 	runtest -remote_cc arm-linux ms ql count
-short		run tests which are labelled as short
-time		print start time and end time of each test item
-xloop N	run the test N times. It will ignore _iteration
		file unless -loop is used after this option.
		If there is a failure of a test, it will skip
		the remaining iterations and move on to the next
		test.


Options for db server:

-server			run db server test
-node nodename		remote node name (implies db server test)
-rmtpath path		EMPRESSPATH on the remote machine
-tcp			use tcp
-udp			use udp
-nserver N		set number of db server process to N
"

if test "$EMPRESSPATH" = "" 
then
	echo "EMPRESSPATH not set"
	exit 1

elif test ! -d $EMPRESSPATH
then
	echo "invalid EMPRESSPATH"
	exit 1
fi

. $EMPRESSPATH/rdbms/util_bin/banner

. $EMPRESSPATH/config/options

get_codeset_list ()
{
	test -d $EMPRESSPATH/rdbms/testsrc || return

	HERE=`pwd`
	cd $EMPRESSPATH/rdbms/testsrc
	for I in *
	do
		if test -d $I
		then
			echo $I
		fi
	done
	cd $HERE
}

MSTESTNOTIFY=false
MSTESTRMCORE=true
MSTESTSHCHECK=false
MSTSETPOSTTIME=false
TESTTYPE=normal
RUN_SV_TEST=false
IGNORE_ITERATION=
ABORTFILE="$EMPRESSPATH/rdbms/local/_runtest_abort"
USER_ABORTFILE="$EMPRESSPATH/rdbms/local/_runtest_user_abort"
export ABORTFILE
rm -fr "$ABORTFILE" "$USER_ABORTFILE"

NLOOP=1

echo "You are running this command:
	$0 $*
"

while :
do
	case "$1" in
	 -abort)
			MSRUNTEST_ABORT_IF_FAIL=X
			export MSRUNTEST_ABORT_IF_FAIL
			;;
	-clean)		CLEAN_TESTWORK=true
			export CLEAN_TESTWORK
			;;
	 -chk)		MSTESTSHCHECK=true
			;;
	 -eng*)		MSLANG=english
			export MSLANG
			;;
	 -save | -savetest | -forcefail)
			SAVE_TEST=true
			export SAVE_TEST
			;;
	 -help)		echo "$USAGE"
			exit 0
			;;
	 -jap*)		MSLANG=japanese
			export MSLANG
			;;
	 -keepcore)	MSTESTRMCORE=false
			;;
	 -long)		TESTTYPE=long
			;;
	 -loop)		IGNORE_ITERATION=false
			;;
	 -node)		shift
			MSNETTEST=$1
			;;
	 -nloop)	shift
			IGNORE_ITERATION=true
			NLOOP=$1
			;;
	 -noloop)	IGNORE_ITERATION=true
			;;
	 -notify)	MSTESTNOTIFY=true
			;;
	 -nserver)	shift
			NSERVER=$1
			;;
	 -short)	TESTTYPE=short
			;;
	 -tcp)		INET_TYPE=tcp
			;;
	 -time)		MSTESTPOSTTIME=true
			;;
	 -rcc | -remote_cc)
			shift
			REMOTE_CC=$1
			export REMOTE_CC
			;;
	 -rmtpath)	shift
			EMPRESSRMTPATH=$1
			;;
	 -server)	RUN_SV_TEST=true
			;;
	 -udp)		INET_TYPE=udp
			;;
	-xloop)		shift
			IGNORE_ITERATION=true
			ONETEST_ABORT_IF_FAIL=X
			export ONETEST_ABORT_IF_FAIL
			NLOOP=$1
			XLOOP=true
			;;
	 -*)		echo "Invalid option $I"
			exit 1
			;;
	 *)		break
			;;
	esac
	shift
done

case $REMOTE_CC in
 "")	TDIR=.
	;;
 *)
	TDIR=xtarget/$REMOTE_CC
	;;
esac
export TDIR

LANG_LIST=`get_codeset_list`

if test "$1" != ""
then
	for I in $LANG_LIST
	do
		if test "$1" = $I
		then
			MSTCODESET=$1
			shift
			break
		fi
	done
fi

test "$MSTCODESET" = "" && MSTCODESET=ascii
export MSTCODESET

if test "$MSTCODESET" != "ascii"
then
	X=`$EMPRESSPATH/bin/empvers -langid`

	if test "$MSTCODESET" != "$X"
	then
		echo "This port does not support '$MSTCODESET'"
		echo "Abort ..."
		exit 1
	fi
fi

case $MSSYS_PLATFORM in
 SYS_OS_WIN32)
	X=`$EMPRESSPATH/bin/empvers -langid`

	if test "$X" = "jp_sjis"
	then
		case $MSLANG in
		   japanese)	jp ;;
		   *)	us ;;
		esac
	fi
	;;
esac

case "$EMPRESSRMTPATH" in
 "")	
	EMPRESSRMTPATH=$EMPRESSPATH
	MSSAMEPATH="yes"
	;;
esac

if test "$RUN_SV_TEST" = true
then
	if test "$MSNETTEST" = ""
	then
		MSNETTEST=`$EMPRESSPATH/common/sys_bin/sysuname`
	fi
	export EMPRESSRMTPATH MSNETTEST
	test "$INET_TYPE" = "" && INET_TYPE=tcp
	test "$NSERVER" = "" && NSERVER=6

	$EMPRESSPATH/rdbms/conf_bin/crglbdd
	$EMPRESSPATH/rdbms/conf_bin/crsvconf $INET_TYPE $NSERVER
fi

export MSTESTPOSTTIME

case $MSSYS_PLATFORM in
 SYS_OS_WIN32)
	PWDCMD=pwd
	OUTFILE_OPT="/Fe"
	;;
 *)	
	PWDCMD=/bin/pwd
	if test ! -f $PWDCMD
	then
		PWDCMD=pwd
	fi
	OUTFILE_OPT="-o "
	;;
esac
export OUTFILE_OPT

# if MSLANG is not specified in the env, set MSLANG to english
# because the test under test directory are in english.

test "$MSLANG" = "" && \
	MSLANG="english" && export MSLANG && \
	LC_ALL=C && export LC_ALL

if test -d $EMPRESSPATH/rdbms/testsrc/$MSTCODESET && \
   test "$MSTEST_USERTEST" = ""
then
	MSTSRCPATH=$EMPRESSPATH/rdbms/testsrc/$MSTCODESET
	MSRMTTSRCPATH=$EMPRESSRMTPATH/rdbms/testsrc/$MSTCODESET
else
	MSTSRCPATH=$EMPRESSPATH/rdbms/test
fi

case "$MSDEBUG" in
 "" )	MSDEBUG="sigdefault";;
esac

MSDEBUG="memchk lkclrcheck $MSDEBUG"

SYSID=`$EMPRESSPATH/rdbms/exec/emputil -SYSID`
export SYSID

# just in case, somebody runs this with the evaluation installation
MSMKDBSHMEM=
export MSMKDBSHMEM
export MSSYS_PLATFORM

case $MSSYS_PLATFORM in
 SYS_OS_WIN32)
	IFS=';'

	XPATH=$EMPRESSPATH/bin
	for I in $PATH
	do
		case $I in
		   ".")		
			# need to get rid of '.' in the path
			# otherwise, test like:
			#	./collate/test/test
			#	./mw/param/echo
			# will act funny.
			;;
		   $EMPRESSPATH/bin)
			;;
		   *)
			XPATH="$XPATH;$I"
			;;
		esac
	done
	PATH=$XPATH

	IFS="
"
	;;
 SYS_OS_UNIX_QNX_NEUTRINO*)
	PATH="$HOME/bin:$EMPRESSPATH/bin:$MSSYS_CCOMPILERPATH:/bin:/usr/bin:/usr/ccs/bin:/usr/ucb:/usr/bsd:/usr/local/bin:$PATH"
	;;
 *)
	PATH="$HOME/bin:$EMPRESSPATH/bin:$MSSYS_CCOMPILERPATH:/bin:/usr/bin:/usr/ccs/bin:/usr/ucb:/usr/bsd:/usr/local/bin"
	;;
esac

if test ! -d $EMPRESSPATH/rdbms/testdbs/$MSTCODESET
then
	echo "EMPRESSPATH/rdbms/testdbs/$MSTCODESET does not exist. Abort."
	exit 1
elif test ! -d $EMPRESSPATH/rdbms/testwork/_testtmp/testdir
then
	echo "EMPRESSPATH/rdbms/testwork/_testtmp/testdir does not exist. Abort."
	exit 1
elif test ! -d $EMPRESSPATH/rdbms/testwork/_nettmp/testdir
then
	echo "EMPRESSPATH/rdbms/testwork/_nettmp/testdir does not exist. Abort."
	exit 1
fi

MSTDBSPATH=`cd $EMPRESSPATH/rdbms/testdbs/$MSTCODESET; $PWDCMD`
MSTESTPATH=`cd $EMPRESSPATH/rdbms/testwork; $PWDCMD`
MSTESTDBPATH=`cd $MSTESTPATH/_testtmp; $PWDCMD`

# A bug in Lynx OS shell. If these variables are not set before export,
# the "for" will not work correctly.
MSTMAJOR=X
MSTMINOR=X
MSTITEM=X

MSRMTTDBSPATH=$EMPRESSRMTPATH/rdbms/testdbs/$MSTCODESET
MSRMTTESTPATH=$EMPRESSRMTPATH/rdbms/testwork
MSRMTTESTDBPATH=$MSRMTTESTPATH/_testtmp
case "$MSSAMEPATH" in
 yes)
	MSTESTDB=$MSTESTDBPATH/testdb;;
 *)
	MSTESTDB=$MSRMTTESTDBPATH/testdb;;
esac
MSTESTUSER=`$EMPRESSPATH/common/sys_bin/getuname`

NLSECHO="$EMPRESSPATH/common/nls/nlsecho"
UE_E0_ITMTEST_NOTFOUND=10128
UE_E0_MAJTEST_NOTFOUND=10126
UE_E0_MINTEST_NOTFOUND=10127

check_feature ()
{
	test -f $EMPRESSPATH/common/sys_bin/chkfture || return 0

	XXX=$1
	CHK_FEATURE_MSG=`$EMPRESSPATH/common/sys_bin/chkfture -testsrc $XXX`
	test "$CHK_FEATURE_MSG" = "" && return 0
	return 1
}

EXEC="eval"
case "$MSNETTEST" in
	"")
		MSGLOBALDATADICTIONARY=""
		MSSERVERCONFIGFILE=""
		case "$MSSYS_MULTITASKSHM" in
		  "")   ;;
		  *)    MSDEBUG="noshmem noserver $MSDEBUG"
			;;
		esac
		;;
	* )
		MSNETTESTDBPATH=`cd $MSTESTPATH/_nettmp; $PWDCMD`
		case "$MSSAMEPATH" in
		 yes)
			MSRMTNETTESTDBPATH=$MSNETTESTDBPATH;;
		 *)
			MSRMTNETTESTDBPATH=$MSRMTTESTPATH/_nettmp;;
		esac
		MSNETTESTDB=$MSRMTNETTESTDBPATH/testdb
		MSGLOBALDATADICTIONARY=$MSNETTESTDBPATH/_glbdd
		MSRMTGLOBALDATADICTIONARY=$MSRMTNETTESTDBPATH/_glbdd
		MSSERVERCONFIGFILE=$MSNETTESTDBPATH/_svcfg."$MSNETTEST"
		MSRMTSERVERCONFIGFILE=$MSRMTNETTESTDBPATH/_svcfg."$MSNETTEST"
		if test ! -f "$MSSERVERCONFIGFILE"
		then
			echo "Server configuration file not exist"
			exit 1;
		fi
		case $MSSERVERNAME in
		   "") MSSERVERNAME="_svruntest" ;;
		esac

		TMP_NAME=`$EMPRESSPATH/common/sys_bin/sysuname`

		if test "$MSNETTEST" = "$TMP_NAME"
		then
			MSLOCAL="true"
		else
			MSLOCAL="false"
			EXEC="$EMPRESSPATH/common/sys_bin/rmt_sh"
		fi
		export MSNETTESTDBPATH MSNETTESTDB MSSERVERNAME MSLOCAL
		export MSRMTNETTESTDBPATH MSNETTESTDB
		;;
esac


export MSDEBUG
export MSSERVERCONFIGFILE MSRMTSERVERCONFIGFILE MSGLOBALDATADICTIONARY
export MSRMTGLOBALDATADICTIONARY
export PATH EMPRESSRMTPATH
export MSTSRCPATH MSRMTTSRCPATH MSTDBSPATH MSRMTTDBSPATH
export MSTESTPATH MSRMTTESTPATH EXEC
export MSTESTDBPATH MSRMTTESTDBPATH MSTESTDB MSTESTUSER
export MSTMAJOR MSTMINOR MSTITEM MSTSRCMAJOR MSTSRCMAJOR MSTDBSMAJOR
export MSRMTTDBSMAJOR MSTSRCMINOR MSRMTTSRCMINOR

# from options file
export MSFILEEXTOBJECT MSFILEEXTLIBRARY MSFILEEXTEXEC MSFILENULL

:	' example: I (empress) am running the test: ms datatype date '
:	' MSTSRCPATH=.../w/testsrc/$MSTCODESET '
:	' MSTDBSPATH=.../w/testdbs/$MSTCODESET '
:	' MSTESTPATH=.../w/testwork '
:	' MSTESTDBPATH=.../w/testwork/_testtmp '
:	' MSTESTDB=.../w/testwork/_testtmp/testdb '
:	' MSTESTUSER=empress '
:	' MSTMAJOR=ms '
:	' MSTMINOR=datatype '
:	' MSTITEM=date '
:	' MSTSRCMAJOR=.../w/testsrc/$MSTCODESET/ms '
:	' MSTDBSMAJOR=.../w/testdbs/$MSTCODESET/ms '
:	' MSTSRCMINOR=.../w/testsrc/$MSTCODESET/ms/datatype '

export MSTESTNOTIFY MSTESTSHCHECK MSTESTRMCORE MSTESTCOPY

:	'set up MS variable and other environment for testing'

MSQLPROMPT1="* "
MSQLPROMPT2=". "
MSVERSIONMESG=
export MSQLPROMPT1 MSQLPROMPT2 MSVERSIONMESG
umask 022


echo ">>START_TIME" `date` '(' `date "+%Y %j %H %M %S"` ')' PID=$$

case "$MSNETTEST" in
 "") ;;
 *)
	TZDB=$MSDEBUG
	export TZDB
	case "$MSLOCAL" in
	 false) $EMPRESSPATH/rdbms/conf_bin/emprtsv &;;
	 *)
		MSDEBUG="abort $TZDB"
		export MSDEBUG
		empsv $MSSERVERNAME
		;;
	esac
	MSDEBUG=$TZDB
	export MSDEBUG
	sleep 5
	empsvadm $MSSERVERNAME svinfo
	;;
esac

cd $MSTSRCPATH
case $# in
 0)
	TESTALL=true
	MAJORLIST="[a-z]*"
	MINORLIST="[a-z]*"
	ITEMLIST="[a-z]*"
	;;
 1)
	MAJORLIST="$1"
	MINORLIST="[a-z]*"
	ITEMLIST="[a-z]*"

	case "$1" in
	  */*/*)
		MAJORLIST=`echo $1 | sed -e 's;/.*;;'`
		MINORLIST=`echo $1 | sed -e 's;.*/\(.*\)/.*;\1;'`
		ITEMLIST=`echo $1 | sed -e 's;.*/;;'`

		case $ITEMLIST in
		 *:)	# cut and paste in win32 gets the extra :
			#
			ITEMLIST=`echo $ITEMLIST | sed -e 's;:$;;'`
			;;
		esac
		MSTESTRMCORE=false
		test "$IGNORE_ITERATION" = "" && IGNORE_ITERATION=true
		;;
	  */*)  
		MAJORLIST=`echo $1 | sed -e 's;/.*;;'`
		MINORLIST=`echo $1 | sed -e 's;.*/;;'`
		;;
	esac
	;;
 2)
	MAJORLIST="$1"
	MINORLIST="$2"
	ITEMLIST="[a-z]*"
	;;
 *)
	MAJORLIST="$1"
	MINORLIST="$2"
	shift
	shift
	ITEMLIST="$*"
	MSTESTRMCORE=false
	test "$IGNORE_ITERATION" = "" && IGNORE_ITERATION=true
	;;
esac

test "$IGNORE_ITERATION" = "" && IGNORE_ITERATION=false

for MSTMAJOR in $MAJORLIST
do
	case "$MSTMAJOR" in
	*.RMDIR)	continue ;;
	esac

	MSTSRCMAJOR=$MSTSRCPATH/$MSTMAJOR
	MSRMTTSRCMAJOR=$MSRMTTSRCPATH/$MSTMAJOR
	MSTDBSMAJOR=$MSTDBSPATH/$MSTMAJOR
	MSRMTTDBSMAJOR=$MSRMTTDBSPATH/$MSTMAJOR

	if test ! -d $MSTSRCMAJOR
	then
		$NLSECHO "%s: major test directory not found" $UE_E0_MAJTEST_NOTFOUND $MSTMAJOR
		continue
	fi

	cd $MSTSRCMAJOR

	MSPWD=`$PWDCMD`
	if check_feature $MSPWD 
	then
		:
	else
		echo "runtest: skip $MSTMAJOR; $CHK_FEATURE_MSG"
		continue
	fi
		
	case $MSNETTEST in
	  "")	;;
	  *)	if test -f _no_server
		then
			continue
		fi
		;;
	esac

	case $MSLOCAL in
	  false)
		if test -f _no_remote
		then
			continue
		fi
		;;
	esac

	for MSTMINOR in $MINORLIST
	do
		case "$MSTMINOR" in
		*.RMDIR)	continue ;;
		esac

		MSTSRCMINOR=$MSTSRCMAJOR/$MSTMINOR
		MSRMTTSRCMINOR=$MSRMTTSRCMAJOR/$MSTMINOR
		if test ! -d $MSTSRCMINOR
		then
			$NLSECHO "%s/%s: minor test directory not found" $UE_E0_MINTEST_NOTFOUND $MSTMAJOR $MSTMINOR
			continue
		fi

		cd $MSTSRCMINOR

		MSPWD=`$PWDCMD`
		if check_feature $MSPWD
		then
			:
		else
			echo "runtest: skip $MSTMAJOR/$MSTMINOR; $CHK_FEATURE_MSG"
			continue
		fi
		
		case $MSNETTEST in
		  "")	;;
		  *)	if test -f _no_server
			then
				continue
			fi
			;;
		esac

		case $MSLOCAL in
		  false)
			if test -f _no_remote
			then
				continue
			fi
			;;
		esac

		if test -f _no_copy
		then
			MSTESTCOPY=false
		else
			MSTESTCOPY=true
		fi

		for MSTITEM in $ITEMLIST
		do
			case "$MSTITEM" in
			*.RM | *.v[0-9])	continue ;;
			esac

			if test ! -f $MSTITEM
			then
				$NLSECHO "%s/%s/%s: item test not found" $UE_E0_ITMTEST_NOTFOUND $MSTMAJOR $MSTMINOR $MSTITEM
				continue
			fi

			case $MSTITEM in
			 *.v[0-9]*) continue ;;
			esac

			MSPWD=`$PWDCMD`

			if check_feature $MSPWD/$MSTITEM
			then
				:
			else
				echo "runtest: skip $MSTMAJOR/$MSTMINOR/$MSTITEM; $CHK_FEATURE_MSG"
				continue
			fi

			if test "$TESTTYPE" != "normal"
			then
				TEST_IS_LONG=false
				if grep 'chmod 644 _longtest' $MSPWD/$MSTITEM \
					> $MSFILENULL
				then
					TEST_IS_LONG=true
				fi

				if test "$TEST_IS_LONG" = "true" && \
				   test "$TESTTYPE" = "short"
				then
					test "$TESTALL" = "true" || \
					echo "runtest: skip $MSTMAJOR/$MSTMINOR/$MSTITEM; long test"
					continue
				fi

				if test "$TEST_IS_LONG" != "true" && \
				   test "$TESTTYPE" = "long"
				then
					test "$TESTALL" = "true" || \
					echo "runtest: skip $MSTMAJOR/$MSTMINOR/$MSTITEM; short test"
					continue
				fi
			fi

			MAXLOOP=$NLOOP

			if test "$IGNORE_ITERATION" = false
			then
				test -f $MSTSRCMINOR/_iteration && \
				MAXLOOP=`cat $MSTSRCMINOR/_iteration`

				test -f $TESTMINOR/_iteration &&
				MAXLOOP=`cat $TESTMINOR/_iteration`
			fi

			case $MAXLOOP in
			   [1-9] | [1-9][0-9] | [1-9][0-9][0-9])
				;;
			   *)
				echo "*** Invalid iteration count $MAXLOOP"
				echo "ITERATION is now 1"
				MAXLOOP=1
				;;
			esac

			if test "$XLOOP" = true
			then
				if test "$MSRUNTEST_ABORT_IF_FAIL" != ""
				then
					# if -abort is set xloop is the
					# same as nloop
					XLOOP=false
				fi
			fi

			ITERATION=$MAXLOOP

			DOTEST=$EMPRESSPATH/rdbms/conf_bin/dotest

			while :
			do
				test "$ITERATION" = "0" && break

				echo
				echo "$MSTMAJOR/$MSTMINOR/$MSTITEM:"

				if test "$MAXLOOP" != 1
				then
					echo "Iteration: " `expr $MAXLOOP - $ITERATION + 1`
				fi

				case $MSSYS_PLATFORM in
				  SYS_OS_WIN32)
					# Korn shell will crash on first
					# command substitution like `uname` in
					# $w/sys_bin/cpdir. To prevent this,
					# call next script without sh.
	 
					$DOTEST
					;;
				  *) case $MSTESTSHCHECK in
					true)	sh -x $DOTEST ;;
					false)	sh $DOTEST ;;
				     esac
					;;
				esac

				ITERATION=`expr $ITERATION - 1`

				if test -f "$ABORTFILE"
				then
					if test "$XLOOP" = "true"
					then
						rm -fr "$ABORTFILE"
						if test "$MAXLOOP" != 1
						then
							cat <<EOM

****
**** Remaining iteration of this test is skipped because of this failure ***
****

EOM
						fi
					fi
					break
				fi
				test -f "$USER_ABORTFILE" && break
			done

			case "$MSNETTEST" in
			 "") ;;
			 *)
				empsvadm $MSSERVERNAME svstats
				empsvadm $MSSERVERNAME svrmall
				;;
			esac

			test -f "$ABORTFILE" && break
			test -f "$USER_ABORTFILE" && break

		done

		test -f "$ABORTFILE" && break
		test -f "$USER_ABORTFILE" && break
	done

	test -f "$ABORTFILE" && break
	test -f "$USER_ABORTFILE" && break
done

if test -f "$USER_ABORTFILE" && test ! -f "$ABORTFILE"
then
	cat <<EOM

***
*** $USER_ABORTFILE is found
*** This runtest is aborted by the user
***

EOM

fi

case "$MSNETTEST" in
 "") ;;
 *)
	empsvadm $MSSERVERNAME svshut
	sleep 10
	;;
esac

echo
echo ">>END_TIME" `date` '(' `date "+%Y %j %H %M %S"` ')'

EX_STATUS=0
test -f "$ABORTFILE" && EX_STATUS=1
test -f "$USER_ABORTFILE" && EX_STATUS=1

rm -fr $ABORTFILE $USER_ABORTFILE

