#!/bin/sh
# Error file findeng borrowed from Xsession stuff

script=$0
if [ -L $script ]; then script=`file $script | awk '{print $NF}'`; fi
WM=`echo $script | tr '[:upper:]' '[:lower:]'`
WM=`basename $WM`

# redirect errors to a file in user's home directory if we can
for errfile in "$HOME/.$WM-errors" "${TMPDIR-/tmp}/$WM-$USER" "/tmp/$WM-$USER"
do
	if ( cp /dev/null "$errfile" 2> /dev/null )
	then
		chmod 600 "$errfile"
		exec > "$errfile" 2>&1
		break
	fi
done

# Store the language
gdmlang=$LANG

set -a
if [ -f /etc/environment ]; then
  . /etc/environment
fi
set +a

for i in /etc/profile.d/*.sh ; do
        if [ -x $i ]; then
                . $i
        fi
done

# Restore GDM's LANG
if [ "x$gdmlang" != "x" -a "$gdmlang" != "(null)" ]; then
        export LANG=$gdmlang
fi

# This is a hack, needed for our current TurboSetup
if [ "x$LANG" != "x" -a "$LANG" != "(null)" ]; then
        export LC_ALL=$LANG
fi


sysmodmap=/etc/X11/Xmodmap
usrmodmap=$HOME/.Xmodmap
startup=$HOME/.${WM}rc
resources=$HOME/.Xresources

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

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

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

if [ -x "$startup" ]; then
	$startup
fi

if [ -f "$resources" ]; then
	xrdb -load "$resources"
fi

if [ ! -n "`which $WM`" ]; then
   echo "$WM: $WM not found. Quiting." >&2
   exit 1
fi

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

