#!/bin/sh
#
# /etc/gdm/Sessions/Gnome
#
# global GNOME session file -- used by gdm

# See /etc/X11/Xsession for inspiration

# Setup standard error and standard output to user's home directory if possible.

if [ -z "$DEBUG_GNOME_SESSION" ]; then
    for errfile in "$HOME/.gnome-errors" "${TMPDIR:-/tmp}/gnomeses-$USER" "/tmp/gnomeses-$USER"; do
      if ( cp /dev/null "$errfile" 2> /dev/null ); then
	chmod 600 "$errfile"
	exec > "$errfile" 2>&1
	break
      fi
    done
fi

### Shell environment

# Abstract getting login options

logindefsfile=/etc/login.defs

haveloginoption() {
  optionname=$1
  echo "$0: checking $logindefsfile for option $optionname" >&2
  grep -qs ^$optionname $logindefsfile
}

loginoptionvalue() {
  optionname=$1
  optionvalue=`awk "\\$1 == \"$optionname\" { print \\$2 }" $logindefsfile`
  echo "$0: $logindefsfile: \"$1\" is \"$optionvalue\"" >&2
  echo $optionvalue
}

if [ -z "$USER" ]; then
    USER=`whoami`
    export USER
fi

# Get defaults from /etc/login.defs
if haveloginoption UMASK; then
    umask `loginoptionvalue UMASK`
fi

# Commented out these lines since GDM does give a reasonable default path
#if haveloginoption ENV_PATH; then
#    eval `loginoptionvalue ENV_PATH`
#fi

if haveloginoption ULIMIT; then
    ulimit `loginoptionvalue ULIMIT`
fi

if haveloginoption MAIL_DIR; then
    MAIL=`loginoptionvalue MAIL_DIR`/$USER
    export MAIL
elif haveloginoption MAIL_FILE; then
    MAIL=$HOME/`loginoptionvalue MAIL_FILE`
    export MAIL
fi

### X environment (resources etc)

# Get options from gnome/session.options or Xsession.options if session.options does not exist

for optionfile in /etc/gnome/session.options /etc/X11/Xsession.options; do
  if [ -f $optionfile ]; then
    break
  fi
done
echo "$0: using $optionfile for options" >&2

# Abstract getting options

haveoption() {
  optionname=$1
  echo "$0: checking $optionfile for option $optionname" >&2
  grep -qs ^$optionname $optionfile
}

sysmodmap=/etc/X11/Xmodmap
usrmodmap=$HOME/.Xmodmap
sysresources=/etc/X11/Xresources
usrresources=$HOME/.Xresources

usrgnomerc=$HOME/.gnomerc

startssh=
sshagent=/usr/bin/ssh-agent

if [ -x $sshagent -a -z "$SSH_AUTH_SOCK" ]; then
  startssh=yes
fi

if [ -d $sysresources ]; then
  if [ "$(echo $sysresources/*)" != "$sysresources/*" ]; then
    for resourcefile in $(ls $sysresources/* 2> /dev/null | egrep '^[-/_[:alnum:]]*$'); do
      xrdb -merge $resourcefile
    done
  fi
fi

if [ -x /usr/bin/X11/xmodmap ]; then
  if [ -f $sysmodmap ]; then
    xmodmap $sysmodmap
  fi
fi

if haveoption allow-user-resources; then
  if [ -f $usrresources ]; then
    xrdb -merge $usrresources
  fi
fi

if [ -x /usr/bin/X11/xmodmap ]; then
  if haveoption allow-user-modmap; then
    if [ -f $usrmodmap ]; then
      xmodmap $usrmodmap
    fi
  fi
fi

gnomesession="/usr/bin/gnome-session --purge-delay=15000"

if [ -f $usrgnomerc ]; then
  . $usrgnomerc
fi

if [ -n "$DEBUG_GNOME_SESSION" ]; then
  exit 0
fi

if [ -n "$startssh" ]; then
  exec $sshagent -- $gnomesession
else
  exec $gnomesession
fi
