#
# Standard actions for any xsession
#

[ -f /etc/profile ] && . /etc/profile
[ -f $HOME/.profile ] && . $HOME/.profile

# Redirect errors to a file in user's home directory if possible
# GDM redirect output itself in a smarter fashion
if [ -z "$GDMSESSION" ]; then
	errfn="xerr-$USER-$DISPLAY"
	for errfile in "$HOME/.xsession-errors" "${TMPDIR:-/tmp}/$errfn" "/tmp/$errfn"; do
		if cp /dev/null "$errfile" 2> /dev/null ; then
			chmod 600 "$errfile"
			exec > "$errfile" 2>&1
			break
		fi
	done
fi

# System wide settings
sysxkbmap=/etc/X11/xinit/Xkbmap
sysmodmap=/etc/X11/xinit/Xmodmap
sysresources=/etc/X11/xinit/Xresources

# Also usable locations
another_sysxkbmap=/etc/X11/Xkbmap
another_sysmodmap=/etc/X11/Xmodmap
another_sysresources=/etc/X11/Xresources

# User settings
userxkbmap=$HOME/.Xkbmap
usermodmap=$HOME/.Xmodmap
userresources=$HOME/.Xresources
userdefaults=$HOME/.Xdefaults

# Merge in defaults
if [ -f $sysresources ]; then
	xrdb -merge $sysresources
elif [ -f $another_sysresources ]; then
	xrdb -merge $another_sysresources
fi

if [ -f $userresources ]; then
	xrdb -merge $userresources
elif [ -f $userdefaults ]; then
	xrdb -merge $userdefaults
fi

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

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

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

unset XKB_IN_USE

# Run all system xinitrc shell scripts.
for i in /etc/X11/xinit/xinitrc.d/*.sh ; do
	[ -x "$i" ] && . "$i"
done

# This must be last line !
# vi:syntax=on:ft=sh
