#!/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.8.4 1998/12/27 13:10:18 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.
# 

bindir=/usr/X11R6/bin


#
# check for the first link
#
if [ ! -L /usr/X11R6/bin/X ]; then
	echo ""
	echo "There is no link /usr/X11R6/bin/X to /var/X11R6/bin/X!"
	echo "Please link the files as mentioned above or install the X-Server again."
	echo "I'm aborting now."
	exit 1
fi

#
# check for the second link
# 	
if [ ! -L /var/X11R6/bin/X ]; then
	echo ""
	echo "There is no link /var/X11R6/bin/X to /usr/X11R6/bin/XF86_xxxx which is"
	echo "the binary of the installed X-Server."
	echo "Please link the files as mentioned above or install the X-Server and"
	echo "run xf86config again."
	echo "I'm aborting now."
	exit 1
fi

#
# check if the XF86_xxxx file is an executable
#
if [ ! -x /var/X11R6/bin/X ]; then
	echo ""
	echo "The file XF86_xxxx (binary of X-Server) doesn't seem to be a binary file."
	echo "Please check it or / and install a new X-Server-binary."
	echo "I'm aborting now."
	exit 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 test -n "`tty | cut -c 10-`"; then
	echo "You can only start X11 from your local machine"
	echo "and not if logged in via network."
	exit 1
fi






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

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

# serverargs="$serverargs -auth $HOME/.Xauthority"
# xauth add $display . `mcookie`
exec xinit $clientargs -- $serverargs
