#!/bin/bash
# Copyright (c) 2024 Pandora FMS
#
# /etc/init.d/pandorarc_agent_daemon
#
# System startup script for Pandora RC Agent
#
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 90 90
# description: Pandora RC Agent startup script
#
# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides:       ehorus_agent
# Required-Start: $network
# Should-Start:   $syslog
# Required-Stop:  $network
# Should-Stop:    $network
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Short-Description: Pandora RC Agent startup script
# Description:    Pandora RC Agent 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
	EH_AGENT_PID=`ps -Af | grep "$EH_AGENT_PATH$EH_AGENT_DAEMON" | grep -v grep | tail -1 | awk '{ print $2 }'`
	echo $EH_AGENT_PID
}

# Pandora RC Agent settings
EH_AGENT_DAEMON="pandorarc_agent"
EH_AGENT_PATH="/usr/bin"

# 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
EH_AGENT_OPTS="/etc/pandorarc/pandorarc_agent.conf"

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

if [ ! -f "${EH_AGENT_PATH}$EH_AGENT_DAEMON" ]; then
	echo "Pandora RC agent not found in ${EH_AGENT_PATH}$EH_AGENT_DAEMON"
	rc_failed 1
	rc_exit
fi

case "$1" in
	start)
		EH_AGENT_PID=`get_pid`
		if [ ! -z "$EH_AGENT_PID" ]; then
			echo "Pandora RC Agent is already running with PID $EH_AGENT_PID"
			rc_failed 2
			rc_exit	
		fi
	
		#export PERL_JSON_BACKEND=0
		${EH_AGENT_PATH}$EH_AGENT_DAEMON -f $EH_AGENT_OPTS
		sleep 1

		EH_AGENT_PID=`get_pid`
		if [ ! -z "$EH_AGENT_PID" ]; then
			echo "Pandora RC Agent is now running with PID $EH_AGENT_PID"
			rc_status -v
		else
			echo "Pandora RC Agent could not be started."
			rc_status -v
		fi

	;;
	
	stop)
		EH_AGENT_PID=`get_pid`
		if [ -z "$EH_AGENT_PID" ]; then
			echo "Pandora RC Agent does not seem to be running"
			rc_failed 2
		else
			kill $EH_AGENT_PID
			sleep 1
			_PID=`get_pid`

			if [ "$_PID" = "$EH_AGENT_PID" ]; then
				echo "Pandora RC Agent could not be stopped"
				rc_status -s
			fi

			echo "Stopping Pandora RC Agent"
			rc_status -v
		fi

	;;

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

	status)
		EH_AGENT_PID=`get_pid`
		if [ -z "$EH_AGENT_PID" ]; then
			echo "Pandora RC Agent is not running."
			rc_status 
		else
			echo "Pandora RC Agent is running with PID $EH_AGENT_PID."
			rc_status 
		fi
		
	;;

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

rc_exit
