#!/bin/sh
# Modification for Linux-Mandrake by Chmouel Boudjnah <chmouel@mandraksoft.com>
# 20000309, Francis Galiegue <fg@mandrakesoft.com>: imwheel -k added for wheel
# mice and braindead-not-supporting-wheel-yet toolkits (this includes Qt...)

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

# redirect errors to a file in user's home directory if we can
for errfile in "$HOME/.xsession-errors" "${TMPDIR-/tmp}/xses-$USER" "/tmp/xses-$USER"
do
    if ( cp /dev/null "$errfile" 2> /dev/null )
    then
	chmod 600 "$errfile"
	exec > "$errfile" 2>&1
	break
    fi
done

# Mandrake default background
if [ "`whoami`" = root ]; then
    xsetroot -solid "#B20003"
else
    xsetroot -solid "#21449C"
fi

AGENT=$(type -p ssh-agent)
if [ -x "$AGENT" ]; then
    if [ -r $HOME/.ssh/identity -o -r $HOME/.ssh2/identification -o -r $HOME/.ssh/id_dsa -o -r $HOME/.ssh/id_rsa ]; then
	SSH_AGENT="$AGENT --"
    fi
fi    

# clean up after xbanner
if [ -f /usr/X11R6/bin/freetemp ]; then
    freetemp
fi

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

# merge in defaults and keymaps 
if [ -f $sysresources ]; then 
    xrdb -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
	# we need to find a browser on this system
	BROWSER=`which mozilla`
	if [ -z "$BROWSER" ] || [ ! -e "$BROWSER" ] ; then
	# not found yet
		BROWSER=
	fi
fi

if [ -z "$BROWSER" ] ; then
	# we need to find a browser on this system
	BROWSER=`which netscape`
	if [ -z "$BROWSER" ] || [ ! -e "$BROWSER" ] ; then
	# not found yet
		BROWSER=
	fi
fi

if [ -z "$BROWSER" ] ; then
	# we need to find a browser on this system
	BROWSER=`which lynx`
	if [ -z "$BROWSER" ] || [ ! -e "$BROWSER" ] ; then
	# not found yet
		BROWSER=
	else
		BROWSER="xvt -e lynx"
	fi
fi
export BROWSER

export HELP_BROWSER="$BROWSER"

if [ -x /usr/sbin/chksession ];then 
    LIST=$(/usr/sbin/chksession -l)
else
    LIST="kde Gnome AfterStep Icewm AnotherLevel failsafe"
fi

# run scripts in /etc/X11/xinit.d
for i in /etc/X11/xinit.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

    if [ -x $i ]; then
	$i &
    fi
done

# now, we see if xdm/gdm/kdm 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

### Launch an XIM input server if needed
# it must be after definition of DESKTOP variable; but before any
# program that may want user input
if [ -x /etc/X11/xinit/XIM ]; then
    . /etc/X11/xinit/XIM
fi

### Launch first time wizard if needed
if [ ! -e $HOME/.drakfw ] && type drakfw > /dev/null 2>&1; then
    touch $HOME/.drakfw
    exec drakfw
fi

if [ -n "$DESKTOP" ]; then
    case $DESKTOP in
    	failsafe)
	exec $SSH_AGENT xvt -geometry 80x24-0-0
	exec $SSH_AGENT xterm -geometry 80x24-0-0
	;;
	default)
	;;
	*)
	exec $SSH_AGENT /bin/sh -c "$(/usr/sbin/chksession -x=$DESKTOP)"
	;;
    esac
fi

# otherwise, take default action
if [ -x "$HOME/.xsession" ]; then
	exec $SSH_AGENT "$HOME/.xsession"
elif [ -x "$HOME/.Xclients" ]; then
	exec $SSH_AGENT "$HOME/.Xclients"
fi

# We may try with chksession
if [ -x /usr/sbin/chksession ];then
#get the first available
	SESSION=$(/usr/sbin/chksession -F)
	[ "$SESSIONxxx" != "xxx" ] && exec $SSH_AGENT sh -c "$(/usr/sbin/chksession -x=$SESSION)"
fi

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