#!/bin/sh
#
#  DEFAULT KDE STARTUP SCRIPT ( KDE-1.1.2 )
#  Modified for Red Hat Linux 6.x

# 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

# 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/urandom bs=1 count=16 | md5sum > $f

# initialize settings
kcontrol -init

# start early for visual feedback
kbgndwm &

# 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.
hassound=`cat /proc/devices | grep sound`
if [ "$hassound" != "" ]; then
    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) &

# finally, give the session control to the window manager

sleep 2 ; exec kwm

