
SERVER=${SERVER:-127.0.0.1:5899}

expect() {
	echo Expect "$1" For "$2"
	wget --output-document=/tmp/wget.$$ -q "$2"
	(sed 's/[<][^<>]*[>]/ /g' ; echo "" ) < /tmp/wget.$$ > /tmp/wget.$$a 
	echo "$1" > /tmp/wget.$$b
	diff -wubB /tmp/wget.$$a /tmp/wget.$$b || { echo >&2 "***FAILURE***" ; exit 13 ; }
	: sleep 1
}

killall Xvnc

for pass in once twice
do
	expect	OK	"http://$SERVER/shutdown"

	expect	1	"http://$SERVER/launch?id=111&url=http://xp.org/1"
	expect	1	"http://$SERVER/launch?id=111&url=http://xp.org/1"

	expect	2	"http://$SERVER/launch?id=222&url=http://xp.org/2"
	expect	2	"http://$SERVER/launch?id=222&url=http://xp.org/2"
	expect	1	"http://$SERVER/launch?id=111&url=http://xp.org/1"
	expect	1	"http://$SERVER/launch?id=111&url=http://xp.org/1"
	expect	2	"http://$SERVER/launch?id=222&url=http://xp.org/2"


	expect	3	"http://$SERVER/launch?id=333&url=http://xp.org/3"
	expect	2	"http://$SERVER/launch?id=222&url=http://xp.org/2"
	expect	3	"http://$SERVER/launch?id=333&url=http://xp.org/3"
	expect	1	"http://$SERVER/launch?id=111&url=http://xp.org/1"
	expect	3	"http://$SERVER/launch?id=333&url=http://xp.org/3"
	expect	1	"http://$SERVER/launch?id=111&url=http://xp.org/1"
	expect	2	"http://$SERVER/launch?id=222&url=http://xp.org/2"
	expect	3	"http://$SERVER/launch?id=333&url=http://xp.org/3"


	expect	4	"http://$SERVER/launch?id=444&url=http://xp.org/4"
	expect	2	"http://$SERVER/launch?id=222&url=http://xp.org/2"
	expect	4	"http://$SERVER/launch?id=444&url=http://xp.org/4"
	expect	3	"http://$SERVER/launch?id=333&url=http://xp.org/3"
	expect	4	"http://$SERVER/launch?id=444&url=http://xp.org/4"
	expect	1	"http://$SERVER/launch?id=111&url=http://xp.org/1"
	expect	4	"http://$SERVER/launch?id=444&url=http://xp.org/4"
	expect	3	"http://$SERVER/launch?id=333&url=http://xp.org/3"
	expect	1	"http://$SERVER/launch?id=111&url=http://xp.org/1"
	expect	2	"http://$SERVER/launch?id=222&url=http://xp.org/2"
	expect	3	"http://$SERVER/launch?id=333&url=http://xp.org/3"
	expect	4	"http://$SERVER/launch?id=444&url=http://xp.org/4"

done

expect	OK	"http://$SERVER/shutdown"

exit 0
