#!/bin/ksh
PATH=$PATH:/usr/lib/windows/bin

let StartedPHI=0

TFILE=/tmp/$LOGNAME.qwin
tty >$TFILE
if grep -q net $TFILE; then
	rm $TFILE
	print "You're logged in on a netdev port so you're unable to run windows here."
	exit 1
else
	rm $TFILE
fi

if test ! /dev/grafx -ef /dev/grafx; then
	if test ! /dev/photon -ef /dev/photon; then
		print "Starting graphics driver..."
		eval ${WINGRAFX:-/usr/lib/windows/bin/gr.auto}
	else
		case $1 in
			-g*) GOPT=$1
				shift;;
		esac
		(/usr/lib/windows/bin/gr.phi -V $GOPT > /dev/null 2> /dev/null ) &
		let StartedPHI=1
	fi
	typeset -i i=0
	while test ! /dev/grafx -ef /dev/grafx -a $i -lt 5; do
		sleep 1;
		let i=i+1;
	done
	unset i
	if test ! /dev/grafx -ef /dev/grafx; then
		print "Unable to start graphics driver. Please check the installation"
		print "and configuration guide for more information."
		exit 1
	fi
fi

if test ! /dev/photon -ef /dev/photon; then
	exec /usr/lib/windows/bin/wnd $*
else
	if test $# -eq 0; then
		(slay -pQ qwslib && /usr/lib/windows/bin/qwslib)
		if [ ! -r /dev/shmem/qwslib32 ]; then
			/usr/lib/windows/bin/qwslib32
		fi
		/usr/lib/windows/bin/screen -N -m-
		if test $StartedPHI -ne 0; then
			slay gr.phi
		fi
	else
		exec /usr/lib/windows/bin/wnd $*
	fi
fi

#ifdef __USAGE
#%C [options]
#Options:
# -A        tries to abort the windows subsystem
# -c name   close the given picture
# -e        list pending events
# -f pid    find the queue id for the given process
# -i pid    display information about the given resource
# -q        quit
# -s        display status
# -S        sleep (revert to text mode)
# -W        wake (restore graphics mode)
# -g...     set window size for QW in Photon
# -d        disable tracing
#
#Examples:
#%C         (re)start the server
#%C -s      display current server status
#endif
