#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          prelude-lml
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog $remote_fs
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start prelude-lml sensor
### END INIT INFO



test $DEBIAN_SCRIPT_DEBUG && set -v -x

NAME=prelude-lml
PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/bin/prelude-lml
PIDFILE=/var/run/$NAME.pid
DAEMONARGS="-d -q -P /var/run/$NAME.pid"

trap "" 1
export LANG=C
export PATH

test -f $DAEMON || exit 0

check_prelude_profile()
{
    if [ ! -d "/etc/prelude/profile/$NAME" ]; then
        echo "Prelude profile for $NAME was not found"
        echo "You must create it with prelude-admin (see README.Debian)"
        echo "NOT starting."
        # do not return with an error, this would prevent package installation
        exit 0
    fi
}

case "$1" in
  start)
    echo -n "Starting Prelude LML: "
    check_prelude_profile
    start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON --oknodo \
    	--quiet -- $DAEMONARGS > /dev/null
    echo "$NAME."
	;;

  stop)
    echo -n "Stopping Prelude LML: "
    start-stop-daemon --stop --pidfile $PIDFILE --exec $DAEMON --quiet \
    	--oknodo > /dev/null
    echo "$NAME."
	;;

  restart|force-restart|reload|force-reload)
    echo -n "Restarting Prelude LML: "
    check_prelude_profile
    start-stop-daemon --stop --pidfile $PIDFILE --exec $DAEMON --quiet \
    	--oknodo > /dev/null
    start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON --oknodo \
    	--quiet -- $DAEMONARGS > /dev/null
    echo "$NAME."
        ;;

  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
       	;;
esac

if [ $? -eq 0 ]; then
	echo .
	exit 0
else
	echo failed
	exit 1
fi
