#!/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

if [ "$ALIVE_TIMEOUT" != "" ] ; then
#move ALIVE TIMEOUt from syncd.conf file to syncdadvanced.conf file.
	echo "ALIVE_TIMEOUT=$ALIVE_TIMEOUT" >> /etc/fiad-conf/syncdadvanced.conf
	grep -v "ALIVE_TIMEOUT"	/etc/fiad-conf/syncd.conf > /etc/fiad-conf/syncdtemp.conf
	mv -f /etc/fiad-conf/syncdtemp.conf /etc/fiad-conf/syncd.conf
fi
if [ "$threshold_to_master" != "" ] ; then
#move threshold_to_master from syncd.conf file to syncdadvanced.conf file.
	echo "threshold_to_master=$threshold_to_master" >> /etc/fiad-conf/syncdadvanced.conf
	grep -v "threshold_to_master" /etc/fiad-conf/syncd.conf > /etc/fiad-conf/syncdtemp.conf
	mv -f /etc/fiad-conf/syncdtemp.conf /etc/fiad-conf/syncd.conf
fi


# get Failover Timeouts
if [ -f /etc/fiad-conf/syncdadvanced.conf ] ; then
	. /etc/fiad-conf/syncdadvanced.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
	echo "ALIVE_TIMEOUT=10" >> /etc/fiad-conf/syncdadvanced.conf
fi

# get ALIVE_TIMEOUT from syncd.conf to check if remote MASTER device is alive.
# Forced default settings to be 60sec in case of Hosted ecQX devices.
if [ "$threshold_to_master" = "" ] ; then
	if [ -f /mnt/factory/factorysafedir/ishosted ] ; then
		threshold_to_master=60
		echo "threshold_to_master=60" >> /etc/fiad-conf/syncdadvanced.conf
	else
		threshold_to_master=20
		echo "threshold_to_master=20" >> /etc/fiad-conf/syncdadvanced.conf
	fi
fi
threshold_to_master_sleep=$((threshold_to_master/5))
if [ "$threshold_to_master_sleep" = "0" ] ; then
	threshold_to_master_sleep=0.5
	threshold_to_master_sleep_end=0.5
else
	threshold_to_master_sleep_end=$((threshold_to_master%5))
	threshold_to_master_sleep_end=$((threshold_to_master_sleep_end+threshold_to_master_sleep))
	threshold_to_master_sleep_end=$(echo "$threshold_to_master_sleep_end-0.5"| bc -l)
	threshold_to_master_sleep=$(echo "$threshold_to_master_sleep-0.5"| bc -l)
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
MARKED_TO_DELETE_DYN_TIMER_UPDATE=0


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
		# Dynamically update Failover Timeouts
		if [ -f /etc/fiad-conf/issyncdupdate ] ; then
			rm -f /etc/fiad-conf/issyncdupdate
			if [ -f /etc/fiad-conf/syncdadvanced.conf ] ; then
				. /etc/fiad-conf/syncdadvanced.conf
			fi
			echo "<-- Info: Dynamically updated Timers, Keep Alive Timeout=$ALIVE_TIMEOUT, Failover Timeout = $threshold_to_master  " >/tmp/logs/keepalived.log
			threshold_to_master_sleep=$((threshold_to_master/5))
			if [ "$threshold_to_master_sleep" = "0" ] ; then
				threshold_to_master_sleep=0.5
				threshold_to_master_sleep_end=0.5
			else
				threshold_to_master_sleep_end=$((threshold_to_master%5))
				threshold_to_master_sleep_end=$((threshold_to_master_sleep_end+threshold_to_master_sleep))
				threshold_to_master_sleep_end=$(echo "$threshold_to_master_sleep_end-0.5"| bc -l)
				threshold_to_master_sleep=$(echo "$threshold_to_master_sleep-0.5"| bc -l)
			fi
		fi

# 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 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 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 $threshold_to_master_sleep
                            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.25
                                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.25
                                    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 $threshold_to_master_sleep
                                        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.25
                                            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.25
                                                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 $threshold_to_master_sleep
                                                    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.25
                                                        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.25
                                                            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 $threshold_to_master_sleep
                                                                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.25
                                                                    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.25
                                                                        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 $threshold_to_master_sleep_end
                                                                            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.25
                                                                                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.25
                                                                                    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
                                                    fi
                                                fi
                                            fi
                                        fi
                                    fi
                                fi
                            fi
                        fi
                    fi
                fi
                rm -f /tmp/wget/*
	else
		/bin/sleep $MASTER_TIMEOUT
		if [ "$MARKED_TO_DELETE_DYN_TIMER_UPDATE" = "$expected_count" ] ; then
			rm -f /etc/fiad-conf/issyncdupdate
			MARKED_TO_DELETE_DYN_TIMER_UPDATE=0
		fi
		if [ -f /etc/fiad-conf/issyncdupdate ] ; then
			#  Wait  to make sure the timer configuration parameters are synched with Master device...
			MARKED_TO_DELETE_DYN_TIMER_UPDATE=$((MARKED_TO_DELETE_DYN_TIMER_UPDATE+1))
			if [ -f /etc/fiad-conf/syncd.conf ] ; then
				. /etc/fiad-conf/syncd.conf
			fi
			if [ -f /etc/fiad-conf/syncdadvanced.conf ] ; then
				. /etc/fiad-conf/syncdadvanced.conf
			fi
			expected_timeout=$((ALIVE_TIMEOUT+MASTER_TIMEOUT))
			synchronization_timeout=$((sync_interval*2))
			expected_count=$((synchronization_timeout/expected_timeout))
			expected_count=$((expected_count+1))
		fi
		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
