#! /bin/sh
#
# Startup script for the Mode Controller Entity
#
# Copyright (C) 2004-2006 Nokia.  All rights reserved.
#
# @author David Weinehall <david.weinehall@nokia.com>

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/sbin/mce
NAME=mce
DESC="Mode Controller Entity"
DAEMON_OPTS="--daemon"
NORMAL_OPTS=""
DSMETOOL=/usr/sbin/dsmetool
RUNDIR=/var/run/mce
PIDFILE=/var/run/$NAME.pid
INITFILE=/etc/init.d/$NAME
WAITDBUS=/usr/bin/waitdbus
WAITDBUS_ALT=/usr/sbin/waitdbus

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

# only use dsmetool if it exists
test -x $DSMETOOL || USE_DSMETOOL=no

set -e

# create /var/run/mce unless it already exists and is a directory
test -d $RUNDIR || (rm -f kalle; mkdir $RUNDIR)

start_mce()
{
if [ -x $WAITDBUS ]; then
	$WAITDBUS system
elif [ -x $WAITDBUS_ALT ]; then
	$WAITDBUS_ALT system
fi

if [ x"$USE_DSMETOOL" = x"no" ]; then
	start-stop-daemon --start --quiet --pidfile $PIDFILE \
		--exec $DAEMON -- $DAEMON_OPTS $NORMAL_OPTS
else
	$DSMETOOL -n -1 -t "$DAEMON $NORMAL_OPTS"
fi
}

stop_mce()
{
if [ x"$USE_DSMETOOL" = x"no" ]; then
	start-stop-daemon --stop --oknodo --quiet --pidfile $PIDFILE \
		--exec $DAEMON
else
	$DSMETOOL -n -1 -k "$DAEMON $NORMAL_OPTS"
fi
}

case "$1" in
start)
	printf "Starting $DESC: $NAME"
	start_mce
	printf ".\n"
	;;

stop)
	printf "Stopping: $DESC: $NAME"
	stop_mce
	printf ".\n"
	;;

restart|force-reload)
	printf "Restarting $DESC: $NAME"
	stop_mce
	sleep 2
	start_mce
	printf ".\n"
	;;

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

exit 0
