#!/bin/sh
#
#  DEFAULT KDE STARTUP SCRIPT ( KDE-1.1.2 )
#  Modified for TurboLinux (based on RedHat KDE 1.1.2)

# start early for visual feedback
kbgndwm &

# clean up old kde /tmp files that belong to the user
for file in /tmp/kfm_* /tmp/kio*; do
    if [ -O $file ]; then
	rm -fr $file
    fi
done

# start autorun daemon if available and not already running
if [ -x /usr/bin/autorun ]; then
    /sbin/pidof 2>/dev/null >/dev/null autorun || \
	/usr/bin/autorun --interval=1000 \
	--cdplayer=/usr/bin/kscd &
fi


# set up user environment if not present
# check for space on /tmp and $HOME and for write access
#  error exit, if not
space_tmp=`df /tmp | xargs | cut -d" " -f11`
space_home=`df $HOME | xargs | cut -d" " -f11`

if [ $space_tmp -lt 50 ]; then
    echo Not enough free disk space on /tmp
    exit 1
fi

if [ $space_home -lt 25 ]; then
    echo Not enough free disk space on $HOME
    exit 1
fi
testfile=KDE_$$.testfile

if ! echo TEST_TEXT >/tmp/$testfile 2>/dev/null ; then
    echo "Have no write permissions for /tmp"
    exit 1
fi
rm -f /tmp/$testfile

if ! echo TEST_TEXT >$HOME/$testfile 2>/dev/null ; then
     echo "Have no write permissions for $HOME"
     exit 1
fi
rm -f $HOME/$testfile

# create necessary directories/files
if [ ! -d $HOME/Desktop ]; then
  cp -a /etc/skel/Desktop $HOME
fi

if [ ! -d $HOME/.kde ]; then
  cp -a /etc/skel/.kde $HOME
fi

if [ ! -f $HOME/.kderc ]; then
  cp -a /etc/skel/.kderc $HOME
fi

f=$HOME/.kde/share/apps/kfm/magic
[ -f $f ] || dd if=/dev/random bs=1 count=16 | md5sum > $f


# initialize settings
kcontrol -init

# Start the common desktop tools in the background.
# The sleeps reduce disk usage during startup.
# kaudioserver will put itself in the background automagically.

# Only start up sound services if there is a soundcard configured.
if [ -f /etc/sysconfig/soundcard ]; then
    . /etc/sysconfig/soundcard
    if [ "$CARDTYPE" != "" ]; then
	sleep 1 ; kaudioserver
	(sleep 1 && exec kwmsound) &
    fi
else
    # have to assume maybe a soundcard was configured manually
    sleep 1; kaudioserver
    (sleep 1 && exec kwmsound) &
fi

# automatically determine bit depth
bpp=`/usr/X11R6/bin/xdpyinfo | grep "depths.*:.*8"`
if [ ! "$bpp" = "" ]; then
  (sleep 1 && exec kfm -ncols 96) &
else
  (sleep 1 && exec kfm) &
fi

(sleep 1 && exec krootwm) &
(sleep 1 && exec kpanel) &

# IBM NetVista Thin Client with TurboLinux 6.0.7
#   ICAClient fix for NT booted clients
if [ -e /usr/lib/ICAClient/config/Wfcmgr.ad ]; then
	xrdb -nocpp -load /usr/lib/ICAClient/config/Wfcmgr.ad 2>&1 | cat > /dev/null
fi
if [ -e /usr/lib/ICAClient/config/Wfica.ad ]; then
	xrdb -nocpp -merge /usr/lib/ICAClient/config/Wfica.ad 2>&1 | cat > /dev/null
fi

# finally, give the session control to the window manager
sleep 2 ; exec kwm

