#!/bin/sh
#
#	NAME
#		XPstartup
#
#	SYNOPSIS
#		XPstartup [-display DISPLAY_NAME] [-xdm] [-debug]
#
#		Examples:
#		    $TEKXP_ROOT/bin/XPstartup &
#		    $TEKXP_ROOT/bin/XPstartup -display myXP:0.0 &
#
#	DESCRIPTION
#		A startup script to be used to initialize the TekXpress
#		and start the user's X session script.  It may be called
#		directly by the user, or as the X session started from xdm.
#
#		It starts an initial client to prevent user configuration 
#		from being lost when the server is reset on last client close.
#		If the -xdm option is given, or if the environment variable 
#		XDM_INITIATED is non-NULL, it does not start this initial 
#		client, since xdm does this.  This option can also be used
#		when the user has set the terminal to "Retain X Settings".
#		It loads the keymap files: the system files to initialize
#		compose sequences and diacriticals, and the user $HOME/.Xkm 
#		file, using the script $TEKXP_ROOT/bin/XPkeymap.
#		Currently, compose sequences are supported only for European
#		VT200 keyboards set to X-style layouts (x_esc and x_f11),
#		as DEC Xlib supports compose sequences internally.
#		It loads system, site, and user Xresources.
#		It then execs either the user, site, or system X session
#		file, whichever is found first. 
#
#	OPTIONS
#	    -display
#		Specifies the display, overriding $DISPLAY.
#	    -xdm
#		Prevents starting a "session keepalive" client.
#	    -debug
#		Turns on script trace (set -x).
#
#	ENVIRONMENT VARIABLES
#	    DISPLAY
#	        points to the TekXpress display.  Overridden by the
#	        -display argument.
#	    TEKXP_ROOT
#	        contains the path to the XP directory tree that
#	        contains the following files.  Default is
#	        "/usr/lib/X11/XP".
#	    XP_ROOT
#	        Old equivalent of TEKXP_ROOT used in V3 XPstartup;
#	        "/usr/lib/X11".  Overridden by TEKXP_ROOT.
#	    PATH
#	        Must include the host-supplied X bin directory, to obtain
#		the xterm and xrdb programs.  Other X utilities are obtained
#	        from $TEKXP_BIN, else $TEKXP_ROOT/bin.  Programs in
#	        $TEKXP_ROOT/bin are linked at installation to one of 4300,
#	        XD88, hp, mips, risc_ultrix, sco, sun3, sun4, or vax_ultrix.
#		XPstartup is not supported on vms.
#	    TEKXP_BIN
#	        Points to a site-defined directory for TEKXP programs,
#	        so that one file server can support sessions on multiple
#		types of hosts.  If not defined, $TEKXP_ROOT/bin is used.
#
#	FILES
#	    $TEKXP_ROOT/config/*/Xresources
#	    USER_XSESSION=$HOME/.xsession
#	    OLD_USER_XSESSION=$HOME/.Xsession
#	    USER_XDEFAULTS=$HOME/.Xdefaults
#	    USER_XRESOURCES=$HOME/.Xresources
#	    SITE_XSESSION=$CONFIG_DIR/zzsite/Xsession
#	    SYS_XSESSION=$CONFIG_DIR/zzTek/Xsession
#	    (in XPkeymap:)
#	    $TEKXP_ROOT/config/$kbd_type/*/Xkm*
#	    USER_XKM=$HOME/.Xkm
#
#	OVERVIEW OF THE X SESSION
#	    If you decide to use this script, then the provided "system
#	    default" Xresources file ($TEKXP_ROOT/config/zzTek/Xresources),
#	    and possibly the "system default" Xsession file
#	    ($TEKXP_ROOT/config/zzTek/Xsession) will be used upon startup
#	    of an X Session.  The scheme for X resources is that first the
#	    "system", then "site" (if any), then "user" X resources will be
#	    loaded via 'xrdb'.  This allows a "site" resource to override a
#	    corresponding "system" resource that had been previously loaded,
#	    and similarly "user" resource overriding "site" or "system"
#	    resource.  The scheme for Xsession files is to execute the first
#	    file found where search order is as follows:
#		$HOME/.xsession
#		$HOME/.Xsession
#		$TEKXP_ROOT/config/zzsite/Xsession
#		$TEKXP_ROOT/config/zzTek/Xsession
#
#	SITE CUSTOMIZATION OF X SESSION
#	    "Site" X resource and X session files override "system" files:
#		$TEKXP_ROOT/config/zzTek/Xresources
#		$TEKXP_ROOT/config/zzTek/Xsession
#	    The system administrator can provide customized X resources and
#	    X session files for use by all users.  To do this simply create
#	    a customized Xresources or sys.Xdefaults, and/or Xsession file in
#	    the directory
#		$TEKXP_ROOT/config/zzsite.
#
#	USER CUSTOMIZATION OF X SESSION
#	    From a user's standpoint, if you want to customize your
#	    X session you must create your own .xsession, .Xdefaults, and/or
#	    .Xkm files in your $HOME directory.  The XPstartup shell script
#	    will load "system" and "site" X resources, so if you want to add
#	    X resources or change previously defined X resources, you will
#	    need to do this in your $HOME/.Xdefaults file.  If a
#	    $HOME/.xsession or $HOME/.Xsession file is not present, then
#	    either a "site" or "system" X session file will be executed
#	    (see following Customizing X Session section).
#
#	CUSTOMIZING X RESOURCES
#		The XPstartup script will attempt load system, site, and user
#		X resources defined in:
#
#			 $CONFIG_DIR/*/Xresources
#			 $HOME/.Xdefaults
#			 else
#			 $HOME/.Xresources	(for backward compatibility)
#
#		where CONFIG_DIR=$TEKXP_ROOT/config.  Any resource in
#		previous files can be overridden in a subsequent file.
#
#		System X Resources:
#		    The following file sets up a number of default resources,
#		    similar to those on Tek workstations:
#			$CONFIG_DIR/zzTek/Xresources
#		Site X Resources:
#		    To add site X resource definitions, the system
#		    administrators simply creates the Xresource file:
#			$CONFIG_DIR/zzsite/Xresources
#		    or any similar name which is alphabetically after "zzTek".
#		    (See /usr/lib/X11/XP/xdm/sample.Xdefaults for an example).
#		User X Resources:
#		    To add user X resources, the user simply creates an
#		    X resource file:
#			$HOME/.Xdefaults
#			or
#			$HOME/.Xresources
#		    (See /usr/lib/X11/XP/xdm/sample.Xdefaults for an example).
#
#		Because of the directory/filenames the zzsite/Xresources will
#		be loaded after the zzTek/Xresources, therefore overwriting
#		resources that may have been previously defined by
#		zzTek/Xresources.  The $HOME/.Xdefaults, if found, are always
#		loaded, thereby overwriting resources that may have been
#		previously defined by other Xresource files.
#
#	CUSTOMIZING THE KEYBOARD
#		XPstartup calls the XPkeymap script to load system, site, and
#		user keymap files.  In particular, compose sequences and
#		diacriticals for European VT200 keyboards on non-DEC systems
#		are supported by XPkeymap.  After the Tek-supplied compose
#		sequences and any site-installed keymappings are loaded,
#		XPkeymap loads the user $HOME/.Xkm file.  See XPkeymap for
#		details.
#
#	CUSTOMIZING X SESSION
#		This XPstartup script will attempt to find a X session file in
#		the following order, executing the first one found:
#			1. $HOME/.xsession
#			1. $HOME/.Xsession
#			2. $SITE_XSESSION
#			where SITE_XSESSION=$CONFIG_DIR/zzsite/Xsession
#			3. $SYS_XSESSION
#			where SYS_XSESSION=$CONFIG_DIR/zzTek/Xsession
#
#		System X Session:
#		    Default X Session script that is executed if others not
#		    found.
#		Site X Session:
#		    If found, replaces the System X Session script.
#		    The system administrator may choose to replace the System X
#		    Session file with a customized Site X Session file for users
#		    who have not created their own customized X Session file.
#		    (See /usr/lib/X11/XP/xdm/sample.Xsession for an example).
#		User X Session:
#		    If found, replaces the Site and System X Session script.
#		    Users can customize their sessions by simply creating
#		    a .Xsession or .xsession file in their $HOME directory.
#		    (See /usr/lib/X11/XP/xdm/sample.Xsession for an example).
#
#
#	BUGS/CAVEATS
#		When running under XDM, the user's X session file must return
#		only after the user terminates the session.  The file must
#		exec some client that the user terminates in order to logout.
#		This differs from XDM user session files on Tek workstations,
#		which were allowed to background clients and return.
#
#
#	RCS VERSION
#		$Header: /tmp_mnt/vol/nwd/tools/media/X11/XP/bin/RCS/XPstartup,v 1.12 1991/05/29 16:09:18 glennw Exp $
#

if [ ! "$TEKXP_ROOT" ]
then
    if [ "$XP_ROOT" ]
    then
    	# XP_ROOT is the old variable set to /usr/lib/X11.
    	TEKXP_ROOT=$XP_ROOT/XP
    else
	TEKXP_ROOT=/usr/lib/X11/XP
    fi
    export TEKXP_ROOT
fi

if [ ! "$TEKXP_BIN" ]
then
    TEKXP_BIN=$TEKXP_ROOT/bin
    export TEKXP_BIN
fi
#
# Add $TEKXP_ROOT/bin to user's path if it or some specific bin/<host>
# directory is not there already.
#
. $TEKXP_BIN/add_xp_bin

# The system administrator or user may add $TEKXP_ROOT/bin to the their path
# so that they can access xmodmap and xsetprop.  If they do not add this
# directory, XPstartup will add it to the beginning of their path.  However, 
# the scripts do not assume that the user's path includes the XP bin directory;
# XPkeymap looks for the environment variable TEKXP_BIN which points to a
# site-defined directory for TEKXP programs, else uses the common bin
# directory configured during V4 tape install.  The TEKXP_BIN variable
# allows using one XP tree with multiple host binaries on a file server in a
# heterogeneous network.  If the xsetprop utility cannot be found in the 
# Tekxp bin directory, we fall back to the user's path, assume that the
# xsetprop and Tek xmodmap utilities are not available, and skip the system and
# keyboard-dependent site keymap file loading.
#

CONFIG_DIR=$TEKXP_ROOT/config
USER_XSESSION=$HOME/.xsession
OLD_USER_XSESSION=$HOME/.Xsession
USER_XRESOURCES=$HOME/.Xresources
# this file obsolete, standard X defaults file is now loaded if present:
USER_XDEFAULTS=$HOME/.Xdefaults
SITE_XSESSION=$CONFIG_DIR/zzsite/Xsession
SYS_XSESSION=$CONFIG_DIR/zzTek/Xsession

#
# Process args.  Save them for XPkeymap.
#
args="$*"
while [ $# != 0 ]
do
    case "$1"
    in
        -debug)
            set -x
            shift
	    ;;
        -xdm)
            XDM_INITIATED='TRUE'
            shift
	    ;;
        -display)
	    #Reset the DISPLAY environment variable
	    DISPLAY=$2
	    export DISPLAY
	    shift;shift
	    ;;
        -*)
            echo >&2 "$0: invalid option: $1"
            echo >&2 "usage: $0 [-display DISPLAY_NAME] [-xdm] [-debug]"
            exit 1
            ;;
    esac
done

if [ "$DISPLAY" = "" ]
then
    echo "XPstartup:  Error -- missing DISPLAY argument or environment variable"
    exit 1
fi

#
# Create any connection to the display and keep it open.
# Remember that if the last connection to the X Server is
# broken, the X Server resets itself and you lose all your
# resource settings, unless you have set "Retain X Settings" to
# Yes, keep the console open, or use xdm or another login manager that
# maintains an open connection throughout the session.
#
if [ "$XDM_INITIATED" = "" ]
then
    # Ultrix does not support test -x.
    if [ -r /bin/xlogo ]
    then
	/bin/xlogo -iconic -geometry +30+1 -xrm "*iconGeometry:+30+1" &
    else
    	if [ -r /bin/xclock ]
	then
	    /bin/xclock -geometry 40x40+30+1 -xrm "*iconGeometry:+30+1" &
	else
	    # print warning message if xterm can't be found
	    $TEKXP_BIN/xterm.sh -warning -geometry 80x24+50+35 -xrm "*iconGeometry:+30+1" &
	fi
    fi
fi
#
# Load system, site, and user keymap files.  In particular, compose
# sequences for European VT200 keyboards.
#
$TEKXP_ROOT/bin/XPkeymap $args

# Load system and site resources.
# For backward compatibility, load files named Xresources first.
#    Assumes there may be subdirectories (e.g., zzTek, zzsite) of
#    $CONFIG_DIR each of which may contain the resource file
#    "Xresources" or "sys.Xdefaults".
#
    cat -s $CONFIG_DIR/*/Xresources \
		 $CONFIG_DIR/*/sys.Xdefaults >/tmp/$$ 2>/dev/null
# XXX this program should be put in the XP bin directories.
    xrdb -load /tmp/$$
    /bin/rm -f /tmp/$$

#
# Merge user's defaults file.
# For backward compatibility, if $HOME/.Xdefaults does not exist,
# use $HOME/.Xresources.
#
    if [ -f $USER_XDEFAULTS ]
    then
 	xrdb -merge $USER_XDEFAULTS
    elif [ -f $USER_XRESOURCES ]
    then
 	xrdb -merge $USER_XRESOURCES
    fi

#
# Find the proper X session script to execute:
#    Search Order:
#	1. User .xsession
#	2. User .Xsession
#	3. Site
#	4. System
# if the file exists but is not executable, user gets an error; note that
# Ultrix does not support test -x.
#
    if [ -r $USER_XSESSION ]
    then
	xsession=$USER_XSESSION
    else
    	if [ -r $OLD_USER_XSESSION ]
    	then
	    xsession=$OLD_USER_XSESSION
    	else
	    if [ -r $SITE_XSESSION ]
	    then
	        xsession=$SITE_XSESSION
	    else
	    	if [ -r $SYS_XSESSION ]
	    	then
	    	    xsession=$SYS_XSESSION
	    	else
		    # this should never happen!
	    	    xsession="xterm -n Default_Session -geometry 80x24+50+50"
	    	fi
	    fi
    	fi
    fi
#
# Execute the proper xsession.
#
    exec $xsession
    exec /bin/sh $xsession
