#!/bin/sh

# This script will launch chef-hipchat-plugin in a mode that will automatically respawn if it
# crashes. Output will be sent to /var/log/chef-hipchat-plugin/node.log. A PID file will be
# created in the standard location.

start () {
    echo -n "Starting chef-hipchat-plugin..."

    # Start daemon
    daemon --chdir='/srv/chef-hipchat-plugin/' --command "/usr/bin/nodejs app.js" --output=/var/log/chef-hipchat-plugin/node.log --name=chef-hipchat-plugin --verbose

    RETVAL=$?
    if [ $RETVAL = 0 ]
    then
        echo "done."
    else
        echo "failed. See error code for more information."
    fi
    return $RETVAL
}

stop () {
    # Stop daemon
    echo -n "Stopping chef-hipchat-plugin..."

    daemon --stop --name=chef-hipchat-plugin  --verbose
    RETVAL=$?

    if [ $RETVAL = 0 ]
    then
        echo "Done."
    else
        echo "Failed. See error code for more information."
    fi
    return $RETVAL
}


restart () {
    daemon --restart --name=chef-hipchat-plugin  --verbose
}


status () {
    # Report on the status of the daemon
    daemon --running --verbose --name=chef-hipchat-plugin
    return $?
}


case "$1" in
    start)
        start
    ;;
    status)
        status
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
        echo $"Usage: chef-hipchat-plugin {start|status|stop|restart}"
        exit 3
    ;;
esac

exit $RETVAL
