#! /bin/sh
#
# Startup script for the System UI
#
# Copyright (c) 2005 Nokia Corporation
#
# Based on David Weinehall <david.weinehall@nokia.com> MCE init script
#
# @author Pablo Virolainen <ext-pablo.virolainen@nokia.com> <ext-pablo.virolainen@nokia.com>

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/systemui
NAME=systemui
DESC="System UI"
DAEMON_OPTS="--daemon"
PIDFILE=/var/run/$NAME.pid
INITFILE=/etc/init.d/$NAME
DSMETOOL=/usr/sbin/dsmetool
DSMETOOL_PARAMETERS="-G users -U systemui -n -1 -t "
STOP_PARAMETERS="-k "
# HOME for GTK theming...
HOME=/home/user
. /etc/osso-af-init/af-defines.sh
. /etc/osso-af-init/osso-gtk.defs
# HOME for esd...
HOME=/tmp

# abort if no executable exists
test -x $DAEMON || exit 0

set -e

case "$1" in
start)
	printf "Starting $DESC: $NAME"
#	start-stop-daemon --start --quiet --pidfile $PIDFILE \
#		--exec $DAEMON -- $DAEMON_OPTS
        $DSMETOOL $DSMETOOL_PARAMETERS $DAEMON
	printf ".\n"
	;;

stop)
	printf "Stopping: $DESC: $NAME"
#	start-stop-daemon --stop --oknodo --quiet --pidfile $PIDFILE --exec $DAEMON
	$DSMETOOL $STOP_PARAMETERS $DAEMON
        sleep 2
	TMP=`ps x | grep -- /usr/bin/systemui |grep -v "grep --"|wc -l |tr -d '\t'`
	if [ $TMP = 1 ]; then
		$DSMETOOL -S 9 $STOP_PARAMETERS $DAEMON
		sleep 1
	fi								
	printf ".\n"
	;;

restart|force-reload)
	printf "Restarting $DESC: $NAME"
#	start-stop-daemon --stop --oknodo --quiet --pidfile $PIDFILE --exec $DAEMON
#	sleep 1
#	start-stop-daemon --start --quiet --pidfile $PIDFILE \
#		--exec $DAEMON -- $DAEMON_OPTS
        $DSMETOOL $STOP_PARAMETERS $DAEMON
	sleep 2
	TMP=`ps x | grep -- /usr/bin/systemui |grep -v "grep --"|wc -l |tr -d '\t'`
	if [ $TMP = 1 ]; then
	  $DSMETOOL -S 9 $STOP_PARAMETERS $DAEMON
	  sleep 1
	fi
	    
	$DSMETOOL $DSMETOOL_PARAMETERS $DAEMON
	printf ".\n"
	;;

*)
	printf "Usage: $INITFILE {start|stop|restart|force-reload}\n" >&2
	exit 1
	;;
esac

exit 0
