#!/usr/local/bin/bash

# Copyright C 2002-2016 by Isilon Systems LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
#
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# Inc.
#
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems.
#
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc. All other trademarks and registered trademarks are the property of
# their respective holders.

debug=0
TERM=15
KILL=9
error=0
script=`basename $0`

if [[ "$1" == "-d" ]]; then
	debug=1
	shift
fi

if [[ $# != 2 ]]; then
	if [[ $# != 0 ]]; then
		echo "Error: 2 arguments required"
	fi
	error=1
else
	name=$1
	shift
	action=$1
	shift
	if [[ "$name" != "capture" && "$name" != "analysis" && "$name" != "reporting" && "$name" != "alerting" ]]; then
		echo "Error: process must be capture, analysis, reporting, alerting"
		error=1
	fi
	if [[ "$action" != "start" && "$action" != "stop" && "$action" != "restart" && "$action" != "status" ]]; then
		echo "Error: action must be start, stop, restart, or status"
		error=1
	fi
fi

if [[ "$error" == "1" ]]; then
	echo "Usage $0 [-d] process (start | stop | restart | status)"
	echo "  where process is capture, analysis, reporting, alerting"
	echo "  -d    : debug output and logging"
	exit 1
fi
process=isi_celog_$name

function debug() {
	if [[ "$debug" == "1" ]]; then
		echo "$*"
		logger -t $script "$*"
	fi
}
function console_log() {
	echo "$*"
	logger -t $script "$*"
}

function log() {
	logger -t $script "$*"
}

function debug_log() {
	if [[ "$debug" == "1" ]]; then
		echo "$*"
		logger -t $script "$*"
	else
		logger -t $script "$*"
	fi
}

# prints pidlist or empty string if no pids
function pidlist() {
	pgrep $process
}

# if running, return 0
function running() {
	pidlist=`pidlist`
	if [[ "$pidlist" != "" ]]; then
		debug Is running pidlist=$pidlist
		return 0
	else
		debug Is not running pidlist=$pidlist
		return 1
	fi
}

# sig, returns 0 if process no longer running, returns 1 if process still running after 3 seconds of backoff time
function sig() {
	SIG=$1
	pidlist=`pidlist`
	if [[ "$pidlist" != "" ]]; then
		log killing $process pidlist=$pidlist with signal=$SIG
		kill -$SIG $pidlist
	fi
	sleep 1
	for i in 1 2 3 4 5; do
		if ! running; then
			return 0
		fi
		sleep 1
	done
	return 1
}

# either stops process or exits
function stopaction() {
if ! sig $TERM; then
	if ! sig $KILL ; then
		console_log Unable to stop $process
		exit 1
	fi
fi
}

case $action in
	restart)
		debug_log Restarting $process: First stop...
		stopaction
		debug_log Restarting $process: Then start...
		$process
		sleep 1
		if ! running; then
			console_log Unable to start $process
			exit 1
		fi
		;;
	stop)
		debug_log Stopping $process
		stopaction
		;;
	start)
		if running; then
			console_log $process already running.
			exit 1
		fi
		debug_log Starting $process
		$process
		sleep 1
		if ! running; then
			console_log Unable to start $process
			exit 1
		fi
		;;
	status)
		if running; then
			echo $process is running.
		else
			echo $process is not running.
		fi
		;;
esac
