#!/bin/sh
DESKTOP=default

# read the user (~/.i18n) or system-wide (/etc/sysconfig/i18n) i18n settings
. /etc/profile.d/10lang.sh

# Allow local users to run X applications, irregardless of system hostname
if [ -x /usr/bin/xhost ] && [ -x /bin/id ]; then
	xhost +si:localuser:$(id -un) >& /dev/null
fi

# OpenMandriva Linux default background
if [ "$(whoami)" = 'root' ]; then
	xsetroot -solid "#B20003"
	IS_ROOT=1
else
	IS_ROOT=
fi

xsetroot -cursor_name watch

userresources=$HOME/.Xresources
userresources2=$HOME/.Xdefaults
sysresources=/etc/X11/Xresources

# merge in defaults and keymaps
if [ -f $sysresources ]; then
	xrdb -nocpp -merge $sysresources
fi

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

if [ -f $userresources2 ]; then
	xrdb -merge $userresources2
fi

if [ -x /etc/X11/xinit/fixkeyboard ]; then
	/etc/X11/xinit/fixkeyboard
fi

if [ -z "$BROWSER" ] ; then
	BROWSER=$(command -v www-browser 2> /dev/null)
	if [ -n "$BROWSER" ] && [ -e "$BROWSER" ] ; then
		export BROWSER
	fi
fi

# now, we see if xdm/gdm/sddm has asked for a specific environment
if [ $# = 1 ]; then
	DESKTOP=$1
else
	# use default DESKTOP from config file
	# users may want to choose their own desktop
	# even when x-session is started by startx command.
	# -- Jaegeum --
	if [ -f $HOME/.desktop ]; then
		. $HOME/.desktop >/dev/null 2>&1
	elif [ -f /etc/sysconfig/desktop ]; then
		. /etc/sysconfig/desktop >/dev/null 2>&1
	fi
fi

# Fix for systemd dbus service not getting the display environment variables
# This fixes an lxqt-config-monitor crash

if [ -d /etc/X11/xinit/xinitrc.d ] ; then
	for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
		[ -x "$f" ] && . "$f"
	done
	unset f
fi

if [ "${DESKTOP}" = default ]; then
	SESSION=plasma
else
	SESSION=${DESKTOP}
fi

# run scripts in /etc/X11/xinit.d
for i in /etc/X11/xinit.d/* /etc/X11/xinit/xinitrc.d/* ; do
	[ -d $i ] && continue
	# Don't run ??foo.{rpmsave,rpmorig,rpmnew,~} scripts
	[ "${i%.rpmsave}" != "${i}" ] && continue
	[ "${i%.rpmorig}" != "${i}" ] && continue
	[ "${i%.rpmnew}" != "${i}" ] && continue
	[ "${i%\~}" != "${i}" ] && continue

	if [ -x $i ]; then
		if grep -q "# to be sourced" $i; then
			. $i "${SESSION}"
		else
			$i "${SESSION}" &
		fi
	fi
done

if [ -n "$DESKTOP" ]; then
	case $DESKTOP in
	failsafe)
		xsetroot -cursor_name left_ptr
		type -p rxvt > /dev/null >& 2 && exec rxvt -geometry 80x24-0-0
		type -p xterm > /dev/null >& 2 && exec xterm -geometry 80x24-0-0
		type -p xvt > /dev/null >& 2 && exec xvt
		type -p twm > /dev/null >& 2 && exec twm
		;;
	default)
		;;
	*)
		if type -p "start${DESKTOP}-x11" > /dev/null; then
			exec "start${DESKTOP}-x11"
		elif type -p "start${DESKTOP}" > /dev/null; then
			exec "start$DESKTOP"
		elif type -p "$DESKTOP" > /dev/null; then
			exec /bin/sh -c "$DESKTOP"
		elif echo "$DESKTOP" | grep -q ' '; then
			# DESKTOP could refer to an actual command as given by the DM
			# check if '$DESKTOP' contains a space, assume it is a full command
			# and then to just start it
			exec /bin/sh -c "$DESKTOP"
		# Invalid desktop setting -- let's just start the best one
		elif [ -x /usr/bin/startplasma-x11 ]; then
			exec /usr/bin/startplasma-x11
		fi
		;;
	esac
fi

# otherwise, take default action
if [ -x "$HOME/.xsession" ]; then
	xsetroot -cursor_name left_ptr
	exec /bin/sh -c "$HOME/.xsession"
elif [ -x "$HOME/.Xclients" ]; then
	xsetroot -cursor_name left_ptr
	exec /bin/sh -c "$HOME/.Xclients"
fi

[ -x /usr/bin/startplasma-x11 ] && exec /usr/bin/startplasma-x11
[ -x /usr/bin/startlxqt ] && exec /usr/bin/startlxqt

# Argh! Nothing good is installed. Fall back to icewm
if [ -x /usr/bin/icewm ]; then
	exec /usr/bin/icewm
else
	# gosh, neither icewm is available;
	# fall back to failsafe settings
	xsetroot -cursor_name left_ptr
	xclock -geometry 100x100-5+5 &
	xvt -geometry +0+50 &
	if [ -x "$BROWSER" -a -f /usr/share/doc/HTML/index.html ]; then
		$BROWSER /usr/share/doc/HTML/index.html &
	fi
	if [ -x /usr/bin/icewm-light ];then
		exec /usr/bin/icewm-light
	elif [ -x /usr/bin/twm ];then
		exec /usr/bin/twm
	elif [ -x /usr/bin/xsm ];then
		exec /usr/bin/xsm
	fi
fi
