#! /bin/sh
# @(#)openxt.sh 23.41 91/09/14
#
# Start an OpenWindows session on a SUN X terminal.
# This script can be called with any of two arguments:
#	-includedemo	causes $OPENWINHOME/demo to be put in
#			the user's path
#	-remote		starts a remote olwm instead of using the
#			window manager utility in conjunction with
#			the terminal's local window manager

# This script is an analogue to the OpenWindows `openwin' command.  
# The `openwin' command is used to start an OpenWindows
# session from the console of a workstation.  This includes starting the
# X/NeWS server, the olwm window manager, and the X client programs
# described in the user's .openwin-init file.

# A user of a Sun X terminal can connect to a host using the
# terminal's telnet window and use this `openxt' script to perform a
# similar function.  In this case, the X server is already running (on
# the Sun X terminal); another server is not be started.

version=2.1

# environment variables that this shell script sets/changes:
export DISPLAY FONTPATH HELPPATH LD_LIBRARY_PATH
export MANPATH OPENWINHOME PATH XFILESEARCHPATH

# Get os version for use by msgprint routine
PATH=/usr/bin:$PATH
rel=`uname -r`
osmajor=`echo $rel | cut -d. -f1`
	
# Local routine:
msgprint(){
    if [ $osmajor = 5 ]
    then
	eval echo "\"`TEXTDOMAINDIR=\`pkginfo -r -v $version SUNWxt.\* 2>/dev/null\`/v$version/locale TEXTDOMAIN=tribble gettext \"$1\"`\""
    else
	eval echo "\"$1\""
    fi
}

# Get the pathname with which we were called. Use it to invoke
# xtname, xtsize, xtlwm and olwm_connect
if [ "`basename $0`" != "$0" ]
then
	progerrdir="`dirname $0`"
	progdir="${progerrdir}/"
else
	progdir=""
fi

# If $DISPLAY is not set and if the xtname program is installed, then use
# it to determine the name of the Sun X terminal to which we are connected.  
# If the xtname program does not return a name, ask the user if
# they are logged in from an X terminal.

if [ -z "${DISPLAY}" ]
then
	if [ -n "$progdir" -a \( ! -x ${progdir}xtname -o ! -x ${progdir}xtsize \) ]
	then
		msgprint '
	The Sun X Terminal user utilities were not installed correctly in
	$progerrdir' 1>&2
		exit 1
	fi

	disp=`${progdir}xtname`:0

	if [ $disp != ":0" ]
	then
		DISPLAY=$disp
	else
		msgprint '
	openxt must be run from a Sun X Terminal Login window only.' 1>&2
		exit 1
	fi

	${progdir}xtsize
fi

## IF $OPENWINHOME is not a directory,
##	print error message and then exit
OPENWINHOME="${OPENWINHOME-/usr/openwin}"
if [ ! -d $OPENWINHOME ]; then
    msgprint '
	OpenWindows is not installed in $OPENWINHOME.
	Set the environment variable \\$OPENWINHOME to the
	directory where OpenWindows is installed.' 1>&2
    exit 1
fi

REMOTE=0
INCLUDEDEMOPATH=0

# Parse command line args.
while [ $# -gt 0 ]; do
    case "$1" in
	-includedemo)
	    INCLUDEDEMOPATH=1
	    ;;
	-remote)
	    REMOTE=1
	    ;;
	*)
	    msgprint '
	usage: $0 <-remote> <-includedemo>' 1>&2
	    exit
	    ;;
	esac
	shift
done

# Local routine:
# Add to the path variable named by $1 the component $2.  $3 must be
# "append" or "prepend" to indicate where the component is added.
addpath () {
    eval value=\"\$$1\"
    case "$value" in
	*:$2:*|*:$2|$2:*|$2)
	    result="$value"
	    ;;
	"")
	    result="$2"
	    ;;
	*)
	    case "$3" in
		p*)
		    result="$2:${value}"
		    ;;
		*)
		    result="${value}:$2"
		    ;;
	    esac
    esac
    eval $1=$result
    unset result value
}

# Local routine:
# convenience routine which appends a string to a path.
append () {
    addpath "$1" "$2" append
}

# Local routine:
# convenience routine which prepends a string to a path.
prepend () {
    addpath "$1" "$2" prepend
}

# If -includedemo was specified on the command line,
# add $OPENWIN/demo to the user's path.
[ $INCLUDEDEMOPATH -eq 1 ] && prepend PATH $OPENWINHOME/demo

# Set up various environment variables
append	HELPPATH	$OPENWINHOME/lib/locale
append	HELPPATH	$OPENWINHOME/lib/help
prepend XFILESEARCHPATH	$OPENWINHOME/lib/locale/%L/%T/%N%S
append	XFILESEARCHPATH	$OPENWINHOME/lib/%T/%N%S
prepend PATH		$OPENWINHOME/bin

prepend LD_LIBRARY_PATH	$OPENWINHOME/lib
append	LD_LIBRARY_PATH	$OPENWINHOME/lib/server

if [ -d $OPENWINHOME/share/man ]; then
    case "$MANPATH" in
    "") MANPATH="$OPENWINHOME/share/man:/usr/man" ;;
    *)  prepend MANPATH $OPENWINHOME/share/man ;;
    esac
fi

# Add locale-specific support
#
if [ -f $OPENWINHOME/bin/set_locale_env ]; then
    . $OPENWINHOME/bin/set_locale_env
fi

# Get rid of our function definitions because a bug in some versions of
# the SunOS sh will turn their names into environment variables otherwise.
unset err addpath append prepend

# The rest of this script is analagous to .xinitrc. The
# user's .xinitrc file is not used because it starts an X/NeWs
# server, and in the case of an X terminal, an X server is
# already running on the terminal
# Check for initialization files or use defaults
xrdb -Usun $OPENWINHOME/lib/Xdefaults		# Load Default X11 resources
if [ -f $HOME/.Xdefaults ]; then
    xrdb -Usun -merge $HOME/.Xdefaults	# Load Users X11 resource database
fi
if [ -f $HOME/.OWdefaults ]; then
    xrdb -Usun -merge $HOME/.OWdefaults	# Load props-written resources
fi

#
# make connection with running window manager on server 
# or start remote olwm
#
msgprint '
	Starting your desktop. Please wait...'
if [ $REMOTE = 0 ]
then					# start remote-menu & signal pid
    if [ -n "$progdir" -a ! -x ${progdir}xtlwm -a ! -x ${progdir}olwm_connect ]
    then
	msgprint '
	The Sun X Terminal user utilities were not installed correctly in
	$progerrdir' 1>&2
	exit 1
    else
	${progdir}xtlwm
	lwm_exit=$?			# get exit status from xtlwm

	sleep 15 & pid=$!		# get script's pid and sleep 

	if [ ${lwm_exit} -eq 2 ]
	then
	    msgprint '
	Cannot connect to local window manager, starting remote wmgr' 1>&2
	    if [ -n "$REMOTE_WMGR" ] && [ -x "$REMOTE_WMGR" ]
	    then
		echo $REMOTE_WMGR
		$REMOTE_WMGR & pidwm=$!
	    else
		echo "
		$OPENWINHOME/bin/olwm"
		$OPENWINHOME/bin/olwm -syncpid $pid & pidwm=$!
	    fi
	    echo ""
	else
	    if [ \( ${lwm_exit} -eq 0 \) ]
	    then
		${progdir}olwm_connect -syncpid $pid & pidwm=$!
	    else
		exit 2			# don't setup desktop, 
	    fi				#   it probably already is
	fi
    fi
else
    sleep 15 & pid=$!			# get script's pid and sleep 
    if [ -n "$REMOTE_WMGR" ] && [ -x "$REMOTE_WMGR" ]
    then
	$REMOTE_WMGR & pidwm=$!
    else
	$OPENWINHOME/bin/olwm -syncpid $pid & pidwm=$!
    fi
fi
#
# Pause until window manager process inits or sleep exits
#
wait $pid


# If user has created a script to use instead of .openwin-init,
# then execute it, else execute .openwin-init.
if [ -n "$XTRC" ] && [ -x $XTRC ]
then
    $XTRC				# Custom xterminal setup
else
    if [ -x $HOME/.openwin-init ]
    then
	$HOME/.openwin-init		# Custom OpenWindows tools
    else
	$OPENWINHOME/lib/openwin-init	# Default OpenWindows tools
    fi
fi

exit 0
