#!/bin/sh

DESC="powermanagerservice"
NAME=powermanagerservice
DAEMON=/usr/bin/$NAME
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME.default ] && . /etc/default/$NAME.default

#
# Function that starts the daemon/service
#
do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    #start-stop-daemon --start --quiet --exec $DAEMON --test > /dev/null \
    #    || return 1
    start-stop-daemon --start --quiet --background --exec $DAEMON -- $LCDSVC_OPTIONS
}

do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --name $NAME
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    return "$RETVAL"
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
    #
    # If the daemon can reload its configuration without
    # restarting (for example, when it is sent a SIGHUP),
    # then implement that here.
    #
    start-stop-daemon --stop --signal 1 --quiet --name $NAME
    return 0
}

case "$1" in
    start)
        if [ "$START_DAEMON" = "true" ]; then
            do_start
	    echo "Started $NAME"
        else
            echo "Start $NAME failed, cause of START_DAEMON not true"
        fi
        ;;
    stop)
        echo  "Stopping $DESC" "$NAME"
        do_stop
        ;;
  status)
        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
  reload|force-reload)
        echo "Reloading $DESC" "$NAME"
        do_reload
        ;;
  restart)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        echo "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
            0|1)
                do_start
            ;;
            *)
            # Failed to stop
            echo "fail to stop"
            ;;
        esac
        ;;

        *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
        exit 3
        ;;
esac

exit 0

