#!/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.24 2005/12/22 14:54:17 rbasch Exp $

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

set initdir=/usr/athena/lib/init
set gconftool=/usr/athena/bin/gconftool-2

# 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

# *******************   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 startup scripts if necessary.
if (-r /etc/.login) source /etc/.login
if (-r /etc/.cshrc) source /etc/.cshrc
if (-r /etc/csh.login) source /etc/csh.login
if (-r /etc/csh.cshrc) source /etc/csh.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 -geometry 80x48+0+38'
else
  set default_windowmanager=metacity
  set terminal='gnome-terminal --geometry=80x40-0-52'

  # GNOME processes can apparently collide trying to start gconfd, so
  # spawn one before we start any in the background.
  $gconftool --spawn
endif


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

# Load user X resources.

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


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

if ( ! $?NOCALLS && -d ~/.gnome && ! -d ~/.gnome2 ) set offer_gnome_upgrade

# Start window manager.  To change your default window manager, set the 
# WINDOW_MANAGER environment variable in your ~/.environment file.
if ($?WINDOW_MANAGER) then
  setenv ATHENA_ENV_WINDOW_MANAGER
else if ( -r ~/.athena-sawfish ) then
  setenv WINDOW_MANAGER sawfish
else
  setenv WINDOW_MANAGER $default_windowmanager
endif

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

if ($?NOCALLS) then
  (metacity >& /dev/null &)
else
  ($WINDOW_MANAGER >& /dev/null &)
endif

# Run standard system/user startup activities

if ( -r ~/.athena_gnome_convert_failure ) then
  # A previous attempt at converting GNOME preferences failed.  Remove
  # any GNOME 2 preferences so that the user gets the defaults.
  $gconftool --recursive-unset /apps
  $gconftool --recursive-unset /desktop
  $gconftool --recursive-unset /system
  rm -rf ~/.gnome2
  rm -f ~/.athena_gnome_convert_failure
else if ($?offer_gnome_upgrade) then
  set dtext = ( \
    "\n" \
    " This Athena release uses GNOME 2, which changes the way the  \n" \
    " desktop is configured.  You can attempt to convert your GNOME 1  \n" \
    " preferences, or you can just accept the defaults for GNOME 2.  \n" \
    " If you do attempt to convert, you will be given a chance to go  \n" \
    " back to the defaults if it doesn't work well.\n" \
    "\n" \
    " Do you wish to attempt to convert your preferences?\n" )
  if ( { gdialog --title "GNOME upgrade check" --yesno "$dtext" 9 70 } ) then
    touch ~/.athena-gnome-upgrade-log
    echo "Attempting GNOME upgrade on `date`" >> ~/.athena-gnome-upgrade-log
    bash $initdir/gnome-stuff-1-to-2 >>& ~/.athena-gnome-upgrade-log
    perl $initdir/gnome-panel-1-to-2 >>& ~/.athena-gnome-upgrade-log
    set offer_gnome_failure
  endif
  unset dtext
endif

if ( ! $?ATHENA_DASH_INTERFACE) then
  # Run gnome-settings-daemon unless the user has chosen to skip it.
  # (We used to run a series of capplets with an init flag; thus the
  # skip variable is named skip_capplets.)
  if (! $?skip_capplets) then
    if ($?verbose_login) echo "Initializing settings..."

    # This incantation causes gnome-settings-daemon to be run via
    # Bonobo, such that we won't continue until the settings-daemon
    # object has been initialized and all settings have been
    # propagated.  If we just naively run gnome-settings-daemon in the
    # background, settings may not propagate before GNOME programs
    # run.  In particular, GNOME programs might render text before the
    # Xft settings have been propagated, leading to incorrect font
    # selection.
    activation-client -s "iid == 'OAFIID:GNOME_SettingsDaemon'" > /dev/null
  endif

  if ((! $?ATHENA_ENV_WINDOW_MANAGER) && (! -r ~/.athena-sawfish) \
      && (-r ~/.sawfish) && (! -r ~/.athena-wm-upgrade-dialog)) then
    touch ~/.athena-wm-upgrade-dialog
    zenity --info --text='This Athena release has changed the default window manager from Sawfish to Metacity.  Metacity is faster, more stable, and better-integrated with GNOME than Sawfish, but is not as customizable.\n\nIf you wish to continue using Sawfish, you may do so by selecting Settings -> Revert to Sawfish Window Manager from the foot menu.'
  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..."
    (gnome-panel >& /dev/null && kill -HUP $XSESSION &)
  endif

  # If you decide to set skip_nautilus, GNOME will stop taking care
  # of your background image unless you run:
  #   gconftool-2 -s -t bool /apps/nautilus/preferences/show_desktop false
  if (! $?skip_nautilus ) then
    (nautilus -n >& /dev/null &)
  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 &)
  else
    if ($?verbose_login) echo "Starting the Athena Dashboard..."
    (dash &)
  endif
else if (! $?skip_xlogout) then
  if ($?verbose_login) echo "Creating logout button..."
  (dash -logout &)
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 xterm window..."
  ($terminal >& /dev/null &)
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
  mailquota -n			# Check quota on post office server
  from -t -n			# Check for mail
  (olc who &) >& /dev/null	# Ping the OLC server
  if (! $?skip_xss) then
    (xscreensaver -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

# Maybe start the Disco-Athena daemon.
if (-r /var/run/athstatusd.sock && ! $?skip_athneteventd) athneteventd

# 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.

# 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 command
# "set skip_lert" in your ~/.environment file.

if ((! $?NOCALLS) && (-r ~/.startup.X)) then
  if ($?verbose_login) then
    echo "Running custom startup activities listed in ~/.startup.X..."
  endif
  ( source ~/.startup.X; \
    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

if ($?offer_gnome_failure) then
  set dtext = ( \
    "\n" \
    " If the conversion went well, your desktop should look about the  \n" \
    " same as it used to.  If not, you might be missing panel buttons  \n" \
    " or other elements of the desktop.  \n" \
    "\n" \
    " Do you wish to keep the results of the conversion?\n" )
  if ( ! { gdialog --title "GNOME failure check" --yesno "$dtext" 7 70 } ) then
    set dtext = ( \
      "\n" \
      " You will now be logged out.  On your next login, you will  \n" \
      " receive the GNOME 2 defaults.  \n" )
    gdialog --infobox "$dtext" 4 70
    touch ~/.athena_gnome_convert_failure
    exit
  endif
  unset dtext
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.
