#!/bin/bash

MONITOR_PATH=$JETHRO_HOME/bin
HOST_CONF_PATH=/opt/jethro/config
PID_PATH=/var/run/jethro

# this paramater have the following status:
# status1 - check if exist host-conf.ini file (exit 10 for exist, exit 11 for not exist)
# status2 - check if the kerberos init process succeed (exit 20 for success, exit 21 for failed)
# status3 - run the KerberosTicketRenewal.sh in loop - background
KERBEROS_STATUS=$1
# KERBEROS_CHECK=0 no paramaters in the host-conf.ini related to kerberos
# KERBEROS_CHECK=1 found paramaters in the host-conf.ini related to kerberos
# check if we are running kerberos inthe system
if [ -s "$PID_PATH/kerberosticketrenewal.pid" ]; then
	KERBEROS_CHECK=1
else
	KERBEROS_CHECK=0
fi

trap kill_servicesmonitor 31
function kill_servicesmonitor() {
    servicesmonitor_pid=$(cat $PID_PATH/servicesmonitor.pid 2>&1) 2>&1
    if [ -s "$PID_PATH/kerberosticketrenewal.pid" ]; then
        kerberodticketrenew_pid=$(cat $PID_PATH/kerberosticketrenewal.pid 2>&1) 2>&1
        kill -9 $kerberodticketrenew_pid 2>&1
        rm -f $PID_PATH/kerberosticketrenewal.pid 2>&1
    fi
    if [ -s "$PID_PATH/servicesmonitor.pid" ]; then
        kerberodticketrenew_pid=$(cat $PID_PATH/kerberosticketrenewal.pid 2>&1) 2>&1
        kill -9 $servicesmonitor_pid > /dev/null 2>&1
        rm -f $PID_PATH/servicesmonitor.pid 2>&1
    fi
	sleep 1
    exit 0
}


# check if we have more then one servicesmonitor.sh service in the background, if yes then kill all duplicate servicesmonitor.sh
if [ -s "$PID_PATH/servicesmonitor.pid" ]; then
	servicesmonitor_pid=$(cat $PID_PATH/servicesmonitor.pid 2>&1) 2>&1
	servicesmonitor_pid_list=$(ps aux | grep "[s]ervicesmonitor.sh" | awk {'print($2)'}) 2>&1
	for servicesmonitor in $servicesmonitor_pid_list; do
		if [ "$servicesmonitor" != "$servicesmonitor_pid" ]; then
			kill -9 $servicesmonitor 2>&1
		fi
	done
else
	servicesmonitor_pid_list=$(ps aux | grep "[s]ervicesmonitor.sh" | awk {'print($2)'}) 2>&1
	for servicesmonitor in $servicesmonitor_pid_list; do
		kill -9 $servicesmonitor 2>&1
	done
fi


# check if KerberosTicketRenewal.sh up and running, if not, then start it but only for the following conditions:
# one of the the kerberos paramaters are enabled in the file host-conf.ini
# running with the following status:
# status1 - check if exist host-conf.ini file (exit 10 for exist, exit 11 for not exist)
# status2 - check if the kerberos init process succeed (exit 20 for success, exit 21 for failed)
# status3 - run the KerberosTicketRenewal.sh in loop - background
case "$KERBEROS_STATUS" in
	status1 )	$MONITOR_PATH/kerberosTicketRenewal.sh status1
				KERBEROS_PID=$?
				if [ $KERBEROS_PID -eq 10 ]; then
					# if exist kerberos paramaters in the host-conf.ini
					exit 10
				elif [ $KERBEROS_PID -eq 11 ]; then
					# not using kerberos security
					exit 11
				else
					echo "unknown ERROR when checking kerberos paramaters in host-conf.ini, exiting..."
					exit 98
				fi ;;
	status2 )	$MONITOR_PATH/kerberosTicketRenewal.sh status2 
				KERBEROS_PID=$?
				if [ $KERBEROS_PID -eq 20 ]; then
					# sucess running kinit
					exit 20
				elif [ $KERBEROS_PID -eq 21 ]; then
					# failed to run kinit
					exit 21
				else
					echo "unknown ERROR when running kinit on kerberos security, exiting..."
					exit 99
				fi ;;
	status3 )	$MONITOR_PATH/kerberosTicketRenewal.sh status3 & 2>&1
				KERBEROS_PID=$!
				# using kerberos security
				KERBEROS_CHECK=1
				# create process id for kerberos security
				echo $KERBEROS_PID > $PID_PATH/kerberosticketrenewal.pid & 
				echo -e "$(date +%Y-%m-%d" "%H:%M:%S) JethroMonitor: Starting KerberosTicketRenewal with PID=$KERBEROS_PID"  >> /var/log/jethro/monitor.log 2>&1 ;;
	* )			: ;#echo -e "$(date +%Y-%m-%d" "%H:%M:%S) JethroMonitor: Starting Environment without Kerberos security"  >> /var/log/jethro/monitor.log 2>&1 ;;
esac

# check if JethroMonitor service is up and running only if the servicesmonitor.sh restart it, update the log file monitor.log
if [ -s "$PID_PATH/JethroMonitor.pid" ]; then
	JethroMonitor_pid=$(cat $PID_PATH/JethroMonitor.pid 2>&1)
	echo -e "$(date +%Y-%m-%d" "%H:%M:%S) JethroMonitor: JethroMonitor is up and running PID=$JethroMonitor_pid"  >> /var/log/jethro/monitor.log 2>&1
fi

# check if we have more then one kerberos script in the background, if yes then kill all duplicate kerberos scripts
if [ -s "$PID_PATH/kerberosticketrenewal.pid" ]; then
	kerberodticketrenew_pid=$(cat $PID_PATH/kerberosticketrenewal.pid 2>&1) 2>&1
	kerberos_list=$(ps aux | grep "[k]erberosTicketRenewal.sh" | awk {'print($2)'}) 2>&1
	for kerberos in $kerberos_list; do
		if [ "$kerberos" != "$kerberodticketrenew_pid" ]; then
			kill -9 $kerberos 2>&1
		fi
	done
fi

# first time running servicesmonitor.sh - check if we have pid for servicesmonitor.sh in /var/run/jethro/
if [ ! -s $PID_PATH/servicesmonitor.pid ]; then
	# first time running servicesmonitor.sh
	$MONITOR_PATH/servicesmonitor.sh 2>&1 & echo $! > $PID_PATH/servicesmonitor.pid &
	servicesmonitor_pid=$(cat $PID_PATH/servicesmonitor.pid 2>&1)
	echo -e "$(date +%Y-%m-%d" "%H:%M:%S) JethroMonitor: Starting servicesmonitor with PID=$servicesmonitor_pid"  >> /var/log/jethro/monitor.log 2>&1
fi

while :
do
	# check if servicesmonitor.sh is up and running, if not, then start it
	servicesmonitor_pid=$(cat $PID_PATH/servicesmonitor.pid 2>&1)
	# check if process id exists
	kill -0 $servicesmonitor_pid > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		# Process is running - doing nothing
		:
	else
		# restarting service servicesmonitor.sh
		$MONITOR_PATH/servicesmonitor.sh 2>&1 & echo $! > $PID_PATH/servicesmonitor.pid &
		servicesmonitor_pid=$(cat $PID_PATH/servicesmonitor.pid 2>&1)
		echo -e "$(date +%Y-%m-%d" "%H:%M:%S) JethroMonitor: servicesmonitor process was not found. Restarting... PID=$servicesmonitor_pid"  >> /var/log/jethro/monitor.log 2>&1
	fi
	
	# if the kerberos security is OFF then check every 10 seconds if the file host-conf.ini has been modify
	if [ $KERBEROS_CHECK -eq 0 ]; then
	# check if the file host-conf.ini has been modify in the last 10 seconds (/opt/jethro/config/host-conf.ini)
		if [ ! -z $(find $HOST_CONF_PATH/ -name host-conf.ini -mmin 0.2) ]; then
			# file host-conf.ini has been modify
			$MONITOR_PATH/kerberosTicketRenewal.sh status1
			KERBEROS_PID=$?
			if [ $KERBEROS_PID -eq 10 ]; then
				# kerberos paramaters has been changed in the host-conf.ini
				$MONITOR_PATH/kerberosTicketRenewal.sh status2
				$MONITOR_PATH/kerberosTicketRenewal.sh status3 & 2>&1
				KERBEROS_PID=$!
				KERBEROS_CHECK=1
				# create process id for kerberos security
				echo $KERBEROS_PID > $PID_PATH/kerberosticketrenewal.pid & 
				echo -e "$(date +%Y-%m-%d" "%H:%M:%S) JethroMonitor: Starting KerberosTicketRenewal with PID=$KERBEROS_PID"  >> /var/log/jethro/monitor.log 2>&1
			elif [ $KERBEROS_PID -eq 11 ]; then
				# kerberos paramaters dosen't changed in the host-conf.ini - doing nothing
				:
			else 
				echo -e "$(date +%Y-%m-%d" "%H:%M:%S) JethroMonitor: Unable to determinant what change in the file host-conf.ini related to kerberose security"
			fi
		fi
	fi
	
	# check if exists process kerberosticketrenewal.pid in /var/run/jethro/ if yes, check if the script kerberosTicketRenewal.sh is running if not, then run it
	if [ -s $PID_PATH/kerberosticketrenewal.pid ]; then
		# process id for kerberos exists
		kerberosticketrenewal_pid=$(cat $PID_PATH/kerberosticketrenewal.pid 2>&1)
		kill -0 $kerberosticketrenewal_pid > /dev/null 2>&1
		if [ $? -eq 0 ]; then
			# Process is running - doing nothing
			:
		else
			# restarting service kerberosTicketRenewal.sh
			$MONITOR_PATH/kerberosTicketRenewal.sh status3 2>&1 & echo $! > $PID_PATH/kerberosticketrenewal.pid &
			kerberosticketrenewal_pid=$(cat $PID_PATH/kerberosticketrenewal.pid 2>&1)
			echo -e "$(date +%Y-%m-%d" "%H:%M:%S) JethroMonitor: kerberosticketrenewal process was not found. Restarting... PID=$kerberosticketrenewal_pid"  >> /var/log/jethro/monitor.log 2>&1
		fi
	fi
	sleep 10 &
	wait $!

done

