#!/bin/sh
# 
# $Header: xswitch,v 1.3 89/06/08 10:52:45 root Exp $ 
# $Locker:  $ 
#

umask 2

# Xswitch will only run on a Vax in the Athena environment.
machine="`/bin/athena/machtype`"

case $machine in
        rt)
                echo "You cannot run an X10 server on an IBM/RT. If you"
                echo "need to run the X10 server, switch to a Vax."
                echo
                echo "Thank you."
                exit 1
                ;;
        vax)
                ;;
        *)
                echo "Unknown machine $machine - Contact Athena Operations"
                exit 1
                ;;
esac

# This machine is a vax

VERSION=`/mit/x10/vaxbin/xversion`

if [ $VERSION = 11 ]
then
	NEWVERSION=10
elif [ $VERSION = 10 ]
then
	NEWVERSION=11
	echo "The only way to go back to X11 is to logout and login again."
	exit 1
else
	/bin/echo "?Cannot determine X version; X may not be running"
	exit 1
fi

XBINLINK=/tmp/Xbin

if [ -f /usr/lib/rgb.dir -a ! -f /usr/lib/X11/rgb.dir ]
then
	RGB="-co /usr/lib/rgb"
else
	RGB=
fi

if [ $NEWVERSION = 11 ]
then

	       BIN=/usr/bin/X11

        SERVERARGS=":0 $RGB"
else
	BIN=/usr/bin/X10
	SERVERARGS="0"
fi

PROCEED=FALSE
RESTART=FALSE

while [ $# -gt 0 ]
do
	case $1 in

		-go|-g*)
			PROCEED=TRUE
			shift ;;

		-restart|-r*)
			if [ $VERSION = 11 ]
			then
			    NEWVERSION=11

				   BIN=/usr/bin/X11

  			    SERVERARGS=":0 $RGB"
			       RESTART=TRUE
			else
				/bin/echo "X11 is not running; -restart ignored"
			fi
			shift ;;

		*)
			/bin/echo 'Usage: xswitch [-restart] [-go]'
			exit 1 ;;

	esac
done

LOG=/tmp/xswitch.log
NL='
'

if [ ${SERVERBIN}x = x ]
then
	if [ -r $BIN/Xqvss ]
	then
		SERVER=Xqvss
	else
		SERVER=X`machtype -d`
	fi
	SERVERBIN=$BIN/$SERVER
fi

if [ ! -r $SERVERBIN ]
then
	/bin/echo "Server $SERVERBIN does not exist, exiting."
	exit 1
fi

if [ ! -f $BIN/xterm ]
then
	/bin/echo "xterm client does not exist, exiting."
	exit 1
fi

trap "" 0 1 3

if [ ${PROCEED}x != TRUEx ]
then
	if [ ${RESTART}x = TRUEx ]
	then
		/bin/echo -n "Confirm to kill and restart X11 (yes or no): "
		read ans
		if [ ${ans}x = yesx -o ${ans}x = YESx ]
		then
		    continue
		else
		    /bin/echo "not confirmed; ignoring xswitch request."
		    exit
		fi
	else
		/bin/echo \
		  "${NL}Confirm:  Hit return to switch to X$NEWVERSION" \
		  "(all current X processes will die)" \
		  "${NL}          or hit ^C to remain where you are:"
		read ans
	fi
fi

trap "" 2

if [ ${RESTART}x = TRUEx -o -f /tmp/xswitch.pid ]
then
	/bin/kill -9 `cat /tmp/xswitch.pid`
fi

if [ $NEWVERSION = 11 ]
then
	/bin/rm -f /tmp/X0
fi

/bin/rm -f $XBINLINK /tmp/xswitch.pid
/bin/ln -s $BIN $XBINLINK

(
	#
	# stop toehold/xlogin/Xsession
	#
	thpid="`/bin/ps ax | /bin/sed -n -e /sed/d -e 's/^ *\([0-9]*\) .* - ttyv0.*\$/\1/p'`"
	xtpid="`/bin/ps ax | /bin/sed -n -e /sed/d -e 's/^ *\([0-9]*\) .* -L .*$/\1/p'`"
	xspid="`/bin/ps ax | /bin/sed -n -e /sed/d -e 's/^ *\([0-9]*\) .* sh .*Xsession.*$/\1/p'`"
	/bin/kill -17 $thpid
	/bin/kill -17 $xtpid
	/bin/kill -17 $xspid
	#
	# kill the x server
	#
	Xpid=`/bin/ps ax | /bin/sed -n -e "s/^ *\([0-9]*\) co .*-.*0.* ttyv0.*\$/\1/p"`
	if [ ${Xpid}x = x ]
	then
		Xpid=`/bin/ps ax | /bin/sed -n -e "s/^ *\([0-9]*\) .*$SERVER.*\$/\1/p"`
	fi
	/bin/kill -1 $Xpid
	sleep 5
	/bin/kill -9 $Xpid

	echo "${NL}Starting X${NEWVERSION} server..." >/dev/console

	#
	# Start the x11 server and xterm
	#
	DISPLAY=:0; export DISPLAY
	HOST=`/bin/hostname`
	if [ $NEWVERSION = 11 ]
	then
		ARG="-display $HOST:0 -geometry 80x24+0+0"
	else
		ARG="$HOST:0 =80x24+0+0"
	fi
	#PATH=${XBIN}:${PATH}; export PATH
	set
# The following is a rude hack to fix a problem with the kernel - Ezra
	(sleep 15 ; /mit/x10/vaxlib/scanlines) &
	(sleep 20 ; /mit/x10/vaxlib/scanlines) &
	$BIN/xinit $BIN/xterm $ARG -- $SERVERBIN $SERVERARGS

	echo "xinit complete... restarting toehold"
	#
	# continue toehold
	#
	/bin/kill -19 $xspid
	/bin/kill -19 $xtpid
	/bin/kill -19 $thpid
	/bin/rm -f $XBINLINK /tmp/xswitch.pid
) > $LOG 2>&1 &

echo $! >/tmp/xswitch.pid

sleep 10
exit
