#!/bin/bash
#
#	/etc/rc.d/init.d/statsd
#
# Starts the statsd daemon
#
# chkconfig: 2345 20 80
# description: Frontend aggregatation of messages destined for graphite daemon
# processname: statsd

### BEGIN INIT INFO
# Provides: statsd
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Frontend aggregatation of messages destined for graphite daemon
### END INIT INFO

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

NAME=statsd
INSTALL_DIR=/usr/share/$NAME
NODE_EXE=/usr/bin/nodejs

[ -x $NODE_EXE ] || exit 0
[ -f $INSTALL_DIR/stats.js ] || exit 0

RETVAL=0

#
# See how we were called.
#

start() {
	# Check if it is already running
	if [ ! -f /var/lock/subsys/$NAME ]; then
	    echo -n $"Starting $NAME daemon: "
        # daemon
        $NODE_EXE $INSTALL_DIR/stats.js /etc/$NAME/config.js >/dev/null 2>&1 &
	    RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            touch /var/lock/subsys/$NAME
            echo_success
        else
            echo_failure
        fi
	    echo
	fi
	return $RETVAL
}

stop() {
	echo -n $"Stopping $NAME daemon: "
    pid=$(ps aux | grep stats.js | grep node | awk '{print $2}')
    kill $pid
	RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/$NAME
        echo_success
    else
        echo_failure
    fi
	echo
    return $RETVAL
}


restart() {
	stop
	start
}	

reload() {
	trap "" SIGHUP
	killall -HUP stats.js
}	

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
reload)
	reload
	;;
restart)
	restart
	;;
condrestart)
	if [ -f /var/lock/subsys/$NAME ]; then
	    restart
	fi
	;;
status)
	if [ -f /var/lock/subsys/$NAME ]; then
        echo "$NAME is running"
        exit 0
    else
        echo "$NAME is stopped"
        exit 3
    fi
	;;
*)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
	exit 1
esac

exit $RETVAL

