### BEGIN INIT INFO
# Provides: overFlow
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: overFlow agent
# Description: overFlow Agent
### END INIT INFO

PROG="ofcollector"
PROG_PATH="/home/insanity/develop/overflow/overflow.collector/bin/"
PID_PATH="/var/run/"
FILE_SERVER=/var/run/of_server

start() {
    if [ -e "$PID_PATH/$PROG.pid" ]; then
        ## Program is running, exit with error.
        echo "$PROG is currently running!" 1>&2
        exit 1
    else
        $PROG_PATH/$PROG 2>&1 > /dev/null &
        echo "$PROG started"
        touch "$PID_PATH/$PROG.pid"
    fi
}

stop() {
    sudo echo -n 'STOP' | netcat -U $FILE_SERVER

    if [ -e "$PID_PATH/$PROG.pid" ]; then
        ## Program is running, so stop it
        #killall $PROG
        rm "$PID_PATH/$PROG.pid"
        echo "$PROG stopped"
    else
        ## Program is not running, exit with error.
        echo "Error! $PROG not started!" 1>&2
        exit 1
    fi
}

status() {
    sudo echo -n $1 | netcat -U $FILE_SERVER
}


if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    reload|restart|force-reload)
        stop
        start
    ;;
    status)
        status 'STATUS'
   ;;
    **)
        echo "Usage: $0 {start|stop|restart|status}" 1>&2
        exit 1
    ;;
esac