#!/bin/bash

# use bash for "exec -l", howto run login shell by /bin/sh ?

if [ $# -eq 1 -a -n "$1" ]; then
	LXSESSION=$1
else
# default session
	LXSESSION=/usr/bin/startlxde
fi

[ -f /etc/profile ] && . /etc/profile
[ -f /etc/bashrc  ] && . /etc/bashrc

if [ -f /etc/X11/xinit/xinitrc-common ]; then
# fedora
	. /etc/X11/xinit/xinitrc-common
	exec -l bash -c "$LXSESSION"
elif [ -x /etc/X11/xinit/Xsession ]; then
# fedora
	exec /etc/X11/xinit/Xsession "$LXSESSION"
elif [ -x /etc/X11/Xsession ]; then
# mandriva, debian, ubuntu
	exec /etc/X11/Xsession "$LXSESSION"
elif [ -x /etc/X11/xinit/xinitrc ]; then
#suse
	export WINDOWMANAGER=$LXSESSION
	exec -l bash -c /etc/X11/xinit/xinitrc
else
# unknown, user should custom /etc/lxdm/xinitrc self
	if [ -x /etc/lxdm/xinitrc ]; then
		. /etc/lxdm/xinitrc "$LXSESSION"
	fi
	if [ -d /etc/X11/xinit/xinitrc.d ] ; then
		for f in /etc/X11/xinit/xinitrc.d/* ; do
			[ -x "$f" ] && . "$f"
		done
		unset f
	fi

	if which dbus-launch >/dev/null && test -z "$DBUS_SESSION_BUS_ADDRESS";
	then
		eval "$(dbus-launch --sh-syntax --exit-with-session)"
	fi

	exec -l bash -c "$LXSESSION"
fi