#!/bin/athena/tcsh -f
# Global xsession file
# Accepts one optional argument, either "nocalls" (use no user files)
# or "dash" (use old dash-based interface).
#
# $Id: xsession,v 1.23 2002-01-24 21:50:10 mwhitson 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 "Ignore your customizations" 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
	    setenv SHELL /bin/athena/tcsh
	else if ($1 == dash) then
	    echo -n "xsession: Running standard xsession "
	    echo "with dash-based interface..."
	    setenv ATHENA_DASH_INTERFACE
        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

# 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


# 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 && ! $?NOCALLS) then
	setenv ATHENA_DASH_INTERFACE
endif

if (! $?ATHENA_DASH_INTERFACE) then
	# Don't use the SGI desktop with the new interface by default,
	# since it doesn't work especially well.  The user can "unset
	# skip_sgi" in .environment to override.
	set skip_sgi
endif

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


# Identify this as a window session.

setenv XSESSION $$

# We set sleep_time to 0 for historical purposes, in case anyone has
# code that depends on its value.

set sleep_time=0

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

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

# Source the system cshrc.
if (-r /etc/cshrc) source /etc/cshrc

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

if ($?ATHENA_DASH_INTERFACE) then
	set default_windowmanager=mwm
	set terminal=xterm
	set terminal_geometry=80x48+0+250
else
	set default_windowmanager=sawfish
	set terminal=gnome-terminal
	set terminal_geometry=80x48+0+242
endif


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

if (! $?skip_sgi) then

  if (! $?host) then
    set host = `hostname`
  endif

  if (-r .desktop-$host/4Dwmdesks) then
    \grep -v _Zwgc .desktop-$host/4Dwmdesks >&! .desktop-$host/4Dwmdesks.fixed
    if ($status == 0) then
      \mv -f .desktop-$host/4Dwmdesks.fixed .desktop-$host/4Dwmdesks
    else
      \rm -f .desktop-$host/4Dwmdesks.fixed
    endif
  endif

  setenv WINDOW_MANAGER 4Dwm
  $initdir/Xsession.dt

  if ((! $?NOCALLS) && (-r .lang)) then
    setenv LANG `cat .lang`
  endif

else

  # Load user X resources.

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

endif


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

# 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 $default_windowmanager

if ( $?skip_sgi ) then

  if ($?verbose_login) echo "Starting $WINDOW_MANAGER window manager..."

  if ($?NOCALLS) then
	(sawfish --no-user-rc &)
  else
	($WINDOW_MANAGER &)
  endif

endif

# Run standard system/user startup activities

if ( ! $?ATHENA_DASH_INTERFACE) then
	# Run the GNOME capplets unless the user has chosen to skip them.
	if (! $?skip_capplets) then
		if ($?verbose_login) echo "Initializing settings..."
		sound-properties --init-session-settings
		background-properties-capplet --init-session-settings
		mouse-properties-capplet --init-session-settings
		keyboard-properties --init-session-settings
	endif
	# 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, skip panel now.
	# TODO: deal with NOCALLS; add logout confirmation
	if (! $?skip_panel && ! ($?skip_dash && $?skip_xlogout)) then
		if ($?verbose_login) echo "Starting panel..."
		(panel-wrapper >& /dev/null && kill -HUP $XSESSION &)
	endif
else if (! $?skip_dash) then
	if (! $?skip_xlogout) then
		if ($?verbose_login) echo "Starting the Athena Dashboard..."
		if ($?verbose_login) echo "Creating logout button..."
		(dash -default -logout -nosession &)
	else
		if ($?verbose_login) echo "Starting the Athena Dashboard..."
		(dash -nosession &)
	endif
else if (! $?skip_xlogout) then
	if ($?verbose_login) echo "Creating logout button..."
	(dash -logout -nosession &)
endif

# 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 $terminal window..."
	# We need to remove the WM_COMMAND property on the terminal window
	# so that 4Dwm does not session-manage it.
	if ($terminal == xterm) then
		(xterm -geometry $terminal_geometry -title xterm -n xterm -e /bin/sh -c 'xprop -id $WINDOWID -remove WM_COMMAND; exec $SHELL' &)
	else if ($terminal == gnome-terminal) then
		(gnome-terminal --geometry=$terminal_geometry --title=Terminal -x /bin/sh -c 'xprop -id $WINDOWID -remove WM_COMMAND; exec $SHELL' >& /dev/null &)
	else
		($terminal &)
	endif
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 (! $?ZEPHYR_CLIENT) setenv ZEPHYR_CLIENT zwgc
	if ($?NOCALLS) then
		zwgc -f /usr/athena/share/zephyr/zwgc.desc \
				-subfile /dev/null && \
		get_message -new -zephyr
	else
		$ZEPHYR_CLIENT && get_message -new -zephyr
	endif
	from -t -n			# Check for mail
	(olc who &) >& /dev/null	# Ping the OLC server
	if (! $?skip_xss) then
		(xss -no-splash &)
	endif
	# Note: authwatch uses the present value of $KRB5CCNAME, which
	# may be modified by ~/.environment.  However, if bash users
	# make a similar change in ~/.bash_environment, that change
	# will NOT be seen by this invocation of authwatch.
	if (! $?skip_authwatch) then
		(authwatch &)
	endif
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 we are doing an SGI login (skip_sgi is not set), we source
# ~/.startup.sgi instead of .startup.X. This is to help the user
# not shoot himself in the foot, due to the session management
# that may be going on in 4Dwm. See documentation somewhere for
# details.

# 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 .startup.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 ($?skip_sgi) then
  set athena_startup=.startup.X
else
  set athena_startup=.startup.sgi
endif

if ((! $?NOCALLS) && (-r ~/${athena_startup})) then
	if ($?verbose_login) then
	  echo "Running custom startup activities listed in ~/${athena_startup}..."
	endif
	( source ~/${athena_startup}; \
	  if (! $?skip_lert) lert -q -z ; \
	  if ($?verbose_login) echo "Session initialization completed." &)
else
	if (! $?skip_lert) lert -q -z
	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, 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 ($?NOCALLS) then
	exec session_gate
else
	exec session_gate -logout
endif

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