#!/bin/bash -login
#
# Copyright (C) 2002-2006  Dmitry V. Levin <ldv@altlinux.org>
#
# Traditional X session start script.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

PROG="${0##*/}"

if [ -z "$DISPLAY" ]; then
	echo "$PROG: \$DISPLAY undefined." >&2
	exit 1
fi

# Emulate which(1) internally.
absolute()
{
	local WHICH

	[ -n "$1" ] || return 1
	WHICH="$(type -p "$1")" || return 1
	[ "$WHICH" != "${WHICH##/}" ] || return 1
	[ -x "$WHICH" ] || return 1

	printf %s "$WHICH"
}

SourceIfNotEmpty()
{
	local f="$1"
	shift
	[ -s "$f" ] && . "$f" "$@"
}

RunIfExecutable()
{
	local f="$1"
	shift
	f="$(absolute "$f")" && [ -x "$f" ] && "$f" "$@"
}

ExecIfExecutable()
{
	local f="$1"
	shift
	f="$(absolute "$f")" && [ -x "$f" ] && exec "$f" "$@"
}

f="$HOME/.xsession-errors"
[ -z "${DISPLAY##:*}" ] && f="$f$DISPLAY" || f="$f-$DISPLAY"

# Redirect errors to a file.
for errfile in "$f" "$HOME/.xsession-errors"; do
	if install -m600 /dev/null "$errfile" 2>/dev/null; then
		exec &>"$errfile"
		break
	fi
done

unset f

echo "Running $PROG[$$] $*"

if [ $# -ge 1 ]; then
	# Clean up after xbanner.
	RunIfExecutable freetemp
else
	# ALT default background and cursor.
	xsetroot -solid "#666699"
	xsetroot -cursor_name left_ptr
fi

if [ -n "$LANGUAGE" ]; then
	try_lang="$LANGUAGE"
elif [ -n "$LANG" ]; then
	try_lang="$LANG"
else
	try_lang=
fi

Xrdb()
{
	if [ -s "$1" ]; then
		xrdb -merge "$1"
		return 0
	else
		return 1
	fi
}

MergeResources()
{
	local f
	for f in "$@"; do
		Xrdb "$f"
		local found=
		local n
		for n in `printf %s "$try_lang" |tr : ' '`; do
			n1="${n%.*}"
			n2="${n1%_*}"
			Xrdb "$f.$n2" && found=1
			Xrdb "$f.$n1" && found=1
			Xrdb "$f.$n" && found=1
			[ -z "$found" ] || break
		done
	done
}

# Merge in defaults and keymaps.
if [ -d /etc/X11/Xresources ]; then
    Xresources_d=$(ls /etc/X11/Xresources)
    MergeResources $Xresources_d "$HOME/.Xresources" "$HOME/.Xdefaults"
else
    MergeResources /etc/X11/Xresources "$HOME/.Xresources" "$HOME/.Xdefaults"
fi

TryXBrowser()
{
	local n
	for n in "$@"; do
		if n=`absolute "$n"`; then
			export BROWSER="$n"
			break
		fi
	done
	return 0
}

TryTextBrowser()
{
	local n
	for n in "$@"; do
		if n=`absolute "$n"`; then
			export BROWSER="xvt -e $n"
			break
		fi
	done
	return 0
}

# We need to set default browser.
# Window manager may redefine this setting.
if [ -z "$BROWSER" ] || [ ! -x "$BROWSER" ]; then
	TryXBrowser xbrowser mozilla firefox konqueror
fi

if [ -z "$BROWSER" ] || [ ! -x "$BROWSER" ]; then
	TryTextBrowser browser links lynx
fi

if [ -z "$BROWSER" ] || [ ! -x "$BROWSER" ]; then
	export BROWSER=
fi

export HELP_BROWSER="$BROWSER"

# Source shell scripts from user ~/.xprofile
SourceIfNotEmpty "$HOME"/.xprofile "$@"

# Run system scripts from /etc/X11/xinit.d/
for f in /etc/X11/xinit.d/*; do
	# Don't run *.rpm* and *~ scripts
	[ "${f%.rpm*}" = "$f" -a "${f%\~}" = "$f" ] || continue

	if [ -x "$f" ]; then
		"$f" &
	fi
done

# Run user scripts from ~/.xsession.d/
for f in "$HOME"/.xsession.d/*; do
	# Don't run *.rpm* and *~ scripts
	[ "${f%.rpm*}" = "$f" -a "${f%\~}" = "$f" ] || continue

	if [ -x "$f" ]; then
		"$f" &
	fi
done

unset f

RunIfExecutable /etc/nxserver/fixkeyboard

RunIfExecutable /etc/X11/xinit/xrandrrc
RunIfExecutable /etc/X11/xinit/fixkeyboard
RunIfExecutable /etc/X11/xinit/XIM

CMD=""
#ssh-agent doesnt start
[ -z "$SSH_AGENT_PID" ] && CMD="$CMD /usr/bin/ssh-agent"
[ -z "$DBUS_SESSION_BUS_ADDRESS" ] && CMD="$CMD /usr/bin/dbus-launch --exit-with-session"

if [ $# -ge 1 ]; then
	$CMD "$@"
else
	# Try user defined action.
	RunIfExecutable "$HOME/.xsession"
	RunIfExecutable "$HOME/.Xclients"

	# Try WM Selector.
	RunIfExecutable wm-select

	# Try WM Manager.
	RunIfExecutable wmselect

	# Try default from runwm.
	runwm default
fi
