#!/bin/bash
# moksha-hub - This init script runs the Moksha Hub
#
# chkconfig: - 25 85
# description:  Enabled the moksha hub daemon
# processname:  moksha-hub
# config: /etc/moksha/production.ini
# pidfile: /var/run/moksha/moksha-hub.pid

### BEGIN INIT INFO
# Provides: moksha-hub
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start:
# Default-Stop: 0 1 6
# Short-Description: start or stop the moksha-hub
# Description: The moksha hub connects to a message broker and runs the moksha \
# message producers and consumers
### END INIT INFO

# Source function library.
. /etc/init.d/functions

PROG=moksha-hub
USER=moksha
PIDFILE=/var/run/moksha/$PROG.pid
OPTIONS=--daemon
SUBSYS=/var/lock/subsys/$PROG

start() {
    echo -n "Starting the Moksha Hub: "
    if [ -f $PIDFILE.lock ]; then
        echo Moksha Hub already running
        exit 2;
    fi

    if [ ! -d /var/run/moksha ]; then
        mkdir /var/run/moksha
        chown $USER:$USER /var/run/moksha
    fi

    daemon --user $USER $PROG $OPTIONS
    RETVAL=$?
    echo

    if [ $RETVAL -eq 0 ]; then
        success
        touch $SUBSYS
    else
        failure
    fi
}

stop() {
        echo -n $"Stopping the Moksha Hub: "
        killproc -p ${PIDFILE} $PROG
        echo
        rm -f ${SUBSYS}
        RETVAL=$?
        echo
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        if [ -f $PIDFILE ]; then
            echo $"Moksha Hub is running."
            RETVAL=0
        else
            echo $"Moksha Hub is not running."
            RETVAL=3
        fi
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage:  {start|stop|status|reload|restart}"
        exit 1
        ;;
esac
exit $?
