#!/bin/sh

#	xr5 is a shell script front end to the X Windows system.
#	It hides all of the details of the way that the X software
#	has been installed but the individual details can be gleaned
#	for use by other software (for example, the location of the
#	library archive file for Xlib)  There is a lot of Sun dependent
#	code in this script but it should not be too difficult to remove
#	the Sun stuff and add whatever is needed for other systems.
#
#		Copyright (c) C. M. Crampton, RAL, 1989
#	Changed to run release 3. June 1989 T. Watson.
#	Changed to run release 4. 1990 C. Goswell
#	Added XFILESEARCHPATH so that the application default files are 
#	picked up
#	April 1990 T. Watson.

#	XINSTALLDIR is the head of the directory hierarchy which
#	contains the X installation files (binaries, manual pages,
#	include files and so on). With Suns, the directory used is
#	switched on by machine type and OS to handle different
#	environments.
#	Changed to run release 5. October 1991 Lakshmi Sastry

DEFAULT_XINSTALLDIR=/u/XR5/xsparc

	if [ "`foption`" -eq 68881 ]
	then
		DEFAULT_XINSTALLDIR=/u/XR5/xmc68000
	else
		
		DEFAULT_XINSTALLDIR=$DEFAULT_XINSTALLDIR
	fi


#	The default XINSTALLDIR (worked out above) can be over-ridden
#	by an environment variable of the same name.
XINSTALLDIR=${XINSTALLDIR-$DEFAULT_XINSTALLDIR}

#	Check number of args.
#	Zero args causes the help info to be printed.
case $# in
	0)	OK=false	;;
	*)	OK=true		;;
esac

STATUS=0	# exit status
XTK=false	# whether the X toolkit is also required
XDEV=		# use default device unless -mono or -colour argument
XSERVER=$XINSTALLDIR/bin/X	# use default server unless other is specified
while [ $# != 0 ]	# loop through for each argument
do
    NEXTARG=$1; shift

    case "$NEXTARG" in

	-start)	#	set everything up and start the server
		
		PATH=$XINSTALLDIR/bin:$PATH:/usr/openwin/bin	
export PATH
		RGBPATH=$XINSTALLDIR/lib/X11/rgb	
export RGBPATH
		XERRORDB=$XINSTALLDIR/lib/X11	
export XERRORDB
		XFONTPATH=${XFONTPATH-$XINSTALLDIR/lib/X11/fonts/misc,$XINSTALLDIR/lib/X11/fonts/75dpi}
							
export XFONTPATH
                XFILESEARCHPATH=${XFILESEARCHPATH-$XINSTALLDIR/lib/X11/%L/%T/%N%S:$XINSTALLDIR/lib/X11/%l/%T/%N%S:$XINSTALLDIR/usr/X11/%T/%N%S}
							
export XFILESEARCHPATH
		
		if [ -f /bin/ldd ]	# crude test for Sun OS4
		then
			if [ -z "$LD_LIBRARY_PATH" ]
			then LD_LIBRARY_PATH=$XINSTALLDIR/lib
			else LD_LIBRARY_PATH=$XINSTALLDIR/lib:$LD_LIBRARY_PATH
			fi
		        export LD_LIBRARY_PATH
		fi
		
		if [ -z "$MANPATH" ]
		then
			MANPATH=$XINSTALLDIR/man:/usr/man
		else
			MANPATH=$XINSTALLDIR/man:$MANPATH
		fi
		export MANPATH
		if [ -z "$INCLUDEPATH" ]
		then
			INCLUDEPATH=$XINSTALLDIR/include
		else
			INCLUDEPATH=$XINSTALLDIR/include:$INCLUDEPATH
		fi
		export INCLUDEPATH
		
		
		if [ -n "$WINDOW_PARENT" -a -r /dev/cgtwo0 ] && ( sun3 ) 2>/dev/null
		then
			echo "Problems with cgtwo frame buffers \c"	1>&2
			echo "when X is started from within SunView"	1>&2
			echo "- quit SunView and start X"		1>&2
			break
		fi
		
		
#		if which kbd_mode | grep -s -v '^no kbd_mode'
#		then 
#			#	we need to save the current setting so it
#			#	can be restored after the server has exited
#			#	[ ugly sed script courtesy Mark Martin @ RAL ]
#			KBDMODE=`kbd_mode 2>&1 | tail -1 | \
#				sed -n 's/Current mode is \([^ ]\).*/-\1/p'`
#			case $KBDMODE
#			in
#			    -[auen])	;;	# okay
#			    *)	echo "Unable to read keyboard mode\c"	1>&2
#			    	echo " - too hairy to continue" 	1>&2
#				exit 1
#				;;
#			esac
#	
#			#	on error or exit:
#			#		reset keyboard
#			#		if on a colour machine:
#			#			clear the colour maps
#			#			refresh suntools, if running
#			trap '	kbd_mode $KBDMODE ;
#				[ ! "(" -r /dev/bwtwo0 -a ! -r /dev/cgfour0 ")" ] &&
#				    ( clear_colormap ;
#				      [ -n "$WINDOW_PARENT" ] &&
#					
#				shelltool -Wp 0 0 -Ws 2000 2000 /bin/true )
#				
#				exit 0' 0 1 2 3 15
#			kbd_mode -u	
#				# set unencoded prior to starting server
#		fi
		#	start the server, use XINITARGS if set
		xinit $XINITARGS $@ -e $0 -init -- $XSERVER $XDEV -ar1 500 -fp $XFONTPATH -co $RGBPATH

		
		break
		;;

	-init)	#	called from xr5 -start
		#	Currently only sets up the resource data base
		#	Any database would be stored in a file called
		#	~/.Xdefaults but may have an extra tag built from
		#	the hostname of the machine
		#	[ Code courtesy Mark Martin @ RAL ]
		HOST=`hostname`
		if [ -f $HOME/.Xdefaults-$HOST ]
		then
			RDB=$HOME/.Xdefaults-$HOST
		else
			RDB=$HOME/.Xdefaults
		fi

		{	echo '*StringConversionWarnings:on'	
			# a good idea
			echo 'xman*helpFile:'"\c"
			echo $XINSTALLDIR"\c"
			echo 'lib/X11/xman.help'
			if [ -f "$RDB" ]
			then
				cat $RDB
			fi
		} | xrdb
		
		[ -z "$SHELL" ] && SHELL=/bin/sh	# ensure a shell
		exec $SHELL -i
		;;

	-capslock)
		#	disable the caps lock key -- can be confusing,
		#	especially on Suns, as it is easy to lock by 
		#  accident
		xmodmap -e 'clear lock'
		;;

	-fp)	#	append to fontpath
		#	[ sed script courtesy Mark Martin @ RAL ]
		F=$1 ; shift
		xset fp `xset q |
		  sed -n '/Font Path:/s;Font Path: *\(.*\) *$;\1,'"$F"';p'`
		;;

	-xtk)	XTK=true
		;;

	-dir)	echo "$XINSTALLDIR"
		;;

	-path)	echo  "$XINSTALLDIR/bin\c"
		;;

	-l)	/usr/ucb/echo -n "-L$XINSTALLDIR/lib" 
		$XTK && /usr/ucb/echo -n " -lXt -lXaw" 
		/usr/ucb/echo " -lX11"
		;;

	-L)	echo "-L$XINSTALLDIR/lib"
		;;

	-I)	echo "-I$XINSTALLDIR/include"
		;;

	-lint)	$XTK && \
		( echo "$XINSTALLDIR/lib/lint/llib-lXt.ln\c"
		  echo " $XINSTALLDIR/lib/lint/llib-lXaw.ln \c" )
		echo "$XINSTALLDIR/lib/lint/llib-lX11.ln"
		;;

	-man)	if [ $# = 0 ]
		then
			echo "$XINSTALLDIR/man"
		else
			MANPATH="$XINSTALLDIR/man":$MANPATH man $@
			break
		fi
		;;

	-mono)	#	Run mono even on machines capable of running
		#	in colour.
		XDEV="-mono"
		;;

	-colour)
		#	run in colour mode if available
		#	only has effect on cgfour equipped Suns from
		#	outside Suntools
		if [ -r /dev/cgfour0 -a -z "$WINDOW_PARENT" ]
		then
			XDEV="-dev /dev/cgfour0"
		fi
		;;

	-server)
		XSERVER=$1 ; shift
		;;

	-rsh)	#	run an X utility on a remote machine but
		#	interacting locally
		if [ -z "$DISPLAY" ]	# check for a local display
		then
			echo "$0: No DISPLAY variable set"	1>&2
			exit 1
		fi

		#	check DISPLAY and munge if necessary
		case $DISPLAY
		in
			unix:*)	#	change unix bit into hostname
				HOST=`hostname`
				DISPLAY=` $DISPLAY | sed -n "s/unix/$HOST/p"`
				;;
		esac

		#	extract name of target host and check that remote
		#	machine is permitted to communicate with local server
		RHOST=$1; shift
		if ( xhost | tail +2 | egrep $RHOST ) 2>/dev/null 1>&2
		then
			: okay
		else
			#	got permit local host
			echo "Host $RHOST is being permitted to access local display" 1>&2
			xhost $RHOST && \
			echo "Use xhost -$RHOST to undo" 1>&2
		fi
		
		#	start rsh  - note: expects this script to be called
		#	xr4 and to be in the current search path.
		rsh $RHOST -n exec sh -c "'PATH=$PATH DISPLAY=$DISPLAY export PATH DISPLAY ; exec xr5 -exec $@ >/dev/null 2>&1'"
		break
		;;

	-exec)	#	PATH probably already has some X bin directory
		#	but possibly the wrong one so prepend local bin
		PATH=$XINSTALLDIR/bin/X11:$PATH	export PATH
		if [ -z "$LD_LIBRARY_PATH" ]
		then LD_LIBRARY_PATH=$XINSTALLDIR/lib:/usr/openwin/lib
		else LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$XINSTALLDIR/lib:/usr/openwin/lib
		fi
		export LD_LIBRARY_PATH
                XFILESEARCHPATH=${XFILESEARCHPATH-$XINSTALLDIR/%L/%T/%N%S:$XINSTALLDIR/%l/%T/%N%S:$XINSTALLDIR/%T/%N%S}
							export XFILESEARCHPATH
		exec $@
		;;

	*)	OK=false	# bad argument
		STATUS=1	# bad status
		break
		;;
	esac
done

#	if "OK" has been set to false, print help info
$OK ||
{	CMD=`basename $0`
	echo 1>&2 "$CMD: (X Windows front end) usage:

-xtk		assume the X toolkit is being used with other options
			e.g: $CMD -xtk -l
-start		start X Windows server. Following args are passed to xinit
-rsh		start a remote X client: $CMD -rsh host cmd args...
-mono		use monochrome output even on colour machines
-colour		use colour output mode if available
-server		the following argument is used as the X server
		    instead of the default (X)
-capslock	disables the caps lock key (especially useful on Suns)
-fp		append to the current font path (argument should follow)
-dir		echo the X directory which may contain sources etc.
-path		echo the X directory which may contain installed X binaries.
-l		echo args to ld for library
-L		echo args to ld for library directory
-I		echo args to cc for X include files
-lint		echo args to lint
-man		echo directory where X manual pages can be found or,
			if an argument follows, try to print the manual
			page specified (on the terminal)"
}
exit $STATUS
