#!/bin/bash -login
#
# $Id: Xsession,v 1.2 2002/11/15 09:36:55 ldv Exp $
# Copyright (C) 2002  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

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

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

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

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

ExecIfExecutable()
{
	local f="$1"
	shift
	[ -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 /usr/X11R6/bin/freetemp
else
	# ALT default background and cursor.
	/usr/X11R6/bin/xsetroot -solid "#666699"
	/usr/X11R6/bin/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
		/usr/X11R6/bin/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 `echo "$try_lang" |/usr/bin/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.
MergeResources /etc/X11/Xresources "$HOME/.Xresources" "$HOME/.Xdefaults"

/etc/X11/xinit/xrootwarn || exit

TryXBrowser()
{
	local n
	for n in "$@"; do
		local b=`/usr/bin/which "$n" 2>/dev/null`
		if [ -n "$b" ] && [ -x "$b" ]; then
			export BROWSER="$b"
			break
		fi
	done
}

TryTextBrowser()
{
	local n
	for n in "$@"; do
		local b=`/usr/bin/which "$n" 2>/dev/null`
		if [ -n "$b" ] && [ -x "$b" ]; then
			export BROWSER="/usr/X11R6/bin/xvt -e $b"
			break
		fi
	done
}

# We need to set default browser.
# Window manager may redefine this setting.
if [ -z "$BROWSER" ] || [ ! -x "$BROWSER" ]; then
	TryXBrowser xbrowser mozilla netscape galeon skipstone
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 system /etc/X11/profile.d/
for f in /etc/X11/profile.d/*.sh; do
	# Don't run *.rpm* and *~ scripts
	[ "${f%.rpm*}" == "$f" -a "${f%\~}" == "$f" ] || continue

	SourceIfNotEmpty "$f" "$@"
done

# 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/X11/xinit/fixkeyboard
RunIfExecutable /etc/X11/xinit/XIM

# See if desktop manager has asked for a specific environment.
if [ $# -ge 1 ]; then
	exec /usr/X11R6/bin/runwm "$@"
else
	# Try user defined action.
	ExecIfExecutable "$HOME/.xsession"
	ExecIfExecutable "$HOME/.Xclients"

	# Try WM Selector.
	ExecIfExecutable /usr/X11R6/bin/wm-select

	# Try WM Manager.
	ExecIfExecutable /usr/X11R6/bin/wmselect

	# Try default from runwm.
	exec /usr/X11R6/bin/runwm default
fi
