#!/bin/sh

userresources=$HOME/.Xresources

if [ x"$XWINHOME" != x ]; then
    XINIT_DIR=$XWINHOME/lib/X11/xinit
else
    XINIT_DIR=/etc/X11/xinit
fi

# export OPENWINHOME and HELPPATH

export OPENWINHOME=/usr/openwin
export HELPPATH=$OPENWINHOME/help

sysresources=$XINIT_DIR/.Xresources
userdefaults=$HOME/.Xdefaults

# merge in defaults and keymaps
if [ -x /lib/cpp ]; then
    XRDB=xrdb
else
    XRDB="xrdb -nocpp"
fi

for res in ${sysresources}* $userresources $userdefaults ; do
    if [ -f $res ]; then
        $XRDB -merge $res
    fi
done

xsetroot -solid SteelBlue

# 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 

if [ -f /usr/share/lgtypes/needs_update -a -x /usr/sbin/rebuild_lgtypes ]; then
        /usr/sbin/rebuild_lgtypes
fi

# create the kde-config-dir (essential for kpanel)
if [ ! -d $HOME/.kde/config ]; then
  install -d -m 755 $HOME/.kde/config
fi

# create the 2nd config dir for KDE
for dir in Autostart Templates Trash; do
  if [ ! -d $HOME/Desktop/$dir ]; then
    install -d $HOME/Desktop/$dir
  fi
done

export KDEDIR=/opt/kde
exec $KDEDIR/bin/startkde 
