#!/bin/bash
# Copyright (c) 2015-2016 WNB
#
# Author: Alexandre Vaz <avaz@wnb.com.br> 
#
# /etc/init.d/wnbmonitor
#
# System startup script for TLS WNB
#
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 90 10
# description: TLS Monitor startup script
#
# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides:       wnbmonitor
# Required-Start: $syslog cron
# Should-Start:   $network cron 
# Required-Stop:  $syslog 
# Should-Stop:    $network 
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description: TLS Monitor startup script
# Description:    WNB Monitor service  startup script
### END INIT INFO

# If you want to run several pandora servers in this machine, just copy 
# this script to another name, editing PANDORA_HOME to the new .conf 

export WNBMONITOR_DAEMON=/usr/bin/wnbmonitor
WNBREGISTRY=/etc/wnbtlscli/registry

# Uses a wait limit before sending a KILL signal, before trying to stop
# TLS server nicely. Some big systems need some time before close
# all pending tasks / threads.

export MAXWAIT=30

# 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
	
# This function replace pidof, not working in the same way in different linux distros

pidof_monitor () {
	# 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. 
	MONITOR_PID=`pgrep -f /usr/bin/wnbmonitor`
#	echo $MONITOR_PID
}

# Main script

if [ ! -f $WNBMONITOR_DAEMON ] 
then
	echo "WNB TLS Monitor  nao encontrado, verifique se a instalacao esta correta"
	rc_status -s
	rc_failed
	exit 1
else
 if [ ! -f $WNBREGISTRY ] 
 then
         echo "WNB TLS registro nao encontrado, verifique se foi executado wnbmonitor -k XXXXX"
         rc_status -s
         rc_failed
         exit 1
 else

  case "$1" in
	start)
		pidof_monitor
		if [ ! -z "$MONITOR_PID" ]
		then
			echo "WNB Monitor ja esta iniciado nesta maquina com o  PID ($MONITOR_PID). Cancelando startup..."
			rc_failed 1
			rc_exit 
		fi
	
		$WNBMONITOR_DAEMON -s &
		pidof_monitor
		
		if [ ! -z "$MONITOR_PID" ]
		then
			echo "WNBMONITOR iniciado com o  PID $MONITOR_PID"
			rc_status -v
		else
			echo "Erro ao iniciar aplicativo. Aborted."
			echo "Verifique os logs para detalhes do erro encontrado"
			rc_status -s
		fi
	;;
		
	stop)
		rm /var/run/wnbmonitor.lock
		pidof_monitor
		sleep 2
		if [ -z "$MONITOR_PID" ]
		then
			echo "WNBMONITOR nao encontrado, nao foi possivel parar o aplicativo."
			rc_failed 
		else
			echo "Parando o aplicativo WNBMONITOR"
			for i in `pgrep -f wnbmonitor`
			do
				kill $i
			done
		
			# Send a KILL -9 signal to process, if it's alive after 60secs, we need
			# to be sure is really dead, and not pretending...
			if [ "$_PID" = $MONITOR_PID ]
			then
				kill -9  $MONITOR_PID   > /dev/null 2>&1
			fi
			rc_status -v
		fi
		
		if [ $TLS_PID -gt 0 ]
		then
			echo "Parando o aplicativo WNBTLSCLI"
			for i in `pgrep -f /usr/bin/wnbtlscli`
			do
			kill $i
			done
		fi
	;;
	status)
	pidof_monitor	
		if [ -z "$MONITOR_PID" ]
		then
			echo "WNBMONITOR nao esta iniciado."
			rc_status 
		else
			echo "WNBMONITOR iniciado com o  PID $MONITOR_PID."
			rc_status
		fi
	;;
	force-reload|restart)
		$0 stop
		rm /var/run/wnbmonitor.lock
		$0 start
		;;
	forced-stop)
		rm /var/run/wnbmonitor.lock
		pkill -f wnb
		;;
	*)
		echo "Usage: wnbmonitor { start | stop | restart | status | forced-stop }"
		exit 1
  esac
 fi 
fi
rc_exit
