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

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

test -e /etc/rc.d/init.d/switchev && /etc/rc.d/init.d/switchev restart

# 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 -a -e /usr/bin/lightnpower.py ]; then
    lightnpower.py --loadandquit
fi

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

HARDWARE=`cat /proc/cpuinfo | grep 'Hardware' | cut -d':' -f2 | cut -d' ' -f3`
echo "pdaXrom [${HARDWARE}]"

test -f /etc/X11/kb/${HARDWARE}/xmodmap && xmodmap /etc/X11/kb/${HARDWARE}/xmodmap

test -f /etc/X11/kb/userdefined.xmodmap && xmodmap /etc/X11/kb/userdefined.xmodmap

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
