#!/bin/sh

# get $host  - from ez.conf
if [ -f /etc/fiad-conf/ez.conf ] ; then
	. /etc/fiad-conf/ez.conf
fi

# get $host  - from dyndns.conf
if [ -f /etc/fiad-conf/dyndns.conf ] ; then
	. /etc/fiad-conf/dyndns.conf
fi

# get redundant_ip from syncd.conf
if [ -f /etc/fiad-conf/syncd.conf ] ; then
	. /etc/fiad-conf/syncd.conf
fi

# get Redundancy method (KEEPALIVED_METHOD)
KEEPALIVED_METHOD=false
if [ -f /etc/fiad-conf/sysconfig/network.conf ] ; then
	. /etc/fiad-conf/sysconfig/network.conf
fi

if [ -f /etc/fiad-conf/redundancy/master_device_status ] ; then
	rm -f /etc/fiad-conf/redundancy/master_device_status
fi

# get ALIVE_TIMEOUT from syncd.conf to check if remote MASTER device is alive 
if [ "$ALIVE_TIMEOUT" = "" ] ; then
	ALIVE_TIMEOUT=10
fi

# get ALIVE_TIMEOUT from syncd.conf to check if remote MASTER device is alive 
if [ "$threshold_to_master" = "" ] ; then
	threshold_to_master=5
fi

# get MASTER_TIMEOUT from syncd.conf to reboot the device if DNS HOST is not assigned to this device
if [ "$MASTER_TIMEOUT" = "" ] ; then
		if [ "$KEEPALIVED_METHOD" = "true" ] ; then
			MASTER_TIMEOUT=120
		else
			MASTER_TIMEOUT=5
		fi
fi

STATE="BACKUP"
/bin/sleep $threshold_to_master
wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
if [[ $? -ne 0 ]]; then
	echo "<-- Access to $redundant_ip failed......" >/tmp/logs/keepalived.log
	wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
	if [[ $? -ne 0 ]]; then
		echo "<-- Access to $redundant_ip failed........." >/tmp/logs/keepalived.log
		/bin/sleep 1
		wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
		if [[ $? -ne 0 ]]; then
			STATE="MASTER"
		fi
	fi
fi

if [ "$STATE" = "MASTER" ] ; then
	echo "<-- Access to $redundant_ip failed, starting MASTER mode script..." >/tmp/logs/keepalived.log
	STATE="MASTER"
	/bin/setsid /etc/keepalived.master >/tmp/logs/keepalived.log &
	CURRENT_DATE=$(date +%s)
	echo "$CURRENT_DATE" >/etc/fiad-conf/redundancy/master_device_status
else
	echo "<-- Access to $redundant_ip success, starting BACKUP mode script..." >/tmp/logs/keepalived.log
	# Also parse master_device_status
	if [ -f /etc/fiad-conf/redundancy/master_device_status ] ; then
		rm -f /etc/fiad-conf/redundancy/master_device_status
	fi
	/bin/setsid /etc/keepalived.backup >/tmp/logs/keepalived.log &
fi


while [ 1 ] ; do
	/bin/sleep $ALIVE_TIMEOUT
	if [ "$STATE" = "BACKUP" ] ; then
# might be instead of master_device_status we may wget another file
		wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
		if [[ $? -ne 0 ]]; then
			/bin/sleep 1
			echo "<-- Warning: Access problem to $redundant_ip " >/tmp/logs/keepalived.log
			wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
			if [[ $? -ne 0 ]]; then
				/bin/sleep 0.5
				echo "<-- Warning: Access problem to $redundant_ip ." >/tmp/logs/keepalived.log
				wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
				if [[ $? -ne 0 ]]; then
					/bin/sleep 1
					echo "<-- Warning: Access problem to $redundant_ip .." >/tmp/logs/keepalived.log
					wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
					if [[ $? -ne 0 ]]; then
						/bin/sleep 1
						echo "<-- Warning: Access problem to $redundant_ip ..." >/tmp/logs/keepalived.log
						wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
						if [[ $? -ne 0 ]]; then
							/bin/sleep 0.5
							echo "<-- Warning: Access problem to $redundant_ip ...." >/tmp/logs/keepalived.log
							wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
							if [[ $? -ne 0 ]]; then
								/bin/sleep 1
								echo "<-- Warning: Access problem to $redundant_ip ....." >/tmp/logs/keepalived.log
								wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
								if [[ $? -ne 0 ]]; then
									/bin/sleep 1
									echo "<-- Warning: Access problem to $redundant_ip ......" >/tmp/logs/keepalived.log
									wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
									if [[ $? -ne 0 ]]; then
										echo "<-- Access problem to $redundant_ip, starting MASTER mode script..." >/tmp/logs/keepalived.log
										STATE="MASTER"
										CURRENT_DATE=$(date +%s)
										echo "$CURRENT_DATE" >/etc/fiad-conf/redundancy/master_device_status
										/bin/setsid /etc/keepalived.master >/tmp/logs/keepalived.log &
									fi
								fi
							fi
						fi
					fi
				fi
			fi
		fi
		rm -f /tmp/wget/*
	else
		/bin/sleep $MASTER_TIMEOUT
		if [ "$KEEPALIVED_METHOD" = "true" ] ; then
			ping -c 2 $host | grep $ROUTER_IP >/dev/null
			if [[ $? -ne 0 ]]; then
				ping -c 2 $host | grep $ROUTER_IP >/dev/null
				if [[ $? -ne 0 ]]; then
					ping -c 2 $host | grep $ROUTER_IP >/dev/null
					if [[ $? -ne 0 ]]; then
						echo "<-- The $host is not assigned to this device, the system will reboot in one second..." >/tmp/logs/keepalived.log
						/bin/sleep 1
						/bin/reboot
					fi
				fi
			fi
		else
			wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
			if [[ $? -ne 0 ]]; then
				wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
				if [[ $? -ne 0 ]]; then
					/bin/sleep 1
					wget --no-check-certificate -t 1 -T 1 -nv -P /tmp/wget https://$redundant_ip/redundancy/master_device_status >/dev/null
					if [[ $? -ne 0 ]]; then
						continue
					fi
				fi
			fi
			CURRENT_DATE=$(date +%s)
			REMOTE_DATE=$(cat /tmp/wget/master_device_status)
			if [ "$REMOTE_DATE" -lt "$CURRENT_DATE" ]; then
				echo "<-- System detected two MASTER device, the system will reboot in one second..." >/tmp/logs/keepalived.log
				/bin/sleep 1
				/bin/reboot
			fi
			rm -f /tmp/wget/*
		fi
	fi
done
