#!/bin/sh
#
# Copyright 2003 Meru Networks
# All rights reserved.
#

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"; exit 1
fi

set_params

get_second_if_params()
{
	SECOND_SUBNET_ADDR=`ifconfig $SECOND_IF | perl -ne 'if (/.*inet addr:(.+?)\s/) { print $1; }'`;
	SECOND_SUBNET_MASK=`ifconfig $SECOND_IF | perl -ne 'if (/.*Mask:(.+?)\s/) { print $1; }'`;
	SECOND_SUBNET_BCST=`ipcalc -b $SECOND_SUBNET_ADDR $SECOND_SUBNET_MASK 2>/dev/null | cut -d= -f 2`
	SUBNET_MASK_CIDR=`ipcalc $SECOND_SUBNET_ADDR $SECOND_SUBNET_MASK -p | cut -d= -f 2`
	NETWORK_ID=`ipcalc $SECOND_SUBNET_ADDR $SECOND_SUBNET_MASK -n | cut -d= -f 2`
}

start() 
{    
    # Disable second_if's ip address
    if [ "$SECOND_INTERFACE_MODE" = "active" ]; then
   
    	ifdown $SECOND_IF
        ifup $SECOND_IF

        # setting all the params like  SECOND_SUBNET_ADDR SECOND_SUBNET_MASK
	get_second_if_params

        if [ "$SECOND_SUBNET_ADDR" = "" ] ;then
            ifdown $FIRST_IF 
            # force Meru to fail too
            fail "Cannot start second interface."
        fi

    fi

    SEC_REAL_IF_HAS_DHCP=false
    [ "`pgrep dhcpcd`" ] && [ "`pgrep dhcpcd | xargs ps | grep -e $SECOND_IF`" ] && 
    {
        pgrep dhcpcd | xargs ps | grep -e $SECOND_IF | awk '{print $1}' | xargs kill -9
        SEC_REAL_IF_HAS_DHCP=true
        # Wait for DHCP to die
        while pgrep dhcpcd | xargs ps | grep -e $SECOND_IF
        do
            sleep 1
        done
    }

    #when the second device is in active mode...i.e., has an IP
    #address..either dhcp'd or static
    if [ "$SECOND_INTERFACE_MODE" = "active" ]; then
        # Bringing all physical interfaces up.
        ifconfig $SECOND_IF 0.0.0.0 promisc || fail "ifconfig $SECOND_IF failed"                                

        if [ "$SEC_REAL_IF_HAS_DHCP" = "true" ]; then
            #REAL_IF uses dhcp, reconfigure on meru2 (since mac is the same, we should get the same ip address)
            dhcpcd -n $SECOND_UDP_IF >/dev/null 2>&1 || fail "dhcpcd on $SECOND_UDP_IF failed"
            [ -d /etc/dhcpc ] && cp -p /opt/meru/etc/dhcpcd.exe /etc/dhcpc
        else
            ifconfig $SECOND_UDP_IF $SECOND_SUBNET_ADDR netmask $SECOND_SUBNET_MASK broadcast $SECOND_SUBNET_BCST up || fail "ifconfig $SECOND_UDP_IF failed"
        fi
       
        if [ "$SEC_REAL_IF_HAS_DHCP" = "true" ]; then
            GW=`cat /etc/dhcpc/dhcpcd-$SECOND_IF.info | grep GATEWAY | awk -F"=" '{print $NF}'`
        else
            GW=`cat /etc/sysconfig/network-scripts/ifcfg-$SECOND_IF | grep GATEWAY | awk -F"=" '{  print $NF }'`
        fi
        
		if [ `echo $GW | grep "'" ` ]; then
			GW=`echo $GW | awk -F"'" '{print $2}'`
        fi 
        ip route del $NETWORK_ID/$SUBNET_MASK_CIDR dev $SECOND_UDP_IF
        if [ "$GW" != "" ] ;then
            route del default gw $GW  dev $SECOND_UDP_IF
            /sbin/route add $GW dev $SECOND_UDP_IF 
        fi

    fi

    # Now bring up the second interface in the redundant mode..i.e., no
    #IP, just the state of the interface is changed to UP and the
    #rebridging interface migration daemon is started

    # RebridMover is always running for monitoring link status
    mknod  /dev/rebrid c 254 3
    /etc/init.d/RebridMover start

	if [ "$HIGH_AVAILABILITY_STATE" = "1" ]; then
		/etc/init.d/wncagent restart
	fi 

}

stop() 
{
    #This function will be called only when HA is on.
    if [ "$SECOND_INTERFACE_MODE" = "active" ]; then	        
        ifconfig $SECOND_UDP_IF down
        ifconfig $SECOND_IF down
		# we call this function cos the HA is being turned off
		if [ -f /tmp/ha_stopped ]; then
			start;
			rm -rf /tmp/ha_stopped
		fi
        #ifconfig $SECOND_IF -promisc || fail "ifconfig $SECOND_IF failed" 		
    else	 
        ifconfig $SECOND_IF -promisc
        ifconfig $SECOND_IF down
    fi    
}

down()
{
    #This function will be called only when HA is off  by meru-wnc's stop function.
    SEC_UDP_IF_HAS_DHCP=false
    if [ "$SECOND_INTERFACE_MODE" = "active" ]; then	        
        SEC_UDP_IF_ROUTES=`ip route show | egrep ".* via .* dev $UDP_IF" | awk '{print $1 " " $3 " "}'`
        [ "`pgrep dhcpcd`" ] && [ "`pgrep dhcpcd | xargs ps | grep $SECOND_UDP_IF`" ] && 
        {
            pgrep dhcpcd | xargs ps | grep $SECOND_UDP_IF | awk '{print $1}' | xargs kill -9
            SEC_UDP_IF_HAS_DHCP=true
        }
        ifconfig $SECOND_UDP_IF down		 
        ifconfig $SECOND_IF -promisc || fail "ifconfig $SECOND_IF failed" 		
    else	 
        ifconfig $SECOND_IF -promisc
        ifconfig $SECOND_IF down
    fi
    if [ "$SEC_UDP_IF_HAS_DHCP" = "true" ]; then
        [ -f /etc/dhcpc/dhcpcd.exe ] && rm -f /etc/dhcpc/dhcpcd.exe
        # UDP_IF uses dhcp,
        dhcpcd -n $SECOND_IF >/dev/null 2>&1
    else
        ifconfig $SECOND_IF $SECOND_SUBNET_ADDR netmask $SECOND_SUBNET_MASK up
    fi
    if [ "$SECOND_INTERFACE_MODE" = "active" ]; then
        # Re-instate routes
        FIRST=true
        for tok in $SEC_UDP_IF_ROUTES
        do
            [ "$FIRST" = "true" ] && 
            {
                route=$tok
                FIRST=false
                continue
            }
            gw=$tok
            FIRST=true
            ip route add $route via $gw  dev $SECOND_IF >/dev/null 2>&1
        done
    fi    
}


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

exit 0
