#!/bin/sh

# This program is Copyright (c) 2013 VividCortex, Inc. All rights reserved.

VERSION=1.5.17

### BEGIN INIT INFO
# Provides:          vividcortex
# Required-Start:    $remote_fs $local_fs $syslog
# Required-Stop:     $remote_fs $local_fs $syslog
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: VividCortex supervisor daemons
# Description:       agent_007 is the main VividCortex daemon. It's in charge
#                    of starting other agents as required and updating
#                    binaries when new versions are available.
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

DESC="VividCortex supervisor daemon"
NAME=vc-agent-007
SERVICE=vividcortex
BIN_PATH=/usr/local/bin
DAEMON=$BIN_PATH/$NAME
SUPERVISOR_ARGS=""
AGENTS="vc-aggregator vc-mysql-query vc-mysql-metrics vc-os-metrics"


# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read optional config file
[ -r "/etc/sysconfig/$SERVICE" ] && . /etc/sysconfig/$SERVICE

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

lockfile="/var/lock/subsys/$SERVICE"
RETVAL=0

start() {
	if [ ! -f "$lockfile" ]; then
		echo -n $"Starting $NAME: "	
	    daemon "$DAEMON $SUPERVISOR_ARGS"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && touch "$lockfile"
	    echo
	fi
	return $RETVAL
}

stop() {
	echo -n "Stopping $NAME: "
	killproc "$DAEMON"
	RETVAL=$?

	for a in $AGENTS; do
		pid=`pidofproc "$BIN_PATH/$a"`

		if [ $? -eq 0 ]; then
			kill $pid
		fi
	done

	[ $RETVAL -eq 0 ] && rm -f "$lockfile"
	echo
    return $RETVAL
}

restart() {
	stop
	start
}	

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart)
	restart
	;;
status)
	status "$NAME"
	RETVAL=$?
	;;
*)
	echo $"Usage: $0 {start|stop|restart|status}"
	RETVAL=2
esac

exit $RETVAL
