#!/bin/bash
#
#
# Xprobe - start an Accelerated-X probing Xserver and Xsetup
#
#
# $XiGId: Xprobe,v 1.1 2000/11/06 18:00:19 evanson Exp $
#
###################################################################
#set -x			# uncomment for debugging.

# Some startup functions.  Most of this hacked out of the XiG installer.

## See if this OS/Install can start an X server...
##
CanStartX()
{
	local xserver=""
	local ttydev="$(tty)"

	if [ ! -d "$XACCELHOME" ]
	then	# No dice dude.
		echo "## Cannot find $XACCELHOME.  Exiting"
		exit 1
	fi


	# If so, could we start one if we were feeling frisky?
	# - must be on a console/vt

	if [ "$SYSTYPE" = "Linux" ]
	then	# only Linux for now
		case "$ttydev" in
			/dev/tty[1-9])
					# it's risky, but it just might work
				;;
			*)
				echo "CanStartX: not on console, ttydev = $ttydev, cannnot start X"
				exit 1
				;;
		esac
	else
		echo "Probing is currently only suported on Linux systems."
		exit 1
	fi

	# so far so good...
	return 0
}

## StartXserver
##
StartXserver()
{	# StartXserver [ other Xserver ]

	local altserver="$1"

	# start the puppy: woof woof.

	if [ ! -z "$altserver" ]
	then	# an alternate server specified
		xserver="$altserver"
	else
		xserver="/usr/X11R6/bin/Xaccel"
	fi

	local PIDFILE=/var/run/server.0.pid

	if [ -e "$PIDFILE" ]
	then 
	    echo "## $PIDFILE exists, another X server is running on display :0"
	    echo "##  Please make sure that no Xservers are currently running"
	    echo "##  before running this script."
	    return 1
	fi


	# try to start it

	if [ -x $xserver ]
	then
		echo "## Killing gpm if it's running."
		killall gpm 

		echo "## StartXserver: Starting Xserver: $xserver -nobanner -fromprobe :0"

		local XSOCKET=/tmp/.X11-unix/X0

		# remove the socket to see if we can be sure the
		#  Xserver is ready to accept connections

		rm -f $XSOCKET

		# there is a desire here to show the user any 'probing' 
		#  messages as well as logging it for support purposes.

		$xserver -nobanner -fromprobe :0 &

		# and now for something completely different...

		local SPID=0
		local done=0
		local retrycount=6
		local thiscount=0

		while [ $done -eq 0 ]
		do
			sleep 2
			if [ -s $PIDFILE ]
			then # it's there... get the PID and test it
				SPID="$(cat $PIDFILE)"

				if [ $SPID -ne 0 ]
				then
					INSTALL_STARTEDX=$SPID
					# see if the socket is ready
					if [ -e $XSOCKET ]
					then
						done=1
					fi
				fi
			fi

			thiscount=$(($thiscount + 1))
			if [ $thiscount -gt $retrycount ]
			then	# hmm... not looking too promising.
				done=1
			fi

		done
				
		if [ $INSTALL_STARTEDX -eq 0 ]
		then	# something went wrong...
			echo "StartXserver: INSTALL_STARTEDX is 0 - server failed"
			return 1
		fi

		if kill -0 $INSTALL_STARTEDX 
		then	# it's still running... a diet rich in fiber.
			export DISPLAY=:0
			echo "## StartXserver: Xserver started, PID = $INSTALL_STARTEDX"
			return 0
		else	# it didn't run... a diet rich in chocolate
			echo "## StartXserver: Xserver startup failed - no such process"
			INSTALL_STARTEDX=0	# reset
			return 1
		fi
	else	# No Xserver
		echo "## StartXserver: Xserver $xserver missing or not executable"
		return 1
	fi

	# shouldn't get here... if so, we'll declare defcon 1 and launch
	#  a full retaliatory strike.
	echo "StartXserver: Shouldn't be here - returning error"
	return 1
}

## KillXserver
##
KillXserver()
{	# KillXserver

	if [ $INSTALL_STARTEDX -ne 0 ]
	then	# we started one (at least we think we did - kill it)
		echo "### Killing Xserver."
		kill -15 $INSTALL_STARTEDX
		#sleep 3	# wait a bit
	fi

	INSTALL_STARTEDX=0
	return
}

##
## MAIN
##
####################################################################

export XACCELHOME=/usr/X11R6/lib/X11/AcceleratedX
export INSTALL_STARTEDX=0
export SYSTYPE="$(uname -s)"
export XKEYSYMDB="$XACCELHOME/etc/XKeysymDB"
export INSTALL_STARTEDX=0

XSETUP=/usr/X11R6/bin/Xsetup

if [ ! -x "$XSETUP" ]
then
	echo "Cannot find $XSETUP, exiting."
	exit 1
fi

if CanStartX
then
	# Let's try it...
	if StartXserver "$1"	# $1 is optional server to use...
	then	# |<001. Lets start Xsetup
		$XSETUP -probe
		rv=$?
	else # failed...
		KillXserver
		exit 1
	fi

	KillXserver
fi

exit $rv # Jah.		
			
