#!/bin/sh
# Global xsession file
# Accepts one optional argument "nocalls" (i.e., use no user files)
#
# Adapted from:
# $Id: xsession,v 1.1 1995/03/23 02:51:45 svalente Exp $
# by David Maze 11 Jul 1997

# Determine whether to use user customizations:  use no customization files
# ONLY if the single argument "nocalls" was provided to xsession (e.g., if
# the user selected the "Ignore your customizations" option from the
# initial xlogin menu).

# The initdir moved, since Athena isn't supporting this.  (I am.  :-)
initdir=/mit/dmaze/init

case $# in
    0)
	;;
    1)
	if [ "$1" = "nocalls" -o "$1" = "-nocalls" ]; then
            echo -n "xsession: Running standard xsession "
            echo "with NO user configuration files..."
	    NOCALLS=yes
            export NOCALLS
	    SHELL=/bin/sh; export SHELL
	elif [ $1 = "dash" ]; then
	    echo -n "xsession: Running standard xsession "
	    echo "with dash-based interface..."
	    ATHENA_DASH_INTERFACE=yes
	    export ATHENA_DASH_INTERFACE
	else
	    bad_args=yes
	fi
	;;
    *)
        bad_args=yes
	;;
esac

if [ -n "$bad_args" ]; then
    echo "xsession: Arguments ($*) not recognized."
    echo "Running standard xsession with user configuration files..."
    unset NOCALLS
    unset bad_args
fi

# Do not echo status messages if noninteractive shell.  For interactive shells,
# echo messages unless user has ~/.hushlogin file AND the session was not 
# started using the xlogin "SYSTEM" option.

if [ ! -r $HOME/.hushlogin -o -n "$NOCALLS" ]; then verbose_login=yes; fi

# Helper function: print a message iff verbose_login is nonempty.
vecho() { test -n "$verbose_login" && echo "$@" }

# Use old dash-based interface if the user has
# ~/.athena_dash_interface and the session was not started with
# NOCALLS.

if [ -r ~/.athena_dash_interface -a -z "$NOCALLS" ]; then
    ATHENA_DASH_INTERFACE=yes; export ATHENA_DASH_INTERFACE
fi

# *******************   ENVIRONMENT SETUP   *******************


# Identify this as a window session.

XSESSION=$$; export XSESSION

# The ~/.cshrc file sets environment variables, attaches lockers, and sets
# the command search path.

vecho "Setting up environment..."

if [ -n "$NOCALLS" ]; then
    if [ -r $initdir/bashrc ]; then . $initdir/bashrc; fi
else
    if [ -r $HOME/.bashrc ]; then
	. $HOME/.bashrc
    elif [ -r $initdir/bashrc ]; then
	. $initdir/bashrc
    fi
fi

if [ -n "$ATHENA_DASH_INTERFACE" ]; then
	default_windowmanager=mwm
	terminal=xterm
	terminal_geometry=80x48+0+250
else
	default_windowmanager=sawfish
	terminal=gnome-terminal
	terminal_geometry=80x48+0+242
fi


# *******************   DEVICE SETUP   *******************

vecho -n "Setting up X:"

# Load user X resources.
if [ -z "$NOCALLS" -a -r $HOME/.Xresources ]; then
    vecho -n " xrdb"
    xrdb -merge $HOME/.Xresources
fi

# *******************   ACTIVITY STARTUP   *******************

# Run standard system/user startup activities


# Run the Athena Dashboard by default.
# To skip this, put the command "set skip_dash" in
# your ~/.environment file.

# Also run the Athena Dashboard as the logout button.
# To skip this, put the command "set skip_xlogout" in
# your ~/.environment file.

# We run this first, before the initial xterm, to give dash a chance to
# start up and be ready to put up the logout button later (below).

if [ -z "$ATHENA_DASH_INTERFACE" ]; then
    # Run the GNOME capplets unless the user has chosen to skip them.
    if [ -z "$skip_capplets" ]; then
	vecho -n " sound"
	sound-properties --init-session-settings
	vecho -n " background"
	background-properties-capplet --init-session-settings
	vecho -n " mouse"
	mouse-properties-capplet --init-session-settings
	vecho -n " keyboard"
	keyboard-properties --init-session-settings
    fi
    # Run the GNOME panel unless the user has chosen to skip it.
    # For compatibility, if the user elected to skip both dash
    # and xlogout in the old dash-based world, start panel now.
    # TODO: deal with NOCALLS; add logout confirmation
    if [ -z "$skip_panel" ] || ! ( [ -n "$skip_dash" ] && [ -n "$skip_xlogout" ] )
    then
	vecho -n " panel"
	((panel-wrapper >/dev/null 2>&1 && kill -HUP $XSESSION) &)
    fi
elif test -z "$skip_dash" ; then
    vecho -n " dash"
    if test -z "$skip_xlogout" ; then
	if test -n "$NOCALLS" ; then
	    (dash -default -logout \
		-userdefs /usr/athena/lib/X11/app-defaults/Dash &)
	else
	    (dash -default -logout &)
	fi
    else
	(dash &)
    fi
else
    if [ -z "$skip_xlogout" ]; then
	vecho -n " dash"
	(dash -logout &)
    fi
fi
vecho "."

# Start window manager.  To change your default window manager, set the 
# WINDOW_MANAGER environment variable in your ~/.environment file.

if [ -z "$WINDOW_MANAGER" ]; then
	WINDOW_MANAGER=$default_windowmanager
	export WINDOW_MANAGER
fi

vecho -n "Starting window manager:"
if [ -n "$NOCALLS" ]; then
    vecho -n "$default_windowmanager"
    if [ "$default_windowmanager" = mwm ]; then
	(mwm -xrm "*configFile: /usr/athena/lib/X11/system.mwmrc" &)
    elif [ "$default_windowmanager" = sawfish ]; then
	(sawfish --no-user-rc &)
    else
	($default_windowmanager &)
    fi
else
    vecho -n " " `basename $WINDOW_MANAGER`
    (eval $WINDOW_MANAGER &)
fi
vecho "."

# Start default initial xterm window.  To skip this, put the command
# "set skip_initial_xterm" in your ~/.environment file.

# BE CAREFUL:  If you skip this, make sure that your ~/.shstartup.X file
# provides a way for you to exit your session (e.g., an xterm from which
# you can type "logout"). 

vecho -n "Starting X clients:"
if [ -z "$skip_initial_xterm" ]; then
    vecho -n " $terminal"
    if [ "$terminal" = gnome-terminal ]; then
	($terminal --geometry=$terminal_geometry &)
    else
	# Assume "normal" X options.
	($terminal -geometry $terminal_geometry &)
    fi
fi

# Run default system startup activities.  To skip these, put the command
# "set skip_x_startup" in your ~/.environment file.

if [ -z "$skip_x_startup" ]; then
    # Start Zephyr client, and if zephyr started okay, send
    # message of the day as windowgram
    vecho -n " zwgc"
    if [ -n "$NOCALLS" ]; then
	zwgc -f /usr/athena/lib/zephyr/zwgc.desc -subfile /dev/null && \
	    get_message -new -zephyr
    else
	zwgc && get_message -new -zephyr
    fi
    vecho -n " olc"
    (olc who &) > /dev/null	2>&1	# Ping the OLC server
    from -t -n			# Check for mail
    if [ -z "$skip_xss" ]; then
        vecho -n " xss"
	(xss -no-splash &)
    fi
fi
vecho "."

# Run the window-session customizing commands in your ~/.shstartup.X file.
# We want to background this process in case something in the startup
# hangs.  Thus, the user will not be left with active windows and no
# method of easily logging out.

# We also run the notification system, lert. Ideally, we wish the user
# to receive the alert in the form of a zephyrgram, and since they may
# run zwgc from their .shstartup.X, we wait until that has finished.
# To skip lert (a generally bad idea unless you plan to run it yourself -
# it is not at all intended for frivolous messages), put the commend
# "set skip_lert" in your ~/.environment file.

if [ -z "$NOCALLS" -a -r $HOME/.shstartup.X ]; then
    . $HOME/.shstartup.X;
    if [ -z "$skip_lert" ]; then lert -q -z; fi
    vecho "Session initialization completed."
else
    if [ -z "$skip_lert" ]; then lert -q -z; fi
    vecho "Session initialization completed."
fi


# Gate the session.

# This command replaces this script and will wait for you to log out.
# To terminate this process, logout (type the "logout" command in any
# xterm window, use the Logout button, or select the "Logout of Athena"
# option from Dash), or invoke the "end_session" program.

if [ -n "$NOCALLS" ]; then
    exec session_gate
else
    exec session_gate -logout
fi

# The session gate will source .logout when it gets a SIGHUP, if
# invoked with the -logout flag.
