#!/bin/bash
# Init script for Pandora FMS agent
# Generic GNU/Linux version
# Sancho Lerena, <slerena@gmail.com>
# v3.0

### BEGIN INIT INFO
# Provides: pandora_agent
# Required-Start: $network
# Required-Stop: $network
# Default-Start:  S 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start/stop pandora-agent daemon
### END INIT INFO

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin
PANDORA_PATH=/etc/pandora
DAEMON=/usr/bin/pandora_agent
PIDFILE=/var/run/pandora_agent.pid
LOGFILE=/var/log/pandora_agent.log

if [ ! -f $DAEMON ]
then
	echo "Pandora FMS Agent not found at $DAEMON, please check setup"
	exit
fi

case "$1" in
  start)
        PANDORA_PID=$(pidof -x $DAEMON)
	if [ ! -z "$PANDORA_PID" ]
	then
            echo "Pandora FMS Agent is currently running on this machine with PID $PANDORA_PID"
            echo "Cannot launch again. Aborting."
            exit 1
        fi
        nohup $DAEMON $PANDORA_PATH 2> $LOGFILE & PANDORA_PID=$!
        echo $PANDORA_PID > $PIDFILE
	echo "Pandora FMS Agent is now running with PID $PANDORA_PID"
        ;;
  stop)
	PANDORA_PID=$(pidof -x $DAEMON)
        if [ -z "$PANDORA_PID" ]
        then
		echo "Pandora FMS Agent is not running, cannot stop it. Aborting now..."
		exit 1
        else
		echo "Stopping Pandora Agent."
		kill $PANDORA_PID > /dev/null 2>&1
	        rm -f $PANDORA_PID        
	fi
        ;;
  force-reload|restart)
        $0 stop
		sleep 3
        $0 start
        ;;
  *)
        echo "Uso: /etc/init.d/pandora_agent_daemon {start|stop|restart|force-reload}"
        exit 1
esac

