:
#
#	@(#) startx.cpp 1.2 92/08/14 
#

XFONTS=/usr/lib/X11/fonts/misc\;/usr/lib/X11/fonts/75dpi\;/usr/lib/X11/fonts/100dpi
XPATH=/usr/bin/X11
SYS_RC_DIR=/usr/lib/X11
SYS_RC_FILE=sys.startxrc
USER_RC_FILE=.startxrc
REMOTE_SERVER=
RETURN=

XINITONCE()
{
	xinit $*
	RETURN=$?
}

XINIT()
{
	ARGS="$*"
	xinit $ARGS
#
# For any startup error other than "Server already exists", give
# up now.
#
	if [ $? -eq 2 ]
	then
	    DISPGIVEN="false"
# Try alternate displays
	    for X in 1 2 3 4 5 6 7 0
	    do
		CMD=""
		SERVOPTS="false"
		DASHLAST="false"
# Generate commmand line for xinit.  If server options are given, insert the
# display number after the "--" and after the server program name if given.
# Otherwise append a "--" and display number to the argument list.
		for i
		do
		    case $i in
# Server options given
			--)     CMD=$CMD$i" " ;
				SERVOPTS="true" ;
				DASHLAST="true" ;; 
# Display number given
			*:*)    CMD=$CMD$i" " ;
				DASHLAST="false" ;
				if test "$SERVOPTS" = true
				then
				    DISPGIVEN="true" 
				fi ;;
# Server program name given
			/*|.*|[a-z]*|[A-Z]*)    CMD=$CMD$i" " ;;
			*)	if test "$DASHLAST" = true
				then
# Server options given but no display number so insert it into command line
# after the "--" and after the server program name if specified.
				    CMD=$CMD:${X}" "
				fi ;
				CMD=$CMD$i" " ;
				DASHLAST="false";;
		    esac
		done
		if test "$DASHLAST" = true
		then
# Server options given but no display number so insert it into command line
# after the "--" and after the server program name if specified.
		    CMD=$CMD:${X}" "
		fi
		if test "$SERVOPTS" = false
		then
# No server options given so append "--" followed by display number
		    CMD=$CMD" -- ":${X}
		fi

		if test "$DISPGIVEN" = true
		then
			break
		fi

		echo Trying display $X.

		XINITONCE  $CMD
#
# If xinit failed due to "Server already exists", then we might
# want to try again.
#
		if [ $RETURN -eq 2 ]
		then
		    if test "$DISPGIVEN" = true
		    then
# Display number given on command line so don't bother trying another display
			break
		    else
			:
		    fi
		else
		    break
		fi
	    done
	fi
}

if [ -f /usr/lib/merge/xmergeset.sh ]
then	. /usr/lib/merge/xmergeset.sh
elif [ -f /usr/lib/merge/console.disp ]
then	XMERGE=`cat /usr/lib/merge/console.disp`
	export XMERGE
fi

# 
case $1 in

-t)
        if [ $DISPLAY ] 
        then   REMOTE_SERVER=TRUE
        else   echo "DISPLAY environment variable not set"
               exit
        fi
 
	;;
*)
	;;
esac

# If the user already has XPATH in their PATH, don't change the path 
if expr $PATH : ".*\/usr\/bin\/X11" >/dev/null
then :
else 
#   Add XPATH to user's PATH
	PATH=${PATH}:${XPATH}
fi
export PATH XFONTS

# Now make sure DISPLAY is set. If not set to ":0.0" (i.e. local default 
# display) so clients that don't default reasonably don't choke.

[ "$DISPLAY" = "" ] 
case $? in
0)
	DISPLAY=:0.0
	export DISPLAY
	;;
esac

# Test if .startxrc is older than /usr/lib/X11/odt.update.
# If it is run /usr/lib/X11/odt.update.
#
RCFILE=$HOME/$USER_RC_FILE
PHX=odt.update
PHXPATH=/usr/lib/X11

NEEDS_UPDATE=
if [ x`find $PHXPATH -name $PHX -newer $RCFILE -print 2>/dev/null` != x ] 
then
	NEEDS_UPDATE=TRUE
fi

if [ $NEEDS_UPDATE ]
then   if [ $REMOTE_SERVER ]
       then   exec /bin/sh  ${PHXPATH}/$PHX 
       else   XINIT /bin/sh ${PHXPATH}/$PHX
       fi

       exit
fi

# Now start X and the intial clients specified in either $HOME/.startxrc
# or $SYS_RC_DIR/$SYS_RC_FILE.

if [ -f ${HOME}/${USER_RC_FILE} ] 
then   if [ $REMOTE_SERVER ]
       then   exec /bin/sh ${HOME}/${USER_RC_FILE} 
       else   XINIT /bin/sh ${HOME}/${USER_RC_FILE} $*
       fi

elif [ -f ${SYS_RC_DIR}/${SYS_RC_FILE} ] 
then   if [ $REMOTE_SERVER ]
       then   exec /bin/sh ${SYS_RC_DIR}/${SYS_RC_FILE}
       else   XINIT /bin/sh ${SYS_RC_DIR}/${SYS_RC_FILE} $*
       fi

else   if [ $REMOTE_SERVER ]
       then   :
       else   XINIT $*
       fi
fi
