#!/bin/sh
# @(#)18	1.12  src/gos/2d/XTOP/programs/xinit/startx.cpp, xclients, gos720 12/18/97 15:14:36 
#
# COMPONENT_NAME: XCLIENTS
#
# FUNCTIONS: startx.cpp
#
# ORIGINS: 27 
#
#  (C) COPYRIGHT International Business Machines Corp. 1992, 1993
#  All Rights Reserved
#  US Government Users Restricted Rights - Use, duplication or
#  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
# 
#  Copyright 1989 Purdue Research Foundation
# 
#  Permission to use, copy, modify, distribute, and sell this software 
#  and its documentation for any purpose is hereby granted without fee, 
#  provided that the above copyright notice appear in all copies and that 
#  both that copyright notice and this permission notice appear in 
#  supporting documentation, and that the name of Purdue not be used in 
#  advertising or publicity pertaining to distribution of the software 
#  without specific, written prior permission.  Purdue makes no 
#  representations about the suitability of this software for any purpose.  
#  It is provided "as is" without express or implied warranty.
# 
#  PURDUE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
#  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL 
#  PURDUE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 
#  ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
#  WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 
#  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
#  SOFTWARE.
# 
#  Author:  George Kraft IV (gk4@staff.cc.purdue.edu)
#           Purdue University Computing Center
#           Mathematical Sciences Building
#           West Lafayette, IN 47907
# 
#  startx(1) - Robustly start X for a workstation, X terminal,
#              or PC with X server software by employing the 
#              user configuration files provided.
#			

PATH=$PATH:/usr/lpp/X11/bin
WAIT="false"
SYSXINITRC="/usr/lpp/X11/defaults/xinitrc"
SYSXDEFAULTS="/usr/lpp/X11/defaults/Xdefaults.tmpl"
SOCKDIR="/tmp/.X11-unix"
XCLEANUP="stty sane"

#	
#	Current working directory is home.
#

cd

#
#  Parse the arguments.
#

usage="$0 [-d DISPLAY:0] [-t | -w] [-x <startup> | [-r <resources>] [-m <wmgr>]] [-wait]"

# How to chop an argument off.
cut='P=$1
shift
set __ -`expr "$P" : '\''-.\(.*\)'\''` "$@"; shift'

# "dwm:r:x:"
while [ $# -gt 0 ]; do
	case "$1" in
	-display)
		export DISPLAY
		DISPLAY=$2
		dpynum=":`echo $DISPLAY | sed -e 's/\(.*\):\(.*\)/\2/'`"
		shift ; shift
		DFLAG=true
		;;
	-display*)
		export DISPLAY
		DISPLAY=`expr "$1" : '-display\(.*\)'`
		dpynum=":`echo $DISPLAY | sed -e 's/\(.*\):\(.*\)/\2/'`"
		shift
		DFLAG=true
		;;
	-d)
		export DISPLAY
		DISPLAY=$2
		dpynum=":`echo $DISPLAY | sed -e 's/\(.*\):\(.*\)/\2/'`"
		shift ; shift
		DFLAG=true
		;;
	-d*)
		export DISPLAY
		DISPLAY=`expr "$1" : '-d\(.*\)'`
		dpynum=":`echo $DISPLAY | sed -e 's/\(.*\):\(.*\)/\2/'`"
		shift
		DFLAG=true
		;;
	-t)
		if [ "true" = "${WFLAG-}" ]; then 
			echo "$usage" 2>&1
			exit
		fi
		TFLAG=true
		WFLAG=false
		shift
		;;
	-t*)
		if [ "true" = "${WFLAG-}" ]; then 
			echo "$usage" 2>&1
			exit
		fi
		TFLAG=true
		WFLAG=false
		eval "$cut"
		;;
	-wait)
		WAIT="true"
		shift
		;;
	-w)
		if [ "true" = "${TFLAG-}" ]; then 
			echo "$usage" 2>&1
			exit
		fi
		WFLAG=true
		TFLAG=false
		shift
		;;
	-w*)
		if [ "true" = "${TFLAG-}" ]; then 
			echo "$usage" 2>&1
			exit
		fi
		WFLAG=true
		TFLAG=false
		eval "$cut"
		;;
	-r)
		xdefaults=$2
		shift ; shift
		;;
	-r*)
		xdefaults=`expr "$1" : '-r\(.*\)'`
		shift
		;;
	-m)
		wmrc=$2
		shift ; shift
		;;
	-m*)
		wmrc=`expr "$1" : '-m\(.*\)'`
		shift
		;;
	-x)
		xinitrc=$2
		shift ; shift
		;;
	-x*)
		xinitrc=`expr "$1" : '-m\(.*\)'`
		shift
		;;
	--)
		shift
		break
		;;
	-*)
		echo "$usage" 1>&2 
		exit 1
		;;
	*)
		break
		;;
	esac
done

if [ $# -gt 0 ]; then
	echo "$usage" 1>&2 
	exit 1
fi

if [ "${DFLAG-}" = "true" ]; then
	echo $DISPLAY | grep ":" > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "Missing server number on \"$DISPLAY\"" 1>&2
		echo "$usage" 1>&2 
		exit 1
	fi
fi

if [ "" != "${xinitrc-}" ]; then 
	if [ "" != "${wmrc-}" ]; then 
		echo "$usage" 1>&2 
		echo "Window manager option unexpected with X startup option." 1>&2
		exit 1
	fi
	if [ "" != "${xdefaults-}" ]; then 
		echo "$usage" 1>&2 
		echo "X resources option unexpected with X startup option." 1>&2
		exit 1
	fi
fi

TTYNAME=`tty | awk 'BEGIN {FS = "/"} {print $3}'`

if [ "${WFLAG-}" = "" ]; then
	case "$TTYNAME" in
	console | co | lft*)
		WFLAG="true"
		TFLAG="false"
		;;
	*)
		TFLAG="true"
		WFLAG="false"
		;;
	esac
fi

#
#	Find a xrdb(1) configuration file.
#

xrdbconfigs="${xdefaults-} .Xdefaults .xdefaults .Xresources .xresources ${SYSXDEFAULTS-}"

for config in $xrdbconfigs; do
	if [ -r $config -a -s $config ]; then
		xrdbconfig="$config"
		break;
	fi
done

#
#	Find a X configuration file.
#

xconfigs="${xinitrc-} ${XINITRC-} .Xinit .xinit .Xinitrc .xinitrc .xsession ${SYSXINITRC-}"

for config in $xconfigs; do
	if [ -r $config -a -s $config ]; then

		xconfig="$config"

                #
    		# Determine the shell of the X config file.
    		#	

		if [ "`head -1 $xconfig | grep "/bin/sh"`" ]; then
			shell="/bin/sh"
		elif [ "`head -1 $xconfig | grep ":"`" ]; then
			shell="/bin/sh"
		elif [ "`head -1 $xconfig | grep "/bin/csh"`" ]; then
			shell="/bin/csh"
		elif [ -f /bin/ksh ]; then
			shell="/bin/ksh"
		else
			shell="/bin/csh"
		fi

		break;
	fi
done

#
#	Find a window manager configuration file.
#

wmgrconfigs="${wmrc-} .mwmrc .twmrc .awmrc .uwmrc .gwmrc .olwmrc"

for config in $wmgrconfigs; do
	if [ -r $config -a -s $config ]; then
		wmgrconfig="$config"
		break;
	fi
done

#
#	Determine which error file to use.
#

xerrors=${XERRORS=.xerrors}

#
#	Does the user know which DISPLAY to send to? 
#

if [ "${TFLAG-}" = "true" -a "${DISPLAY-}" = "" ]; then
	export DISPLAY
	DISPLAY=`who am i | sed -e 's/\(.*\)(\(.*\))/\2/' | cut -f1 -d" "`:0 > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		echo "Environment variable DISPLAY was set to $DISPLAY" 2>&1
	else
		echo "$usage" 2>&1
		echo "Uknown server DISPLAY." 2>&1
		exit 1
	fi
fi

if [ "" != "${DISPLAY-}" ]; then
	echo $DISPLAY | grep -v ":" && DISPLAY=$DISPLAY:0	> /dev/null 2>&1
fi

#
#	Start X.
#

if [ "${xconfig-}" != "" ]; then
	if [ -r $xconfig -a -s $xconfig ]; then
		if [ "${WFLAG-}" = "true" -a "${DFLAG-}" = "true" ]; then
			xinit /bin/sh -c \
				"DISPLAY=$DISPLAY $shell $xconfig" -- $dpynum \
				> $xerrors 2>&1; ${XCLEANUP-}
			exit 0
		elif [ "${WFLAG-}" = "true" ]; then
			xinit /bin/sh -c "$shell $xconfig" \
				> $xerrors 2>&1; ${XCLEANUP-}
			exit 0
		else
			echo "Please wait - starting your session"
			if [ "true" = "${WAIT}" ]; then
				(exec $shell $xconfig > $xerrors 2>&1 )
			else
				($shell $xconfig > $xerrors 2>&1 & )
			fi

			exit 0
		fi
	fi
fi

if [ "${WFLAG-}" = "true" -a "${DFLAG-}" = "true" ]; then
	(xinit /bin/sh -c \
		"DISPLAY=$DISPLAY xterm -C" -- $dpynum \
		> $xerrors 2>&1; ${XCLEANUP-} &)
	exit 0
elif [ "${WFLAG-}" = "true" ]; then
	(xinit /bin/sh -c "xterm -C" > $xerrors 2>&1; ${XCLEANUP-} &)
fi

if [ "${WFLAG-}" = "true" ]; then
	sleep 10
	if [ -r ${XAUTHORITY-.Xauthority} ]; then
		if [ -s .${XAUTHORITY-.Xauthority} ]; then
			echo "Empty ${XAUTHORITY-.Xauthority} file." \
				> $xerrors 2>&1
		else
			xhost + > $xerrors 2>&1
		fi
	fi
else
	echo "Suspend your Telnet(1C) session and wait for the window manager."
fi

if [ "${xrdbconfig-}" != "" ]; then
	xrdb -retain $xrdbconfig > $xerrors 2>&1
fi

case "$wmgrconfig" in
.awmrc)
	(awm > $xerrors 2>&1 &)
	;;
.uwmrc)
	(uwm > $xerrors 2>&1 &)
	;;
.twmrc)
	(twm > $xerrors 2>&1 &)
	;;
.gwmrc)
	(gwm > $xerrors 2>&1 &)
	;;
.olmrc)
	(olwm > $xerrors 2>&1 &)
	;;
.mwmrc | *)
	(mwm > $xerrors 2>&1 &)
	;;
esac

if [ "${xrdbconfig-}" != "" ]; then
	xrdb $xrdbconfig > $xerrors 2>&1
fi

if [ "${WFLAG-}" = "true" ]; then
	wait
else
	if [ "true" = "${WAIT}" ]; then
		(exec xterm > $xerrors 2>&1)
	else
		(xterm > $xerrors 2>&1 &)
	fi
fi