#!/bin/sh
#
# Very simple session manager for matchbox tools
#

test -e /proc/power_key_off && echo "0" >/proc/power_key_off 2>/dev/null

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

# Uncomment below to enable parsing of debian menu entrys
# export MB_USE_DEB_MENUS=1 

if [ -e $HOME/.matchbox/session ]
then
    exec $HOME/.matchbox/session
fi

if [ -e /etc/matchbox/session ]
then
    exec /etc/matchbox/session
fi

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

if [ -f $HOME/Choices/lightnpower.cfg ]; then
    lightnpower.py --loadandquit
fi

if [ -f /usr/bin/gconfd-2 ]; then
    /usr/bin/gconfd-2 15 &
fi

MODEL=`cat /proc/deviceinfo/product`
echo 'MODEL:'$MODEL
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)
	test -f /etc/X11/kb/akita.xmodmap && xmodmap /etc/X11/kb/akita.xmodmap
	;;
    SL-C3000)
	test -f /etc/X11/kb/akita.xmodmap && xmodmap /etc/X11/kb/akita.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

openbox 2>/dev/null >/dev/null &

touch /tmp/.mb-restart

while [ -f /tmp/.mb-restart ];
do

rm -f /tmp/.mb-restart

if [ -f $HOME/.matchbox/mb-desktop-background ]; then
    MBBGND=`cat $HOME/.matchbox/mb-desktop-background`
else
    MBBGND=""
fi

if [ -f $HOME/.matchbox/mb-panel-orientation ]; then
    MBORIENT=`cat $HOME/.matchbox/mb-panel-orientation`
else
    MBORIENT="--orientation south"
fi

/usr/X11R6/bin/xset s 0

eval "matchbox-desktop $MBBGND" 2>/dev/null >/dev/null &
eval "matchbox-panel --no-flip $MBORIENT" 2>/dev/null >/dev/null

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

done

GCONF2_PID=`pidof gconfd-2`
if [ ! "$GCONF2_PID" = "" ]; then
    kill $GCONF2_PID
fi

test -e /proc/power_key_off && echo "1" >/proc/power_key_off 2>/dev/null
