#!/bin/sh

if [ -f /etc/image_features ] ; then
	. /etc/image_features
fi

/etc/init.d/rsyncd stop

wait_and_echo() {
PID=$1
wait $PID
CODE=$?
return $CODE
}

if [ "$FEATURE_REDUNDANCY" != "1" ] ; then
    /bin/reboot
    exit 0;
fi
if [ -f /etc/fiad-conf/sysconfig/network.conf ] ; then
    . /etc/fiad-conf/sysconfig/network.conf
fi
if [ "$START_KEEPALIVED" != "true" ] ; then
    if [ -f /mnt/factory/factorysafedir/syncd.conf ] ; then
	rm -f /mnt/factory/factorysafedir/syncd.conf
    fi
    /bin/reboot
    exit 0;
fi
if [ ! -f /tmp/masterMode ] ; then
    /bin/reboot
    exit 0;
fi


if [ -f /etc/fiad-conf/syncd.conf ] ; then
	. /etc/fiad-conf/syncd.conf
fi

if [ -f /mnt/factory/factorysafedir/syncd.conf ] ; then
    . /mnt/factory/factorysafedir/syncd.conf
    rm -f /mnt/factory/factorysafedir/syncd.conf
fi

## Get Backup Device Mode
WGET_TIMEOUT=10
timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/ https://'$redundant_ip'/redundancy/backup_device_status >/dev/null)' &
wait_and_echo $!
if [[ $? -ne 0 ]]; then
    /bin/sleep 1
    echo "<-- Warning: Cannot Get Backup Device Mode at $(date) to $redundant_ip .." >/tmp/logs/keepalived.log
    timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/ https://'$redundant_ip'/redundancy/backup_device_status >/dev/null)' &
    wait_and_echo $!
    if [[ $? -ne 0 ]]; then
        /bin/sleep 1
        echo "<-- Warning:   Cannot Get Backup Device Mode at $(date) to $redundant_ip .." >/tmp/logs/keepalived.log
        timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/ https://'$redundant_ip'/redundancy/backup_device_status >/dev/null)' &
        wait_and_echo $!
        if [[ $? -ne 0 ]]; then
            echo "<-- Warning: No Access to Backup Device. Rebooting the master device." >/tmp/logs/keepalived.log
            /bin/reboot
            exit 0
        fi
    fi
fi
BackupDeviceStatus=$(cat /tmp/backup_device_status | grep Active)
rm -f /tmp/backup_device_status

if [ "$BackupDeviceStatus" != "" ] ; then
## Backup Device in Active Mode, so after reboot restore the previous Active Mode on Backup device.
    touch  /mnt/factory/factorysafedir/update_backup_device
else
    /bin/reboot
    exit 0
fi
## Send command to configure Backup Device in Passive Mode
echo "Config" > /etc/fiad-conf/redundancy/keepalived.swap

for sec in `seq 1 50`; do
    /bin/sleep 2
    echo "<-- Warning: Wait Backup device to be rebooted, $(date)" >/tmp/logs/keepalived.log
    timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/ https://'$redundant_ip'/redundancy/backup_device_status >/dev/null)' &
    wait_and_echo $!
    if [[ $? -ne 0 ]]; then
        /bin/sleep 1
        echo "<-- Warning: No Access to Backup device,   $(date) " >/tmp/logs/keepalived.log
        timeout $WGET_TIMEOUT bash -c '(wget --no-check-certificate -t 1 -nv -P /tmp/ https://'$redundant_ip'/redundancy/backup_device_status >/dev/null)' &
        wait_and_echo $!
        if [[ $? -ne 0 ]]; then
            echo "<-- Warning: No Access to Backup device, Rebooting   $(date) " >/tmp/logs/keepalived.log
	    break
        fi
    fi
done

/bin/reboot
exit 0
