#!/bin/bash -x
# Cygwin script based on the KDE default start script and SUSE
# startkde script.
#
# $Id: startkde,v 1.4 2002/11/14 21:35:29 habacker Exp $
#
# Requirements: 
# This script requires the settings from kde314.sh and qt3.x.sh located 
# in /etc/profile.d. These files are part of the kdelibs respective qt 
# binary package. 
# 
# Boot sequence:
#
# kdeinit is used to fork off processes which improves memory usage
# and startup time.
#
# * kdeinit starts the dcopserver and klauncher first.
# * then kdesktop is launched, it is started very early in the startup
#   process to give the user visual feedback as soon as possible.
#   Since it needs a working ksycoca database it waits for a
#   "sycoca database changed" signal from kded before it becomes functional.
# * Then kded is started. kded is responsible for keeping the sycoca
#   database up to date. When an up to date database is present it sends
#   a "sycoca database changed" signal.
#
# * The rest of the start-up sequence is less critical.
#
# kdeinit starts dcopserver, klauncher, kdesktop, kicker, and kded

# set misc vars 
#export DISPLAY=127.0.0.1:0.0

#
# setup a shell for konsole 
#
export SHELL=/bin/bash

#
# cygwin kdeinit supports running kio process as regular apps, 
# rather than as forked kdeinit instances.
# Running kio_xxx.exe requires setting an additional path to 
# the kio modules 
# uncomment the first line to disable this feature  
# 
export KDEINIT_KIO_EXEC=1
if test -n "$KDEINIT_KIO_EXEC"; then 
	export PATH=/opt/kde3/lib/kde3:$PATH
fi 
# Prevent an ever growing .ICEauthority file, since that will kill application
# startup performance.
rm -f $HOME/.ICEauthority

# The user's personal KDE directory is usually $HOME/.kde3, but this setting
# may be overridden by setting KDEHOME.

kdehome=$HOME/.kde31
#
# set the enviroment, if not yet set
#
[ -z "$QTDIR" ]        && export QTDIR=/opt/qt3
[ -z "$KDEDIR" ]          && export KDEDIR=/opt/kde3
[ -z "$KDEHOME" ]          && export KDEHOME=$kdehome
#
# create some user depended dirs in the $KDEHOME/share dir if not exist 
#
[ -e "$KDEHOME/share/mimelnk"  ] || cp -a $KDEDIR/share/mimelnk $KDEHOME/share 
[ -e "$KDEHOME/share/applnk"   ] || cp -a $KDEDIR/share/applnk $KDEHOME/share 
[ -e "$KDEHOME/share/services" ] || cp -a $KDEDIR/share/services $KDEHOME/share 
[ -e "$KDEHOME/share/servicetypes" ] || cp -a $KDEDIR/share/servicetypes $KDEHOME/share 

if test "$1" != "-multiwindow"; then 
	#
	# some fast color for the background
	# better for our eyes ;-)
	#
	[ -e "${kdehome}/share/config/kdesktoprc" ] && \
	  color="`sed -n '/^Color1=/ s/Color1=//p' "${kdehome}/share/config/kdesktoprc" | head -1 | awk -F \, '{ printf("#%02x%02x%02x",$1,$2,$3) }' -`"
	[ -z "$color" -a "$USER" = "root" ] && color="#bd0000"
	[ -z "$color" -a "$USER" != "root" ] && color="#004e98"
	xsetroot -cursor_name left_ptr -solid "$color"
fi 	
# These aren't used by Cygwin - but some Linux programs may expect them.
# (particularly LD_LIBRARY_PATH).
#
test -n "${LD_LIBRARY_PATH}" && \
    export LD_LIBRARY_PATH="${KDEDIR}/lib:${KDEDIR}/bin:${QTDIR}/lib:${QTDIR}/bin:${LD_LIBRARY_PATH}" || \
  export LD_LIBRARY_PATH="${KDEDIR}/lib:${KDEDIR}/bin:${QTDIR}/lib:${QTDIR}/bin"
test -n "${LTDL_LIBRARY_PATH}" && \
    export LTDL_LIBRARY_PATH="${KDEDIR}/lib:${KDEDIR}/bin:${QTDIR}/lib:${QTDIR}/bin:${LD_LIBRARY_PATH}" || \
  export LTDL_LIBRARY_PATH="${KDEDIR}/lib:${KDEDIR}/bin:${QTDIR}/lib:${QTDIR}/bin"

#
# Check first for current resolution/depth
#
export X=`xdpyinfo |grep dimensions:|cut -d " " -f 7|cut -d "x" -f 1`
export Y=`xdpyinfo |grep dimensions:|cut -d " " -f 7|cut -d "x" -f 2`
export D=`xdpyinfo |awk '/depths/ {print $NF}'`
[ -e "$HOME/.skel/kdebase" ] || INITIAL="--force"

#
# delete a existing ksocket in /tmp. It makes trouble on startup
# this is only a workaround, but it should fix the problem for 
# the most people, sorry
rm -rf "/tmp/ksocket-${USER}" "${KDEHOME}/tmp-${HOSTNAME}"

#
# Link "tmp" and "socket" resource to directory in /tmp
# Creates a directory /tmp/kde-$USER and links $KDEHOME/tmp-$HOSTNAME to it.
lnusertemp tmp >/dev/null
lnusertemp socket >/dev/null

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

echo "startkde: Starting up mode $1 ... "  1>&2

# run KPersonalizer before the session, if this is the first login
if kreadconfig --file kpersonalizerrc --group General --key FirstLogin --default true --type bool; then
    # start only dcopserver, don't start whole kdeinit (takes too long)
    echo 'startkde: Running kpersonalizer...'  1>&2
    dcopserver
    kwin &
    kpersonalizer --before-session
    # handle kpersonalizer restarts (language change)
    while test $? -eq 1; do
        kpersonalizer --r --before-session
    done
    dcopserver_shutdown
    # shutdown will also make kwin quit, give it time to do so
    sleep 1
fi

# the splashscreen and progress indicator
# do not start it on locolor displays

if test "$1" != "-multiwindow"; then 
	[ "`xdpyinfo |awk '/depths/ {print $NF}'`" -gt 8 ] && ksplash
else 
	[ "`xdpyinfo |awk '/depths/ {print $NF}'`" -gt 8 ] && ksplash --test
fi

#
# select x mode depending apps settings 
#
case "$1" in 
	-multiwindow)
		AUTOSTART_DIR=autostart$1
		KDEWM="unknown"	# dummy name to avoid loading of a window manager 
		KDEINIT_APPS="+kcminit +knotify"
		;;
	-rootless | -fullscreen | *)
		AUTOSTART_DIR=autostart-default
		KDEINIT_APPS="+kcminit +knotify"
		;;	
esac

rm $KDEHOME/share/autostart
ln -fs $KDEDIR/share/$AUTOSTART_DIR $KDEHOME/share/autostart
	
# launch basic kde applications 
kdeinit $KDEINIT_APPS

# finally, give the session control to the session manager
# if the KDEWM environment variable has been set, then it will be used as KDE's
# window manager instead of kwin.
# if KDEWM is not set, ksmserver will ensure kwin is started.
# kwrapper is used to reduce startup time and memory usage
# kwrapper does not return usefull error codes such as the exit code of ksmserver.
# We only check for 255 which means that the ksmserver process could not be 
# started, any problems thereafter, e.g. ksmserver failing to initialize, 
# will remain undetected.
test -n "$KDEWM" && KDEWM="--windowmanager $KDEWM"
kwrapper ksmserver $KDEWM 
if test $? -eq 255; then
  # Startup error
  echo 'startkde: Could not start ksmserver. Check your installation.'  1>&2
  xmessage -geometry 500x100 "Could not start ksmserver. Check your installation."
fi

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

# Clean up
kdeinit_shutdown
dcopserver_shutdown

# kill other kde applications 
for i in `ps -s | egrep '(kded|kdeinit|knotify|kicker|kdesktop|kio)' | cut -c 4-7`; do kill -15 $i; sleep 2; done 

if test -n "$KDEINIT_KIO_EXEC"; then 
	for i in `ps -s | egrep '(kio)' | cut -c 4-7`; do kill -15 $i; done 
fi 

echo 'startkde: Running shutdown scripts...'  1>&2

# Run scripts found in $KDEDIRS/shutdown
for prefix in `kde-config --path exe| sed -e 's^bin/^shutdown/^g;s^:^ ^'`
do 
  for file in $prefix/*
  do 
    if test -f $file
    then 
       $file
    fi
  done
done

echo 'startkde: Done.'  1>&2
