#!/bin/sh
#
# np1arpwarn   Advertises ARPs to kill any NP1 slaves  
#
# chkconfig: 345 09 91
# description: Advertises ARPs to kill any NP1 slaves.

# ****************************************************************************
# Filename:    np1arpwarn
# Component:   N Plus 1 Redundancy
# Subsystem:   init.d script
# 
#      Copyright Meru Networks 2003, All Rights Reserved 
#  
# File Overview
# -------------
# Runs on a master controller to warn a slave controller that uses the same
# IP address as this master controller that is booting.  Give the slave
# controller enough time to relinquish control of the IP address so that
# the rest of the bootup process for this master controller is smooth.
# 
# 
# Author: Kannan Konath
#         embedUR systems
#         42808 Christy St., Suite 102
#         Fremont, CA 94538
#         Phone: +1 (510) 353-9111
#
# Creation Date: November 21, 2005 (checkin into Perforce)
#
# Modifications:
# ---------------------------------------------------------------------------
# Author         Date        Comments
#                mm-dd-yy    
# ---------------------------------------------------------------------------
# 
#              
# ****************************************************************************

LOG=/opt/wlan/var/log/np1arpwarn.log
INITLOG=/opt/wlan/var/log/meru_init.log
MERU_CONF_f=/opt/wlan/etc/meru.conf

if [ ! -f $INITLOG ]; then
    touch $INITLOG
fi

if [ -f /etc/init.d/meru-functions ]; then
    . /etc/init.d/meru-functions
else
    echo "/etc/init.d/meru-functions must exist to use $0" >> $INITLOG
    exit 1
fi

# Source meru.conf
if [ ! -f ${MERU_CONF_f} ]; then
    echo "${MERU_CONF_f} not present. $0 operation not possible" >> $INITLOG
    exit 1
else
    source ${MERU_CONF_f}

    # Validate REAL_IF
    if [ ! "${REAL_IF}" ]; then
        echo "$0 Incorrect configuration for REAL_IF." >> $INITLOG
        exit 1
    fi
fi
 
# Assume REAL_IF is the interface we want

start() {
	# Get the IP address for REAL_IF
	source /etc/sysconfig/network-scripts/ifcfg-$REAL_IF

	# Bring that interface up without an IP address
	/sbin/ifconfig $REAL_IF up
        
	# To avoid issue similar to Bug 15607 :
	# After interface is up, wait for 30 seconds,
	# link should have stabilised in that time.
	sleep 30

    let ii=1
    let imax=120
    let istate=0
    let istatemax=3

        
    while [ ${ii} -lt ${imax} ]; do
        # ARP advertise with duplicate detection 
        /sbin/arping -c 2 -D $IPADDR -I $REAL_IF > $LOG 
        STATE=`/bin/cat $LOG | /bin/grep "Received " | /bin/awk '{print \$2}'`
	    
        if [ "$STATE" == "0" ]; then
            echo "Done ARP advertising ($ii th trial) ..." >> $INITLOG

            # remove LOG
            rm -f $LOG

            # Even though we got no response, we want to make sure
            # arp reply is not delayed or lost. We will check istatemax times.
            if [ ${istate} -lt ${istatemax} ]; then
            	let istate=istate+1
            else 
                return
            fi
        else 

            RESPONSE=$(/bin/cat $LOG | /bin/grep "Unicast")
            echo "${RESPONSE} ($ii th trial) ..." >> $INITLOG
            sleep 1
        fi

        let ii=ii+1
    done
	
    # remove LOG
    rm -f $LOG

    # Continue on...
}

stop() {
	echo "Stopping np1arpwarn" > /dev/null 2>&1
}

case "$1" in
        start)
                start
                ;;
        stop)
		stop
                ;;
        restart)
                stop
                start
                ;;
        *)
                echo $"Usage: $prog {start|stop|restart}"
                exit 1
esac

exit 0
