#!/bin/ksh
#
#ident	"@(#)video.stf	1.13	04/12/15 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# THIS IS THE SIMPLE VIDEO DISPLAY TEST
# All it does is open a box and spiral some text around.
#
#########################################################################
# NOTES ON OPERATION AND ERROR HANDLING:
# This script is called by staf.eng, which is the main test harness.
# Standard output gets recorded in the test module log file by staf.eng.
#
# Variable maxwarn is checked by staf.eng in the "chk_err()" monitoring process,
# by grep'ing the log file of this test module.
# If maxwarn is exceeded, the staf.eng script will kill this test module.
# There are two formats for the error echos to the log files;
#
# 1.   echo "!ERROR: blah blah foo bar";
#      which will terminate the test.  If the test harness detects
#      a string match with "!ERROR", then all testing terminates.
#
# 2.   echo "!WARNING: blah blah foo bar";
#       The above format "!WARNING:" will make the log file entry,
#       but all testing will continue, no termination of tests,
#       UNLESS maxwarnings variable is exceeded.
#
# DANGER!!:  Even if you do not intend to use WARNING messages, variable
# maxwarn *MUST* be defined and the "maxwarnings=" line *MUST* be
# echo'ed to the logfile.  This syntax is mandatory.
#########################################################################

# variables
prgdir=../../prgs
X11Perf_Bin=/usr/X11/demo/x11perf	# the path of x11perf binary
X11Per_Args="-v1.3 -rop GXcopy GXxor -repeat 1 -time 1 -all"	# the arguments to start x11perf
LogFile=/dev/null	# output of x11perf, to calculate the xmark value in the future
iter=1

#maxwarn=50	# max number of nonfatal warnings to tolerate.
# SET LOGFILE FLAG FOR MAX WARNING COUNT
# This is checked by staf.eng, and if exceeded, staf.eng will kill this module.
# ( This only makes sense if this script echos warning messages, but
#   inclusion of this variable in the log file is *MANDATORY* in all cases. )
#echo "maxwarnings=${maxwarn}"
#

export DISPLAY=`hostname`":0.0"	# test system hostname

# clean_up
clean_up()
{
	pkill x11perf
	echo "Video test ending at: " `date`
}

### MAIN ###

# set trap
trap 'clean_up;exit' 17

# check args
if [ $# -eq 1 ]
then
	iter=$1
fi

if [ $iter -lt 1 ]
then
	echo "Wrong argument!"
	echo "The first argument must be no less than 1"
	exit 1
fi

# check whether x11perf binary exists and executable
if [ ! -f $X11Perf_Bin ] || [ ! -x $X11Per_Bin ]
then
	echo "Wrong environment!"
	echo "Please make sure you have installed x11perf binary as $X11Perf_Bin"
	echo "And $X11Perf_Bin is executable."
	exit 1
fi

# check whether Xserver has been started
ps -ef | egrep '(\/bin\/X |Xsun|Xorg)' | egrep -v egrep > /dev/null
if [ $? -ne 0 ]
then
	echo "Wrong environment!"
	echo "Start Xserver before running the video case."
	exit 1
fi

# check whether the user has logged on the system to be tested. If the end user start Xsession manually 
# then some errors appear, but it is the best way until now. 
ps -ef | egrep '(Xsession)' | egrep -v egrep > /dev/null
if [ $? -ne 0 ]
then
	echo "Wrong environment!"
	echo "Start Xwindows and log onto the system to be tested before running the video case."
	exit 1
fi

echo "Video test starting at: " `date`

# do test
num=$iter
while [ $num -gt 0 ]
do
	starttime=`$prgdir/time_sec`

	#$X11Perf_Bin  $X11Per_Args > $LogFile 2>&1 
	$X11Perf_Bin  $X11Per_Args 
	if [ $? -ne 0 ]
	then
		echo "!ERROR: Video test stopped with errors."
		exit 1
	fi

	endtime=`$prgdir/time_sec`
	
	if [ $iter -gt 1 ]
	then
		ltime=`expr $endtime - $starttime`
		echo LOOPTIME: $ltime
	fi
	num=`expr $num - 1`

	echo "$num iterations remaining."
done

clean_up
echo "!PASS: Video test completed" 
exit 0 
