#!/bin/bash
# Copyright (c) 2005-2010 Artica ST
#
# Author: Sancho Lerena <slerena@artica.es> 2006-2010
#
# /etc/init.d/tentacle_server
#
# System startup script for Tentacle Server
#
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 90 90
# description: Tentacle Server startup script
#
# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides:       tentacle_server
# Required-Start: $network
# Should-Start:   $syslog
# Required-Stop:  $network
# Should-Stop:    $network
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Short-Description: Tentacle Server startup script
# Description:    Tentacle Server startup script
### END INIT INFO

# Check for SUSE status scripts
if [ -f /etc/rc.status ]
then
	. /etc/rc.status
	rc_reset
else
	# Define rc functions for non-suse systems, "void" functions.
	function rc_status () { VOID=1; }
	function rc_exit () { exit; }
	function rc_failed () { VOID=1; }

fi

function get_pid {
	# This sets COLUMNS to XXX chars, because if command is run 
	# in a "strech" term, ps aux don't report more than COLUMNS
	# characters and this will not work. 
	COLUMNS=300
	TENTACLE_PID=`ps -Af | grep "$TENTACLE_PATH$TENTACLE_DAEMON" | grep "$TENTACLE_PORT" | grep -v grep | tail -1 | awk '{ print $2 }'`
	echo $TENTACLE_PID
}

# Pandora server settings
PANDORA_SERVER_PATH="/var/spool/pandora/data_in"

# Tentacle server settings
TENTACLE_DAEMON="tentacle_server"
TENTACLE_PATH="/usr/bin"
TENTACLE_USER="pandora"

TENTACLE_ADDR="0.0.0.0"
TENTACLE_PORT="41121"
TENTACLE_EXT_OPTS="-i.*\.conf:conf;.*\.md5:md5;.*\.zip:collections"

# Set umask to 0002, because group MUST have access to write files to
# use remote file management on Pandora FMS Enterprise.

umask 0007

# Main script
TENTACLE_OPTS="-a $TENTACLE_ADDR -p $TENTACLE_PORT -s $PANDORA_SERVER_PATH $TENTACLE_EXT_OPTS -d"

# Fix TENTACLE_PATH
case "$TENTACLE_PATH" in
	*\/)
	;;
	*)
		TENTACLE_PATH="${TENTACLE_PATH}/"
	;;
esac

if [ ! -f "${TENTACLE_PATH}$TENTACLE_DAEMON" ]; then
	echo "Tentacle Server not found in ${TENTACLE_PATH}$TENTACLE_DAEMON"
	rc_failed 1
	rc_exit
fi

case "$1" in
	start)
		TENTACLE_PID=`get_pid`
		if [ ! -z "$TENTACLE_PID" ]; then
			echo "Tentacle Server is already running with PID $TENTACLE_PID"
			rc_failed 2
			rc_exit	
		fi
	
		sudo -u $TENTACLE_USER ${TENTACLE_PATH}$TENTACLE_DAEMON $TENTACLE_OPTS
		sleep 1

		TENTACLE_PID=`get_pid`
		if [ ! -z "$TENTACLE_PID" ]; then
			echo "Tentacle Server is now running with PID $TENTACLE_PID"
			rc_status -v
		else
			echo "Tentacle Server could not be started."
			echo "Verify that port $TENTACLE_PORT is not used."
			rc_status -v
		fi

	;;
	
	stop)
		TENTACLE_PID=`get_pid`
		if [ -z "$TENTACLE_PID" ]; then
			echo "Tentacle Server does not seem to be running"
			rc_failed 2
		else
			kill $TENTACLE_PID
			sleep 1
			TENTACLE_PID=`get_pid`

			if [ ! -z "$TENTACLE_PID" ]; then
				echo "Tentacle Server could not be stopped"
				rc_status -s
			fi

			echo "Stopping Tentacle Server"
			rc_status -v
		fi

	;;

	force-reload|restart)
		$0 stop
		sleep 1
		$0 start
		rc_status
	;;

	status)
		TENTACLE_PID=`get_pid`
		if [ -z "$TENTACLE_PID" ]; then
			echo "Tentacle Server is not running."
			rc_status 
		else
			echo "Tentacle Server is running with PID $TENTACLE_PID."
			rc_status 
		fi
		
	;;

	*)
		echo "Usage: $0  {start | stop | restart | status}"
		exit 1
	;;
esac

rc_exit