#!/bin/sh

if test "x`id -u`" = x0; then
    echo Please run this as a normal user. Not supported when run as root.
    exit 1
fi

argcount=$#

#if test $# -gt 0; then
#	testtype=$1
#	shift
#else
#	read testtype
#fi
#echo ""


SETCOLOR_SUCCESS="echo -en \\033[1;32m"
SETCOLOR_FAILURE="echo -en \\033[1;31m"
SETCOLOR_WARNING="echo -en \\033[1;33m"
SETCOLOR_NORMAL="echo -en \\033[0;39m"

PRINT_FAIL="echo -e \\033[1;31mFAIL\\033[0;39m"
PRINT_PASS="echo -e \\033[1;32mPASS\\033[0;39m"

GREP=grep

testtype=1

case "$testtype" in
	0)
		echo "Running in test mode (0)"
		nprinters1=0
		nprinters2=0
		pjobs=0
		pprinters=0
		loglevel="debug2"
		;;
	2)
		echo "Running the medium tests (2)"
		nprinters1=10
		nprinters2=20
		pjobs=20
		pprinters=10
		loglevel="debug"
		;;
	3)
		echo "Running the extreme tests (3)"
		nprinters1=500
		nprinters2=1000
		pjobs=100
		pprinters=50
		loglevel="debug"
		;;
	4)
		echo "Running the torture tests (4)"
		nprinters1=10000
		nprinters2=20000
		pjobs=200
		pprinters=100
		loglevel="debug"
		;;
	*)
		echo "Running the timid tests (1)"
		nprinters1=0
		nprinters2=0
		pjobs=10
		pprinters=0
		loglevel="debug2"
		testtype="1"
		;;
esac

echo "Starting CUPS tests..."

# Force the permissions of the files we create...
umask 022

# Find user name
user="$USER"
if test -z "$user"; then
	if test -x /usr/ucb/whoami; then
		user=`/usr/ucb/whoami`
	else
		user=`whoami`
	fi

	if test -z "$user"; then
		user="unknown"
	fi
fi

echo "User name: $user"

port="${CUPS_TESTPORT:=8631}"

#cwd=`pwd`
ABSOLUTE_FILENAME=`readlink -e "$0"`
cwd=`dirname "$ABSOLUTE_FILENAME"`
root=`dirname $cwd`

CUPS_TESTROOT="$root"; export CUPS_TESTROOT

#CUPS installation prefix
CUPS_PREFIX=/usr

BASE="${CUPS_TESTBASE:=}"
if test -z "$BASE"; then
	BASE=/tmp/cups-$user
fi
export BASE

echo "Test directory: $BASE"

# Make sure that the LPDEST and PRINTER environment variables are
# not included in the environment that is passed to the tests.  These
# will usually cause tests to fail erroneously...
unset LPDEST
unset PRINTER

usedebugprintfs=Y

case "$usedebugprintfs" in
	Y* | y*)
		echo "Enabling debug printfs (level 5); log files can be found in $BASE/log..."
		CUPS_DEBUG_LOG="$BASE/log/debug_printfs.%d"; export CUPS_DEBUG_LOG
		CUPS_DEBUG_LEVEL=5; export CUPS_DEBUG_LEVEL
		CUPS_DEBUG_FILTER='^(http|_http|ipp|_ipp|cups.*Request|cupsGetResponse|cupsSend).*$'; export CUPS_DEBUG_FILTER
		;;

	0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)
		echo "Enabling debug printfs (level $usedebugprintfs); log files can be found in $BASE/log..."
		CUPS_DEBUG_LOG="$BASE/log/debug_printfs.%d"; export CUPS_DEBUG_LOG
		CUPS_DEBUG_LEVEL="$usedebugprintfs"; export CUPS_DEBUG_LEVEL
		CUPS_DEBUG_FILTER='^(http|_http|ipp|_ipp|cups.*Request|cupsGetResponse|cupsSend).*$'; export CUPS_DEBUG_FILTER
		;;

	*)
		;;
esac


echo "Creating directories for test..."
rm -rf $BASE
mkdir $BASE
mkdir $BASE/bin
mkdir $BASE/bin/backend
mkdir $BASE/bin/driver
mkdir $BASE/bin/filter
mkdir $BASE/bin/notifier
mkdir $BASE/certs
mkdir $BASE/share
mkdir $BASE/share/banners
mkdir $BASE/share/drv
mkdir $BASE/share/locale
#for file in ../locale/cups_*.po; do
#	loc=`basename $file .po | cut -c 6-`
#	mkdir $BASE/share/locale/$loc
#	ln -s $root/locale/cups_$loc.po $BASE/share/locale/$loc
#	ln -s $root/locale/ppdc_$loc.po $BASE/share/locale/$loc
#done
mkdir $BASE/share/mime
mkdir $BASE/share/model
mkdir $BASE/share/ppdc
mkdir $BASE/interfaces
mkdir $BASE/log
mkdir $BASE/ppd
mkdir $BASE/spool
mkdir $BASE/spool/temp
mkdir $BASE/ssl

cp $CUPS_PREFIX/lib/cups/backend/dnssd $BASE/bin/backend
cp $CUPS_PREFIX/lib/cups/backend/http $BASE/bin/backend
cp $CUPS_PREFIX/lib/cups/backend/ipp $BASE/bin/backend
cp $CUPS_PREFIX/lib/cups/backend/lpd $BASE/bin/backend
cp $CUPS_PREFIX/lib/cups/backend/mdns $BASE/bin/backend
ln -s $root/share/cups-test/pseudo $BASE/bin/backend
cp $CUPS_PREFIX/lib/cups/backend/snmp $BASE/bin/backend
cp $CUPS_PREFIX/lib/cups/backend/socket $BASE/bin/backend
cp $CUPS_PREFIX/lib/cups/backend/usb $BASE/bin/backend
ln -s $CUPS_PREFIX/lib/cups/cgi-bin $BASE/bin
ln -s $CUPS_PREFIX/lib/cups/monitor $BASE/bin
ln -s $CUPS_PREFIX/lib/cups/notifier/dbus $BASE/bin/notifier
ln -s $CUPS_PREFIX/lib/cups/notifier/mailto $BASE/bin/notifier
ln -s $CUPS_PREFIX/lib/cups/notifier/rss $BASE/bin/notifier
if test -f $root/testnotify/testnotify; then
    ln -s $root/testnotify/testnotify $BASE/bin/notifier #TODO fix this
else
    ln -s $root/lib/cups/notifier/testnotify $BASE/bin/notifier
fi

ln -s $CUPS_PREFIX/lib/cups/daemon $BASE/bin/daemon
ln -s $CUPS_PREFIX/lib/cups/filter/commandtops $BASE/bin/filter
ln -s $CUPS_PREFIX/lib/cups/filter/gziptoany $BASE/bin/filter
ln -s $CUPS_PREFIX/lib/cups/filter/pstops $BASE/bin/filter
ln -s $CUPS_PREFIX/lib/cups/filter/rastertoepson $BASE/bin/filter
ln -s $CUPS_PREFIX/lib/cups/filter/rastertohp $BASE/bin/filter
ln -s $CUPS_PREFIX/lib/cups/filter/rastertolabel $BASE/bin/filter
ln -s $CUPS_PREFIX/lib/cups/filter/rastertopwg $BASE/bin/filter

ln -s $root/share/cups-test/banners/standard $BASE/share/banners
ln -s $root/share/cups-test/banners/classified $BASE/share/banners


ln -s $CUPS_PREFIX/share/cups/ppdc $BASE/share/data
ln -s $CUPS_PREFIX/share/cups/drv/sample.drv $BASE/share/drv
ln -s $CUPS_PREFIX/share/cups/mime/mime.types $BASE/share/mime
ln -s $CUPS_PREFIX/share/cups/mime/mime.convs $BASE/share/mime
ln -s $CUPS_PREFIX/share/cups/ppdc/*.h $BASE/share/ppdc
ln -s $CUPS_PREFIX/share/cups/ppdc/*.defs $BASE/share/ppdc
ln -s $CUPS_PREFIX/share/cups/templates $BASE/share


#
# Local filters and configuration files...
#
echo "Creating filters and configuration files..."

instfilter() {
	# instfilter src dst format
	#
	# See if the filter exists in a standard location; if so, make a
	# symlink, otherwise create a dummy script for the specified format.
	#
	src="$1"
	dst="$2"
	format="$3"

	for dir in /usr/local/libexec/cups/filter /usr/libexec/cups/filter /usr/lib/cups/filter; do
		if test -x "$dir/$src"; then
			ln -s "$dir/$src" "$BASE/bin/filter/$dst"
			return
		fi
	done

	# Source filter not present, create a dummy filter
	case $format in
		passthru)
			ln -s gziptoany "$BASE/bin/filter/$dst"
			;;
		pdf)
			cat >"$BASE/bin/filter/$dst" <<EOF
#!/bin/sh
trap "" TERM
trap "" PIPE
gziptoany "$1" "$2" "$3" "$4" "$5" \$6 >/dev/null
case "\$5" in
	*media=a4* | *media=iso_a4* | *PageSize=A4*)
		gziptoany "$1" "$2" "$3" "$4" "$5" "$root/share/cups-test/tests/onepage-a4.pdf"
		;;
	*)
		gziptoany "$1" "$2" "$3" "$4" "$5" "$root/share/cups-test/tests/onepage-letter.pdf"
		;;
esac
EOF
			chmod +x "$BASE/bin/filter/$dst"
			;;
		ps)
			cat >"$BASE/bin/filter/$dst" <<EOF
#!/bin/sh
trap "" TERM
trap "" PIPE
gziptoany "$1" "$2" "$3" "$4" "$5" \$6 >/dev/null
case "\$5" in
	*media=a4* | *media=iso_a4* | *PageSize=A4*)
		gziptoany "$1" "$2" "$3" "$4" "$5" "$root/share/cups-test/tests/onepage-a4.ps"
		;;
	*)
		gziptoany "$1" "$2" "$3" "$4" "$5" "$root/share/cups-test/tests/onepage-letter.ps"
		;;
esac
EOF
			chmod +x "$BASE/bin/filter/$dst"
			;;
		raster)
			cat >"$BASE/bin/filter/$dst" <<EOF
#!/bin/sh
trap "" TERM
trap "" PIPE
gziptoany "$1" "$2" "$3" "$4" "$5" \$6 >/dev/null
case "\$5" in
	*media=a4* | *media=iso_a4* | *PageSize=A4*)
		gziptoany "$1" "$2" "$3" "$4" "$5" "$root/share/cups-test/tests/onepage-a4-300-black-1.pwg.gz"
		;;
	*)
		gziptoany "$1" "$2" "$3" "$4" "$5" "$root/share/cups-test/tests/onepage-letter-300-black-1.pwg.gz"
		;;
esac
EOF
			chmod +x "$BASE/bin/filter/$dst"
			;;
	esac
}

ln -s $root/share/cups-test/test.convs $BASE/share/mime

instfilter imagetopdf imagetopdf pdf
instfilter pdftopdf pdftopdf passthru
instfilter pdftops pdftops ps
instfilter pdftoraster pdftoraster raster
instfilter pstoraster pstoraster raster
instfilter texttopdf texttopdf pdf

if test -d /usr/share/cups/charsets; then
    ln -s /usr/share/cups/charsets $BASE/share
fi


# Then create the necessary config files...

echo "Creating cupsd.conf for test..."

#if test $ssltype = 2; then
#	encryption="Encryption Required"
#else
#	encryption=""
#fi

cat >$BASE/cupsd.conf <<EOF
StrictConformance Yes
Browsing Off
Listen 127.0.0.1:$port
Listen $BASE/sock
PassEnv DYLD_LIBRARY_PATH
PassEnv LD_LIBRARY_PATH
PassEnv LD_PRELOAD
PassEnv LOCALEDIR
PassEnv SHLIB_PATH
MaxSubscriptions 3
MaxLogSize 0
AccessLogLevel actions
LogLevel $loglevel
LogTimeFormat usecs
PreserveJobHistory Yes
PreserveJobFiles 5m
<Policy default>
<Limit All>
Order Allow,Deny
$encryption
</Limit>
</Policy>
EOF

#if test $testtype = 0; then
#	echo WebInterface yes >>$BASE/cupsd.conf
#fi

echo "Creating cups-files.conf for test..."

cat >$BASE/cups-files.conf <<EOF
FileDevice yes
Printcap
User $user
ServerRoot $BASE
StateDir $BASE
ServerBin $BASE/bin
CacheDir $BASE/share
DataDir $BASE/share
FontPath $BASE/share/fonts
DocumentRoot $root/doc
RequestRoot $BASE/spool
TempDir $BASE/spool/temp
AccessLog $BASE/log/access_log
ErrorLog $BASE/log/error_log
PageLog $BASE/log/page_log

PassEnv DYLD_INSERT_LIBRARIES
PassEnv DYLD_LIBRARY_PATH
PassEnv LD_LIBRARY_PATH
PassEnv LD_PRELOAD
PassEnv LOCALEDIR
PassEnv SHLIB_PATH
EOF

echo "Creating printers.conf for test..."

i=1
while test $i -le $nprinters1; do
	cat >>$BASE/printers.conf <<EOF
<Printer test-$i>
Accepting Yes
DeviceURI file:/dev/null
Info Test PS printer $i
JobSheets none none
Location CUPS test suite
State Idle
StateMessage Printer $1 is idle.
</Printer>
EOF

	cp $root/share/cups-test/testps.ppd $BASE/ppd/test-$i.ppd

	i=`expr $i + 1`
done

while test $i -le $nprinters2; do
	cat >>$BASE/printers.conf <<EOF
<Printer test-$i>
Accepting Yes
DeviceURI file:/dev/null
Info Test raw printer $i
JobSheets none none
Location CUPS test suite
State Idle
StateMessage Printer $1 is idle.
</Printer>
EOF

	i=`expr $i + 1`
done

if test -f $BASE/printers.conf; then
	cp $BASE/printers.conf $BASE/printers.conf.orig
else
	touch $BASE/printers.conf.orig
fi

export SHLIB_PATH

CUPS_DISABLE_APPLE_DEFAULT=yes; export CUPS_DISABLE_APPLE_DEFAULT
CUPS_SERVER=127.0.0.1:$port; export CUPS_SERVER
CUPS_SERVERROOT=$BASE; export CUPS_SERVERROOT
CUPS_STATEDIR=$BASE; export CUPS_STATEDIR
CUPS_DATADIR=$BASE/share; export CUPS_DATADIR
LOCALEDIR=$BASE/share/locale; export LOCALEDIR

#
# Set a new home directory to avoid getting user options mixed in...
#

HOME=$BASE
export HOME

LANG=C
export LANG

LC_MESSAGES=C
export LC_MESSAGES

#
# Start the server; run as foreground daemon in the background...
#

echo "Starting scheduler:"
echo "    /usr/sbin/cupsd -c $BASE/cupsd.conf -f >$BASE/log/debug_log 2>&1 &"
echo ""

/usr/sbin/cupsd -c $BASE/cupsd.conf -f >$BASE/log/debug_log 2>&1 &

cupsd=$!

if test "x$testtype" = x0; then
	# Not running tests...
	echo "Scheduler is PID $cupsd and is listening on port $port."
	echo ""

	# Create a helper script to run programs with...
	runcups="$BASE/runcups"

	echo "#!/bin/sh" >$runcups
	echo "# Helper script for running CUPS test instance." >>$runcups
	echo "" >>$runcups
	echo "# Set required environment variables..." >>$runcups
	echo "CUPS_DATADIR=\"$CUPS_DATADIR\"; export CUPS_DATADIR" >>$runcups
	echo "CUPS_SERVER=\"$CUPS_SERVER\"; export CUPS_SERVER" >>$runcups
	echo "CUPS_SERVERROOT=\"$CUPS_SERVERROOT\"; export CUPS_SERVERROOT" >>$runcups
	echo "CUPS_STATEDIR=\"$CUPS_STATEDIR\"; export CUPS_STATEDIR" >>$runcups
	echo "DYLD_LIBRARY_PATH=\"$DYLD_LIBRARY_PATH\"; export DYLD_LIBRARY_PATH" >>$runcups
	echo "LD_LIBRARY_PATH=\"$LD_LIBRARY_PATH\"; export LD_LIBRARY_PATH" >>$runcups
	echo "LD_PRELOAD=\"$LD_PRELOAD\"; export LD_PRELOAD" >>$runcups
	echo "LOCALEDIR=\"$LOCALEDIR\"; export LOCALEDIR" >>$runcups
	echo "SHLIB_PATH=\"$SHLIB_PATH\"; export SHLIB_PATH" >>$runcups
	if test "x$CUPS_DEBUG_LEVEL" != x; then
		echo "CUPS_DEBUG_FILTER='$CUPS_DEBUG_FILTER'; export CUPS_DEBUG_FILTER" >>$runcups
		echo "CUPS_DEBUG_LEVEL=$CUPS_DEBUG_LEVEL; export CUPS_DEBUG_LEVEL" >>$runcups
		echo "CUPS_DEBUG_LOG='$CUPS_DEBUG_LOG'; export CUPS_DEBUG_LOG" >>$runcups
	fi
	echo "" >>$runcups
	echo "# Run command..." >>$runcups
	echo "exec \"\$@\"" >>$runcups

	chmod +x $runcups

	echo "The $runcups helper script can be used to test programs"
	echo "with the server."
	exit 0
fi

if test $argcount -eq 0; then
	echo "Scheduler is PID $cupsd; run debugger now if you need to."
	echo ""
	echo $ac_n "Press ENTER to continue... $ac_c"
	read junk
else
	echo "Scheduler is PID $cupsd."
	sleep 2
fi

IPP_PORT=$port; export IPP_PORT

while true; do
	running=`LC_ALL=C lpstat -r 2>/dev/null`
	if test "x$running" = "xscheduler is running"; then
		break
	fi

	echo "Waiting for scheduler to become ready..."
	sleep 10
done

date=`date "+%Y-%m-%d"`

strfile=$BASE/cups-str-2.2-$date-$user.html

rm -f $strfile
cat $root/share/cups-test/str-header.html >$strfile

# Run the IPP tests...

echo ""
echo "Running IPP compliance tests..."

echo "<H1><A NAME='IPP'>1 - IPP Compliance Tests</A></H1>" >>$strfile
echo "<P>This section provides the results to the IPP compliance tests" >>$strfile
echo "outlined in the CUPS Software Test Plan. These tests were run on" >>$strfile
echo `date "+%Y-%m-%d"` by $user on `hostname`. >>$strfile
echo "<PRE>" >>$strfile

fail=0
TEST_PATH=$root/share/cups-test/tests
for file in $TEST_PATH/4*.test $TEST_PATH/ipp-2.1.test; do
	echo $ac_n "Performing $file: $ac_c"
	echo "" >>$strfile

	if test $file = $TEST_PATH/ipp-2.1.test; then
		uri="ipp://127.0.0.1:$port/printers/Test1"
		options="-V 2.1 -d NOPRINT=1 -f testfile.ps"
	else
		uri="ipp://127.0.0.1:$port/printers"
		options=""
	fi
	$VALGRIND ipptool -tI $options $uri $file >> $strfile
	status=$?

	if test $status != 0; then
		${PRINT_FAIL}
		fail=`expr $fail + 1`
	else
		${PRINT_PASS}
	fi
done

echo "</PRE>" >>$strfile


# Run the command tests...
echo ""
echo "Running command tests..."

echo "<H1><A NAME='COMMAND'>2 - Command Tests</A></H1>" >>$strfile
echo "<P>This section provides the results to the command tests" >>$strfile
echo "outlined in the CUPS Software Test Plan. These tests were run on" >>$strfile
echo $date by $user on `hostname`. >>$strfile
echo "<PRE>" >>$strfile

export TEST_PATH

for file in $TEST_PATH/5*.sh; do
	#
	# Make sure the past jobs are done before going on.
	#
	sh $TEST_PATH/waitjobs.sh 1800

	echo $ac_n "Performing $file: $ac_c"
	echo "" >>$strfile
	echo "\"$file\":" >>$strfile

	sh $file $pjobs $pprinters >> $strfile
	status=$?

	if test $status != 0; then
		${PRINT_FAIL}
		fail=`expr $fail + 1`
	else
		${PRINT_PASS}
	fi
done

echo $ac_n "Performing restart test: $ac_c"
echo "" >>$strfile
echo "\"5.10-restart\":" >>$strfile

kill -HUP $cupsd

while true; do
	sleep 5

	running=`LC_ALL=C lpstat -r 2>/dev/null`
	if test "x$running" = "xscheduler is running"; then
		break
	fi
done

description="`LC_ALL=C lpstat -l -p Test1 | grep Description | sed -e '1,$s/^[^:]*: //g'`"
if test "x$description" != "xTest Printer 1"; then
	echo "Failed, printer-info for Test1 is '$description', expected 'Test Printer 1'." >>$strfile
	echo "FAIL (got '$description', expected 'Test Printer 1')"
	fail=`expr $fail + 1`
else
	echo "Passed." >>$strfile
	${PRINT_PASS}
fi

for file in $TEST_PATH/6*.sh; do
	#
	# Make sure the past jobs are done before going on.
	#
	sh $TEST_PATH/waitjobs.sh 1800

	echo $ac_n "Performing $file: $ac_c"
	echo "" >>$strfile
	echo "\"$file\":" >>$strfile

	sh $file $pjobs $pprinters >> $strfile
	status=$?

	if test $status != 0; then
		${PRINT_FAIL}
		fail=`expr $fail + 1`
	else
		${PRINT_PASS}
	fi
done

echo "</PRE>" >>$strfile

#
# Stop the server...
#

kill $cupsd
wait $cupsd
cupsdstatus=$?

# Verify counts...
echo "Test Summary"
echo ""
echo "<H1><A NAME='SUMMARY'>3 - Test Summary</A></H1>" >>$strfile

if test $cupsdstatus != 0; then
	echo "FAIL: cupsd failed with exit status $cupsdstatus."
	echo "<p>FAIL: cupsd failed with exit status $cupsdstatus.</p>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: cupsd exited with no errors."
	echo "<p>PASS: cupsd exited with no errors.</p>" >>$strfile
fi

# Job control files
count=`ls -1 $BASE/spool | wc -l`
count=`expr $count - 1`
if test $count != 0; then
	echo "FAIL: $count job control files were not purged."
	echo "<P>FAIL: $count job control files were not purged.</P>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: All job control files purged."
	echo "<P>PASS: All job control files purged.</P>" >>$strfile
fi

# Pages printed on Test1 (within 1 page for timing-dependent cancel issues)
count=`$GREP '^Test1 ' $BASE/log/page_log | awk 'BEGIN{count=0}{count=count+$7}END{print count}'`
expected=`expr $pjobs \* 2 + 34`
expected2=`expr $expected + 2`
if test $count -lt $expected -a $count -gt $expected2; then
	echo "FAIL: Printer 'Test1' produced $count page(s), expected $expected."
	echo "<P>FAIL: Printer 'Test1' produced $count page(s), expected $expected.</P>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: Printer 'Test1' correctly produced $count page(s)."
	echo "<P>PASS: Printer 'Test1' correctly produced $count page(s).</P>" >>$strfile
fi

# Paged printed on Test2
count=`$GREP '^Test2 ' $BASE/log/page_log | awk 'BEGIN{count=0}{count=count+$7}END{print count}'`
expected=`expr $pjobs \* 2 + 3`
if test $count != $expected; then
	echo "FAIL: Printer 'Test2' produced $count page(s), expected $expected."
	echo "<P>FAIL: Printer 'Test2' produced $count page(s), expected $expected.</P>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: Printer 'Test2' correctly produced $count page(s)."
	echo "<P>PASS: Printer 'Test2' correctly produced $count page(s).</P>" >>$strfile
fi

# Paged printed on Test3
count=`$GREP '^Test3 ' $BASE/log/page_log | awk 'BEGIN{count=0}{count=count+$7}END{print count}'`
expected=2
if test $count != $expected; then
	echo "FAIL: Printer 'Test3' produced $count page(s), expected $expected."
	echo "<P>FAIL: Printer 'Test3' produced $count page(s), expected $expected.</P>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: Printer 'Test3' correctly produced $count page(s)."
	echo "<P>PASS: Printer 'Test3' correctly produced $count page(s).</P>" >>$strfile
fi

# Requests logged
count=`wc -l $BASE/log/access_log | awk '{print $1}'`
expected=`expr 35 + 18 + 30 + $pjobs \* 8 + $pprinters \* $pjobs \* 4`
if test $count != $expected; then
	echo "FAIL: $count requests logged, expected $expected."
	echo "<P>FAIL: $count requests logged, expected $expected.</P>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count requests logged."
	echo "<P>PASS: $count requests logged.</P>" >>$strfile
fi

# Did CUPS-Get-Default get logged?
if $GREP -q CUPS-Get-Default $BASE/log/access_log; then
	echo "FAIL: CUPS-Get-Default logged with 'AccessLogLevel actions'"
	echo "<P>FAIL: CUPS-Get-Default logged with 'AccessLogLevel actions'</P>" >>$strfile
	echo "<PRE>" >>$strfile
	$GREP CUPS-Get-Default $BASE/log/access_log | sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' >>$strfile
	echo "</PRE>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: CUPS-Get-Default not logged."
	echo "<P>PASS: CUPS-Get-Default not logged.</P>" >>$strfile
fi

# Emergency log messages
count=`$GREP '^X ' $BASE/log/error_log | wc -l | awk '{print $1}'`
if test $count != 0; then
	echo "FAIL: $count emergency messages, expected 0."
	$GREP '^X ' $BASE/log/error_log
	echo "<P>FAIL: $count emergency messages, expected 0.</P>" >>$strfile
	echo "<PRE>" >>$strfile
	$GREP '^X ' $BASE/log/error_log | sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' >>$strfile
	echo "</PRE>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count emergency messages."
	echo "<P>PASS: $count emergency messages.</P>" >>$strfile
fi

# Alert log messages
count=`$GREP '^A ' $BASE/log/error_log | wc -l | awk '{print $1}'`
if test $count != 0; then
	echo "FAIL: $count alert messages, expected 0."
	$GREP '^A ' $BASE/log/error_log
	echo "<P>FAIL: $count alert messages, expected 0.</P>" >>$strfile
	echo "<PRE>" >>$strfile
	$GREP '^A ' $BASE/log/error_log | sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' >>$strfile
	echo "</PRE>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count alert messages."
	echo "<P>PASS: $count alert messages.</P>" >>$strfile
fi

# Critical log messages
count=`$GREP '^C ' $BASE/log/error_log | wc -l | awk '{print $1}'`
if test $count != 0; then
	echo "FAIL: $count critical messages, expected 0."
	$GREP '^C ' $BASE/log/error_log
	echo "<P>FAIL: $count critical messages, expected 0.</P>" >>$strfile
	echo "<PRE>" >>$strfile
	$GREP '^C ' $BASE/log/error_log | sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' >>$strfile
	echo "</PRE>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count critical messages."
	echo "<P>PASS: $count critical messages.</P>" >>$strfile
fi

# Error log messages
count=`$GREP '^E ' $BASE/log/error_log | \
       $GREP -v '(usb) crashed on signal 11' | \
       $GREP -v '(dnssd) stopped with status 1' | \
       $GREP -v 'loadFile failed: temp file: not a PDF file' | \
       $GREP -v 'Failed to connect to system bus' | \
       $GREP -v -E 'Unable to open listen socket for address .* Address family not supported by protocol.' | \
       $GREP -v 'Unable to write uncompressed print data: Broken pipe' | \
       $GREP -v 'Cannot create the privileged socket.' | \
       wc -l | awk '{print $1}'`
if test $count != 33; then
	echo "FAIL: $count error messages, expected 33."
	$GREP '^E ' $BASE/log/error_log
	echo "<P>FAIL: $count error messages, expected 33.</P>" >>$strfile
	echo "<PRE>" >>$strfile
	$GREP '^E ' $BASE/log/error_log | sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' >>$strfile
	echo "</PRE>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count error messages."
	echo "<P>PASS: $count error messages.</P>" >>$strfile
fi

# Warning log messages
count=`$GREP '^W ' $BASE/log/error_log | $GREP -v CreateProfile | \
       $GREP -v 'Unable to initialize USB access via libusb, libusb error' | \
       $GREP -v 'org.freedesktop.ColorManager' | \
       $GREP -v -E 'Avahi client failed: -(1|26)' | \
       wc -l | awk '{print $1}'`
if test $count != 8; then
	echo "FAIL: $count warning messages, expected 8."
	$GREP '^W ' $BASE/log/error_log
	echo "<P>FAIL: $count warning messages, expected 8.</P>" >>$strfile
	echo "<PRE>" >>$strfile
	$GREP '^W ' $BASE/log/error_log | sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' >>$strfile
	echo "</PRE>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count warning messages."
	echo "<P>PASS: $count warning messages.</P>" >>$strfile
fi

# Notice log messages
count=`$GREP '^N ' $BASE/log/error_log | wc -l | awk '{print $1}'`
if test $count != 0; then
	echo "FAIL: $count notice messages, expected 0."
	$GREP '^N ' $BASE/log/error_log
	echo "<P>FAIL: $count notice messages, expected 0.</P>" >>$strfile
	echo "<PRE>" >>$strfile
	$GREP '^N ' $BASE/log/error_log | sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' >>$strfile
	echo "</PRE>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count notice messages."
	echo "<P>PASS: $count notice messages.</P>" >>$strfile
fi

# Info log messages
count=`$GREP '^I ' $BASE/log/error_log | wc -l | awk '{print $1}'`
if test $count = 0; then
	echo "FAIL: $count info messages, expected more than 0."
	echo "<P>FAIL: $count info messages, expected more than 0.</P>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count info messages."
	echo "<P>PASS: $count info messages.</P>" >>$strfile
fi

# Debug log messages
count=`$GREP '^D ' $BASE/log/error_log | wc -l | awk '{print $1}'`
if test $count = 0; then
	echo "FAIL: $count debug messages, expected more than 0."
	echo "<P>FAIL: $count debug messages, expected more than 0.</P>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count debug messages."
	echo "<P>PASS: $count debug messages.</P>" >>$strfile
fi

# Debug2 log messages
count=`$GREP '^d ' $BASE/log/error_log | wc -l | awk '{print $1}'`
if test $count = 0; then
	echo "FAIL: $count debug2 messages, expected more than 0."
	echo "<P>FAIL: $count debug2 messages, expected more than 0.</P>" >>$strfile
	fail=`expr $fail + 1`
else
	echo "PASS: $count debug2 messages."
	echo "<P>PASS: $count debug2 messages.</P>" >>$strfile
fi

#
# Log files...
#

echo "<H1><A NAME='LOGS'>4 - Log Files</A></H1>" >>$strfile

echo "<H2><A NAME='access_log'>access_log</A></H2>" >>$strfile
echo "<PRE>" >>$strfile
sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' $BASE/log/access_log >>$strfile
echo "</PRE>" >>$strfile

echo "<H2><A NAME='error_log'>error_log</A></H2>" >>$strfile
echo "<PRE>" >>$strfile
$GREP -v '^d' $BASE/log/error_log | sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' >>$strfile
echo "</PRE>" >>$strfile

echo "<H2><A NAME='page_log'>page_log</A></H2>" >>$strfile
echo "<PRE>" >>$strfile
sed -e '1,$s/&/&amp;/g' -e '1,$s/</&lt;/g' $BASE/log/page_log >>$strfile
echo "</PRE>" >>$strfile

#
# Format the reports and tell the user where to find them...
#

cat $root/share/cups-test/str-trailer.html >>$strfile

echo ""

if test $fail != 0; then
    ${SETCOLOR_FAILURE}
	echo "$fail tests failed."
	${SETCOLOR_NORMAL}
else
    ${SETCOLOR_SUCCESS}
	echo "All tests were successful."
	${SETCOLOR_NORMAL}
fi

echo "Log files can be found in $BASE/log."
echo "A HTML report was created in $strfile."
echo ""

if test $fail != 0; then
	echo "Copies of the error_log and `basename $strfile` files are in"
	echo "`pwd`."
	echo ""

	exit 1
fi
