#!/bin/sh
# Set lang and according locale
#SourceIfNotEmpty /etc/profile.d/lang.sh

FixBrowser()
{
    BROWSER="kfmclient openProfile webbrowsing"
    HELP_BROWSER="kfmclient openProfile webbrowsing"
}

trap 'echo GOT SIGHUP' HUP
xsetroot -cursor_name left_ptr

[ -z "$KDEDIR" ] && KDEDIR=/usr
if [ -z "$KDEHOME" ]; then  KDEHOME=$HOME/.kde; else KDEHOME=`echo "$KDEHOME"|sed "s,^\~/,$HOME/,"`; fi
if [ -n "$TMPDIR" ]; then KDETMP=$TMPDIR; else KDETMP=/tmp; fi

# fix $BROWSER
if [ -n "$BROWSER" ]; then
    if [ "$BROWSER" == "/usr/bin/xbrowser" -a -L /usr/bin/xbrowser ]; then
	[ "$(readlink `readlink /usr/bin/xbrowser`)" == "$KDEDIR""/bin/konqueror" ] && FixBrowser
    else	
	[ "$BROWSER" == "$KDEDIR""/bin/konqueror" ] && FixBrowser
    fi
fi

export KDEDIR KDEHOME KDETMP BROWSER HELP_BROWSER

lnusertemp tmp >/dev/null
lnusertemp socket >/dev/null
lnusertemp cache >/dev/null

# In case of dcop sockets left by a previous session, cleanup
dcopserver_shutdown

echo 'startkde: Starting up...'  1>&2

# We set LD_BIND_NOW to increase the efficiency of kdeinit.
# kdeinit unsets this variable before loading applications.
LD_BIND_NOW=true kdeinit +kcminit +knotify
if test $? -ne 0; then
  # Startup error
  echo 'startkde: Could not start kdeinit. Check your installation.'  1>&2
  xmessage -center "Could not start kdeinit. Check your installation."
fi

kwrapper ksmserver
if test $? -eq 255; then
  # Startup error
  echo 'startkde: Could not start ksmserver. Check your installation.'  1>&2
  xmessage -center "Could not start ksmserver. Check your installation."
fi

echo 'startkde: Shutting down...'  1>&2

# Clean up
kdeinit_shutdown
dcopserver_shutdown
artsshell -q terminate

echo 'startkde: Done.'  1>&2
