#!/bin/sh

# $XConsortium: startx.cpp,v 1.4 91/08/22 11:41:29 rws Exp $
# $XFree86: xc/programs/xinit/startx.cpp,v 3.0 1994/05/22 00:02:28 dawes Exp $
# 
# This is just a sample implementation of a slightly less primitive 
# interface than xinit.  It looks for user .xinitrc and .xserverrc
# files, then system xinitrc and xserverrc files, else lets xinit choose
# its default.  The system xinitrc should probably do things like check
# for .Xresources files and merge them in, startup up a window manager,
# and pop a clock and serveral xterms.
#
# Site administrators are STRONGLY urged to write nicer versions.
# 

# GPM can be a horrible interference, so we'll kill it off by default when
# X is started if you're using a PS/2 type mouse or any other kind of busmouse
# known to require exclusive access.  Added 1/3/96 by Patrick J. Volkerding.
MOUSE_TYPE=`ls -l /dev/mouse`
export MOUSE_TYPE;
export GPM_IS_DEAD;
MOUSE_TYPE=`echo $MOUSE_TYPE`
MOUSE_TYPE=`echo $MOUSE_TYPE | cut -f 11 -d ' '`
GPM_IS_DEAD=""
if [ "$MOUSE_TYPE" = "psaux" ]; then
  echo "Killing gpm..."
  gpm -k
  GPM_IS_DEAD=true
  sleep 1
fi

userclientrc=$HOME/.xinitrc
userserverrc=$HOME/.xserverrc
sysclientrc=/usr/X11R6/lib/X11/xinit/xinitrc
sysserverrc=/usr/X11R6/lib/X11/xinit/xserverrc
clientargs=""
serverargs=""

if [ -f $userclientrc ]; then
    clientargs=$userclientrc
else if [ -f $sysclientrc ]; then
    clientargs=$sysclientrc
fi
fi

if [ -f $userserverrc ]; then
    serverargs=$userserverrc
else if [ -f $sysserverrc ]; then
    serverargs=$sysserverrc
fi
fi

whoseargs="client"
while [ "x$1" != "x" ]; do
    case "$1" in
	/''*|\.*)	if [ "$whoseargs" = "client" ]; then
		    clientargs="$1"
		else
		    serverargs="$1"
		fi ;;
	--)	whoseargs="server" ;;
	*)	if [ "$whoseargs" = "client" ]; then
		    clientargs="$clientargs $1"
		else
		    serverargs="$serverargs $1"
		fi ;;
    esac
    shift
done

xinit $clientargs -- $serverargs

# Well, it would be nice if control returned to this point so the GPM daemon
# could be automatically restarted, but it looks like it can't.  Oh well, if
# you can find a working location for this code, here you can have it. :^)
if [ "GPM_IS_DEAD" = "true" ]; then
  touch /tmp/script.$$
  > /tmp/script.$$
  chmod 700 /tmp/script.$$
  grep gpm /etc/rc.d/rc.local >> /tmp/script.$$
  . /tmp/script.$$
  rm -f /tmp/script.$$
fi

