#!/bin/sh

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

VERSION=1.6.83

### BEGIN INIT INFO
# Provides:          vividcortex
# Required-Start:    $remote_fs $local_fs $syslog
# Required-Stop:     $remote_fs $local_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 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/default/$SERVICE ] && . /etc/default/$SERVICE

# Load required variables and functions (LSB)
. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	start-stop-daemon --start --quiet --exec $DAEMON --test > /dev/null || return 1
	start-stop-daemon --start --quiet --exec $DAEMON -- $SUPERVISOR_ARGS || return 2
}

do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred

	# Kill vc-agent-007, return if that cannot be done. (If it wasn't running, that's OK.)
	# (Trying to kill the other agents if vc-agent-007 won't die will only lead to madness.)
	start-stop-daemon --stop --quiet --oknodo --retry=TERM/5/KILL/5 --exec $DAEMON --user root
	RETVAL="$?"
	[ "$RETVAL" -ne 0 ] && return "$RETVAL"
	rm -f /var/run/vc-agent-007.pid

	FAILVAL=0
	for a in $AGENTS; do
		if [ -x "${BIN_PATH}/${a}" ]; then
			start-stop-daemon --stop --quiet --oknodo --retry=TERM/5/KILL/5 --exec "${BIN_PATH}/${a}" --user root
			RETVAL=$?
			if [ "$RETVAL" -eq 0 ]; then
				rm -f /var/lock/${a}_*.lock /var/run/${a}_*.pid
			else
				FAILVAL="$RETVAL"
			fi
		else
			# Just in case there's something old left behind.
			rm -f /var/lock/${a}_*.lock /var/run/${a}_*.pid
		fi
	done

	# Will return 0 only if all agents were killed successfully.
	return $FAILVAL
}

case "$1" in
  start)
	pidofproc "$DAEMON" >/dev/null && exit 0

	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;

  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;

  status)
       status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;

  restart|force-reload)
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;

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

