#!/bin/sh

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

VERSION=unknown

### BEGIN INIT INFO
# Provides:          vividcortex
# Required-Start:    $remote_fs $local_fs
# Required-Stop:     $remote_fs $local_fs
# Should-Start:      $syslog $network $named $time
# Should-Stop:       $syslog $network $named $time
# 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-mongo-query vc-mongo-metrics vc-mysql-query vc-mysql-metrics vc-os-metrics vc-pgsql-query vc-pgsql-metrics vc-redis-query vc-redis-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
				exit 0
				;;
			*)  [ "$VERBOSE" != no ] && log_end_msg 1
				exit 1
				;;
		esac
		;;

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

	status)
		status_of_proc "$DAEMON" "$NAME"
		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
						exit 0
						;;
					*)
						log_end_msg 1 # 1 old process is still running, other: failed to start
						exit 1
						;;
				esac
				;;
			*)
				log_end_msg 1 # Failed to stop
				exit 1
				;;
		esac
		;;

	reload)
		exit 3
		;;

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

exit 1 # should not reach here; 1 = generic or unspecified error
