#!/bin/csh -f
# Global xsession file
# Accepts one optional argument "nocalls" (i.e., use no user files)
#
# $Id: xsession,v 1.23 91/06/20 07:31:11 probe Exp $

# Note: This shell script deliberately uses the -f flag.

set initdir=/usr/athena/lib/init

# 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 SYSTEM option from the initial xlogin menu).

switch ($#argv)
    case 0:
        breaksw
    case 1:
        if (($1 == nocalls) || ($1 == -nocalls)) then
            echo -n "xsession: Running standard xsession "
            echo "with NO user configuration files..."
            setenv NOCALLS
        else
            set bad_args
        endif
        breaksw
    default:
        set bad_args
endsw

if ($?bad_args) then
        echo "xsession: Arguments ($argv) not recognized."
        echo "Running standard xsession with user configuration files..."
        unsetenv NOCALLS
        unset bad_args
endif


# 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 ~/.hushlogin) || ($?NOCALLS)) set verbose_login


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


# Identify this as a window session.

setenv XSESSION $$
/usr/bin/X/xset +fp /usr/lib/X11/fonts/misc/,/usr/lib/X11/fonts/75dpi/,/usr/lib/X11/fonts/100dpi/
# Set the sleep time for waiting between launching large applications.
# These can be overridden in the ~/.cshrc or ~/.environment files.

switch (`machtype -c`)
    case VAXSTAR:
	set sleep_time=10
	breaksw
    case MVAX-II:
	set sleep_time=10
	breaksw
    case RTPC-ROMPC:
	set sleep_time=6
	breaksw
    case PS2-*:
	set sleep_time=6
	breaksw
    default:
	set sleep_time=0
endsw

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

if ($?verbose_login) echo "Setting up environment..."

if ($?NOCALLS) then
	if (-r ${initdir}/cshrc) source ${initdir}/cshrc
else
	if (-r ~/.cshrc) then
		source ~/.cshrc
	else 
		if (-r ${initdir}/cshrc) source ${initdir}/cshrc
	endif
endif



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


# Load user X resources.

if ((! $?NOCALLS) && (-r ~/.Xresources)) xrdb -merge ~/.Xresources


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

if (! $?WINDOW_MANAGER) setenv WINDOW_MANAGER twm
if ($?verbose_login) echo "Starting $WINDOW_MANAGER window manager..."
($WINDOW_MANAGER &)
sleep $sleep_time
# mwm takes twice as long to start
if ("$WINDOW_MANAGER" =~ mwm) sleep $sleep_time	


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

# Run standard system/user startup activities


# 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 ~/.startup.X file
# provides a way for you to exit your session (e.g., an xterm from which
# you can type "logout"). 

if (! $?skip_initial_xterm) then
	if ($?verbose_login) echo "Creating initial xterm window..."
	(xterm -geometry 80x48+0+0 &)
	sleep $sleep_time
endif


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

if (! $?skip_xlogout) then
	if ($?verbose_login) echo "Creating logout button..."
	(dash -logout &)
	sleep $sleep_time
	sleep $sleep_time
endif


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

if (! $?skip_x_startup) then
	if ($?verbose_login) echo "Running standard startup activities..."
	# Start Zephyr client, and if zephyr started okay, send
	# message of the day as windowgram
	if ($?NOCALLS) then
		( zwgc -f /usr/athena/lib/zephyr/zwgc.desc \
				-subfile /dev/null && \
		  get_message -new -zephyr &)
	else
		( zwgc && get_message -new -zephyr &)
	endif
	sleep $sleep_time
	msgchk -nonotify nomail		# Check for mail
	(olc who &) >& /dev/null	# Ping the OLC server
endif


# Run the window-session customizing commands in your ~/.startup.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.

if ((! $?NOCALLS) && (-r ~/.startup.X)) then
	if ($?verbose_login) then
	  echo "Running custom startup activities listed in ~/.startup.X..."
	endif
	( source ~/.startup.X; \
	  if ($?verbose_login) echo "Session initialization completed." &)
else
	echo "Session initialization completed."
endif


# Gate the session.

# This command replaces this script and will wait for you to log out.
# To terminate this process, type the "logout" command in an xterm window
# or invoke the "end_session" program.

exec session_gate -logout			# Wait for logout

# The session gate will source .logout when it gets a SIGHUP.
