#!/bin/ksh
#
# This is SORT OF LIKE an X session, but not quite.  You get a command as the
# first argument (it could be multiple words, so run it with "eval").  As a
# special case, the command can be:
#  default - Run the appropriate Xclients startup (see the code below)
#  custom - Run ~/.xsession and if that's not available run 'default'
#
# (Note that other arguments could also follow, but only the command one is
# right now relevant and supported)
#
# The output is ALREADY redirected to .xsession-errors in LightDM.  This way
# .xsession-errors actually gets more output.
# This also prevents DoS attacks if some app in the users session
# can be prodded to dump lots of stuff on the stdout/stderr.  We wish to be
# robust don't we?  
#
# Also note that this is not run as a login shell, this is just executed.
# This is why we source the profile files below.
#
# based on:
# GDM Xsession
# $XConsortium: Xsession /main/10 1995/12/18 18:21:28 gildea $

command="$@"

# this will go into the .xsession-errors along with all other echo's
# good for debugging where things went wrong
echo "$0: Beginning session setup..."

# First read /etc/profile and .profile
test -f /etc/profile && . /etc/profile
test -f "$HOME/.profile" && . "$HOME/.profile"
# Second read /etc/xprofile and .xprofile for X specific setup
test -f /etc/xprofile && . /etc/xprofile
test -f "$HOME/.xprofile" && . "$HOME/.xprofile"

OLD_IFS=$IFS

lightdmwhich () {
  COMMAND="$1"
  OUTPUT=
  IFS=:
  for dir in $PATH
  do
    if test -x "$dir/$COMMAND" ; then
      if test "x$OUTPUT" = "x" ; then
        OUTPUT="$dir/$COMMAND"
      fi
    fi
  done
  IFS=$OLD_IFS 
  echo "$OUTPUT"
}

# Reload audio settings after sourcing the user's .profile to ensure that any
# AUDIODEV settings defined by the user are honored.
#
AUDIOCTL_SAVE_HOSTNAME=`/usr/bin/hostname`
AUDIOCTL_SAVE_DEVICES=""
AUDIOCTL_SAVE_DIR="$HOME/.audioctl"
AUDIOCTL_DEVICE_OWNER=`/usr/bin/stat -L /dev/audio -c %U`

# Only set audio settings if logindevperm has set the owner of the audio
# device to this user.
#
if test "x$USER" = "x$AUDIOCTL_DEVICE_OWNER" ; then
  if test -x "/usr/bin/audioctl" ; then
    AUDIOCTL_SAVE_DEVICES="`/usr/bin/audioctl show-device | /usr/bin/awk '/^ *Name /{ print $3; }'`"
  fi
  if test -n "$AUDIOCTL_SAVE_HOSTNAME" -a -n "$AUDIOCTL_SAVE_DEVICES"; then
    for device in $AUDIOCTL_SAVE_DEVICES; do
      AUDIOCTL_SAVE_FILE="$AUDIOCTL_SAVE_DIR/audioctl-$AUDIOCTL_SAVE_HOSTNAME-$device"
      if test -f "$AUDIOCTL_SAVE_FILE" ; then
        /usr/bin/audioctl load-controls "$AUDIOCTL_SAVE_FILE"
      fi
     done
  fi
fi

zenity=`lightdmwhich zenity`

# Note: ~/.xsession-errors is now done in the daemon so that it
# works for ALL sessions (except ones named 'Failsafe')

# clean up after xbanner
freetemp=`lightdmwhich freetemp`
if [ -n "$freetemp" ] ; then
	"$freetemp"
fi

userresources="$HOME/.Xresources"
usermodmap="$HOME/.Xmodmap"
userxkbmap="$HOME/.Xkbmap"

sysresources=/etc/X11/Xresources 
sysmodmap=/etc/X11/Xmodmap 
sysxkbmap=/etc/X11/Xkbmap

if [ -f "$sysresources" ]; then
    xrdb -nocpp -merge "$sysresources"
fi

if [ -f "$userresources" ]; then
    xrdb -nocpp -merge "$userresources"
fi

# merge in keymaps
if [ -f "$sysxkbmap" ]; then
    setxkbmap `cat "$sysxkbmap"`
    XKB_IN_USE=yes
fi

if [ -f "$userxkbmap" ]; then
    setxkbmap `cat "$userxkbmap"`
    XKB_IN_USE=yes
fi

#
# Eeek, this seems like too much magic here
#
if [ -z "$XKB_IN_USE" -a ! -L /etc/X11/X ]; then
    if grep '^exec.*/Xsun' /etc/X11/X > /dev/null 2>&1 && [ -f /etc/X11/XF86Config ]; then
       xkbsymbols=`sed -n -e 's/^[     ]*XkbSymbols[   ]*"\(.*\)".*$/\1/p' /etc/X11/XF86Config`
       if [ -n "$xkbsymbols" ]; then
           setxkbmap -symbols "$xkbsymbols"
           XKB_IN_USE=yes
       fi
    fi
fi

# xkb and xmodmap don't play nice together
if [ -z "$XKB_IN_USE" ]; then
    if [ -f "$sysmodmap" ]; then
       xmodmap "$sysmodmap"
    fi

    if [ -f "$usermodmap" ]; then
       xmodmap "$usermodmap"
    fi
fi

unset XKB_IN_USE

# run all system xinitrc shell scripts.
if [ -d /etc/X11/xinit/xinitrc.d ]; then
    for i in /etc/X11/xinit/xinitrc.d/* ; do
        if [ -x "$i" -a ! -d "$i" ]; then
	    . "$i"
        fi
    done
fi

# add ssh-agent if found
sshagent="`lightdmwhich ssh-agent`"
if [ -n "$sshagent" ] && [ -x "$sshagent" ] && [ -z "$SSH_AUTH_SOCK" ]; then
    command="$sshagent -- $command"
elif [ -z "$sshagent" ] ; then
    echo "$0: ssh-agent not found!"
fi

echo "$0: Setup done, will execute: $command"

eval exec $command

echo "$0: Executing $command failed, will run xterm"

if [ -n "$zenity" ] ; then
	disptext="Failed to start the session, so starting a failsafe xterm session.  Windows will have focus only if the mouse pointer is above them.  To get out of this mode type 'exit' in the window."
	"$zenity" --info --text "$disptext"
fi

exec xterm -geometry 80x24+0+0