#!/bin/sh
#
# session manager for icewm
#

MODEL=`cat /proc/deviceinfo/product`
echo " Model is $MODEL"

if [ -f $HOME/Choices/pdaxii13.conf ]; then
    PDAXII=true
    echo " found pdaxii13.conf"
    if [ "`grep RESUME_BEEP $HOME/Choices/pdaxii13.conf|grep true`" != "" ]; then
      echo " - beep on resume is enabled"
    else
      echo " - beep on resume is disabled"
    fi
    if [ "`grep GMPLAYER $HOME/Choices/pdaxii13.conf|grep true`" != "" ]; then
      echo " - default mplayer is gmplayer"
    else
      echo " - default mplayer is mplayer-bvdd"
    fi
else
    PDAXII=false
    echo " pdaxii13.conf not found"
fi


if [ "`which feh`" != "" ] && [ "$PDAXII" = "true" ] && [ -f $HOME/.matchbox/autoexec ]; then
    if [ "`grep SPLASH $HOME/Choices/pdaxii13.conf|grep true`" != "" ]; then
        feh -Z -z -x -g 640x480 /usr/share/pixmaps/Wallpapers/splash.png &
	echo "... displaying splash screen" 
    else
	echo "... splash screen is not enabled" 
    fi
fi

if [ -f /usr/bin/unclutter ]; then
    unclutter -idle 5 &
fi

test -f /etc/profile && . /etc/profile

if [ ! -d $HOME/Choices/common ]; then
    mkdir -p $HOME/Choices/common
fi

if [ -f $HOME/Choices/lightnpower.cfg -a -e /usr/bin/lightnpower.py ]; then
    echo "... loading lightnpower settings" 
    lightnpower.py --loadandquit
fi

if [ -f /usr/bin/gconfd-2 ]; then
    echo "... starting gconf daemon" 
    /usr/bin/gconfd-2 15 &
fi

if [ -f /usr/share/sounds/welcome.wav ] && [ "$PDAXII" = "true" ]; then
    if [ "`grep PLAYSOUND $HOME/Choices/pdaxii13.conf|grep true`" != "" ]; then
        mplayer2 /usr/share/sounds/welcome.wav 2>/dev/null >/dev/null &
	echo "... playing welcome sound"
    else
	echo "... welcome sound is not enabled"
    fi
fi

case "$MODEL" in
    SL-C860|SL-C760|SL-C750|SL-7500|SL-C700) 
	test -f /etc/X11/kb/corgi.xmodmap && xmodmap /etc/X11/kb/corgi.xmodmap
	;;
    SL-6000)
	test -f /etc/X11/kb/tosa.xmodmap && xmodmap /etc/X11/kb/tosa.xmodmap
	;;
    SL-C1000|SL-C3000|SL-C3100|SL-C3200)
        echo "... loading spitz.xmodmap"
	#test -f /etc/X11/kb/akita.xmodmap && xmodmap /etc/X11/kb/akita.xmodmap
	test -f /etc/X11/kb/spitz.xmodmap && xmodmap /etc/X11/kb/spitz.xmodmap
	;;
    *)
	cat /proc/cpuinfo | grep 'Sharp-Collie' 2>/dev/null >/dev/null && test -f /etc/X11/kb/collie.xmodmap && xmodmap /etc/X11/kb/collie.xmodmap
	;;
esac

test -f /etc/X11/kb/userdefined.xmodmap && xmodmap /etc/X11/kb/userdefined.xmodmap
test -f /usr/bin/chkhinge && /usr/bin/chkhinge 2>/dev/null >/dev/null

mixmos -r 1 2>/dev/null >/dev/null
echo "... loading preset volume settings"

xhost +127.0.0.1 2>/dev/null >/dev/null
echo "... enabling xhost for loopback"

if [ "`grep STICKYKEYS $HOME/Choices/pdaxii13.conf|grep true`" = "" ]; then
    echo "... accessx (sticky keys) is not enabled"
else
    /usr/X11R6/bin/ax +stickykeys -stickytwokeydisable -stickylatchtolock -mousekeys -bouncekeys -timeout 2>/dev/null
    echo "... accessx (sticky keys) enabled"
fi
/usr/X11R6/bin/xset -r 35
/usr/X11R6/bin/xset -r 120 
/usr/X11R6/bin/xset -r 77
/usr/X11R6/bin/xset -r 64 
/usr/X11R6/bin/xset -r 78 
/usr/X11R6/bin/xset s 0 0

if [ "`date +%Y`" = "2004" ]; then
  echo "... setting new date and time"
  /etc/rc.d/init.d/atd stop 2>/dev/null >/dev/null
  datentime.py
  hwclock --systohc
  /etc/rc.d/init.d/atd start 2>/dev/null >/dev/null
fi

if [ "`which rox`" != "" ]; then
  if [ "$PDAXII" != "true" ]; then
        echo "... found rox, loading rox pinboard"
        eval "rox --pinboard=MYPINBOARD 2>/dev/null >/dev/null" &
  elif [ "`grep ROX $HOME/Choices/pdaxii13.conf|grep true`" != "" ]; then
	echo "... loading rox pinboard"
        eval "rox --pinboard=MYPINBOARD 2>/dev/null >/dev/null" &
  fi
fi

if [ "`which scim`" != "" ] && [ "$PDAXII" = "true" ]; then
    if [ "`grep SCIM $HOME/Choices/pdaxii13.conf|grep true`" != "" ]; then
        echo "... enabling SCIM"
        export GTK_IM_MODULE=scim
        export QT_IM_MODULE=scim
        export XMODIFIERS="@im=SCIM"
	if [ "`grep scim  /etc/gtk-2.0/gtk.immodules`" = "" ]; then
	  gtk-query-immodules-2.0 > /etc/gtk-2.0/gtk.immodules
	fi
        eval "scim -d 2>/dev/null >/dev/null" &
    else
        echo "... SCIM is not enabled"
        export GTK_IM_MODULE=""
        export QT_IM_MODULE=""
        export XMODIFIERS=""
	if [ "`grep scim /etc/gtk-2.0/gtk.immodules`" != "" ]; then
	  mv /etc/gtk-2.0/gtk.immodules /etc/gtk-2.0/gtk.immodules.tmp
	  grep -v scim /etc/gtk-2.0/gtk.immodules.tmp > /etc/gtk-2.0/gtk.immodules
	  rm /etc/gtk-2.0/gtk.immodules.tmp
	fi
    fi
fi

if [ "`which xscreensaver`" != "" ] && [ "$PDAXII" = "true" ]; then
    if [ "`grep XSCREENSAVER $HOME/Choices/pdaxii13.conf|grep true`" != "" ]; then
        echo "... enabling xscreensaver"
	if [ "`id -u`" = "0" ]; then
          eval "sudo -u zaurus xscreensaver -nosplash 2>/dev/null >/dev/null&"
        else
          eval "xscreensaver -nosplash 2>/dev/null >/dev/null&"
        fi
    else
        echo "... xscreensaver is not enabled"
    fi
fi

if [ "$PDAXII" = "true" ] && [ "`which torsmo`" != "" ]; then
    if [ "`grep TORSMO $HOME/Choices/pdaxii13.conf|grep true`" != "" ]; then
        echo "... enabling torsmo"
	eval "torsmo -d 2>/dev/null >/dev/null &"
    else
        echo "... torsmo is not enabled"
    fi
fi

if [ ! -f /etc/icewm/Office ] && [ ! -f $HOME/.icewm/Office ]; then
    makeicemenu
fi

echo "... loading icewm"
if [ -f /etc/icewm/startup ]; then
  /usr/bin/icewm-session 2>/dev/null >/dev/null 
else
  /usr/bin/icewmbg & >/dev/null
  /usr/bin/icewmtray & >/dev/null
  if [ -f ~/.icewm/icedock ]; then
    ICEDOCK=~/.icewm/icedock
  else
    ICEDOCK=/etc/icewm/icedock
  fi
  if [ -f $ICEDOCK ]; then
    for i in `cat $ICEDOCK`
    do
      $i 2>/dev/null &
    done
  fi		
  mb-applet-tasks & >/dev/null
  /usr/bin/icewm 2>/dev/null >/dev/null 
fi

echo "... killing X"
for PID in `ps x | grep "scim-*" | grep -v "grep" | sed 's/^ *//' | cut -f1 -d' ' -`
do
    kill $PID 2>/dev/null >/dev/null
done

GCONF2_PID=`pidof gconfd-2`
if [ ! "$GCONF2_PID" = "" ]; then
    echo "... killing gconf daemon"
    kill $GCONF2_PID
fi
