#!/bin/sh
#
# ntopng init script

. /etc/rc.d/init.d/functions

prog="ntopng"
exec="/usr/bin/ntopng"
config="/etc/ntopng/ntopng.conf"
pidfile="/var/run/ntopng/ntopng.pid"
lockfile="/var/lock/subsys/ntopng"

start() {
    echo -n $"Starting $prog: "
    daemon $exec $config --community --daemon
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "

    pid=$(pidof ntopng)

    if [ -n "$pid" ]; then
        kill $pid 2>/dev/null
        sleep 2

        if pidof ntopng >/dev/null; then
            kill -9 $(pidof ntopng) 2>/dev/null
        fi

        rm -f $pidfile
        retval=0
    else
        retval=1
    fi

    if [ $retval -eq 0 ]; then
        success
        rm -f $lockfile
    else
        failure
    fi

    echo
    return $retval
}

restart() {
    stop
    sleep 3
    start
}

ntop_status() {
    pid=$(pidof ntopng)

    if [ -n "$pid" ]; then
        echo "Ntop-NG (pid $pid) is running"
        return 0
    else
        echo "Ntop-NG is stopped"
        return 3
    fi
}

case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    restart
    ;;
status)
    ntop_status
    ;;
*)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 2
esac

exit $?