#!/bin/sh 
#
# Copyright(c) 2000-2003 Broadcom Corporation, all rights reserved
#
# Name:        baspif
#
# Description: This script invokes ifconfig to start or stop network
#              interfaces participating in load-balancing, failover 
#              and VLAN team. This script is part of Broadcom 
#              Advanced Server Program (BASP)| driver for Linux.
#
# Author:      Frankie Fan
#
# Updated:     Apr 12, 2002
#

#
PATH=/sbin:/usr/sbin:/bin:/usr/bin

#
usage()
{
    echo "usage: $0 team-config {start|stop}"
    exit 1
}

#
# support_action is defined for following distributions
#	0	generic
#	1	RedHat
#	2	Suse
#
# load the functions
if [ -d /usr/src/redhat -a -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
    support_action=1
elif [ -f /etc/rc.status ]; then
    . /etc/rc.status
    support_action=2
else
    support_action=0
fi

  
start_cleanup()
{
    exit 1
}

bring_up()
{
    if [ $support_action -eq 1 ]; then
        # Redhat
        ifup $1
    elif [ $support_action -eq 2 ]; then
        # Suse
        . /etc/rc.config
        . /etc/init.d/network start $1
    fi
}

# init
QUIET="-q"

# arguments check
[ -z "$1" ] && usage
[ ! -f $1 ] && usage
[ -z "$2" ] && usage
if [ -n "$3" ]; then
    [ "$3" = "-v" ] && QUIET=
fi

# parse required operation
case "$2" in
    start)
        DO_START=1
        DO_PRESTART=0
        ;;
    stop)
        DO_START=0
        DO_PRESTART=0
        ;;
    prestart)
        DO_START=1
        DO_PRESTART=1
        ;;
    *)
        usage
        ;;
esac

# load the team configuration
TEAM_CFG=$1
. $TEAM_CFG

# sanity checks
[ -z "$TEAM_ID" ] && exit 1
[ -z "$TEAM_TYPE" ] && exit 1
[ -z "$TEAM_NAME" ] && exit 1

# require at least one physical interface
[ -z "$TEAM_PA0_NAME" ] && exit 1
[ -z "$TEAM_PA0_ROLE" ] && exit 1

# require least one virtual interface
[ -z "$TEAM_VA0_NAME" ] && exit 1
[ -z "$TEAM_VA0_VLAN" ] && exit 1

# constants
TEAM_PA_MAX=8
TEAM_VA_MAX=64

# start/stop the physical interfaces
i=0
PA_BOUND=0
while [ $i -lt $TEAM_PA_MAX ]; do
    pa_name=`eval echo '$'TEAM_PA${i}_NAME`
    pa_role=`eval echo '$'TEAM_PA${i}_ROLE`
    if [ -n "$pa_name" -a -n "$pa_role" ]; then
        if [ $DO_START -eq 1 ]; then
            if [ $DO_PRESTART -eq 1 ]; then
                bring_up $pa_name >/dev/null 2>&1
            fi
            ifconfig $pa_name 0.0.0.0 up >/dev/null 2>&1
            if [ $? -eq 0 ]; then
                PA_BOUND=` expr $PA_BOUND + 1 `
            fi
        else
            ifconfig $pa_name down >/dev/null 2>&1
        fi
    fi
    i=`expr $i + 1`
done

# if none of the PA is bound, error out
if [ $DO_START -eq 1 -a $PA_BOUND -eq 0 ]; then
    start_cleanup
fi

#
# start/stop the virtual interfaces
#
if [ $DO_PRESTART -eq 0 ]; then
    i=0
    VA_BOUND=0
    while [ $i -le $TEAM_VA_MAX ]; do
        va_name=`eval echo '$'TEAM_VA${i}_NAME`
        va_vlan=`eval echo '$'TEAM_VA${i}_VLAN`
        va_ip=`eval echo '$'TEAM_VA${i}_IP`
        va_netmask=`eval echo '$'TEAM_VA${i}_NETMASK`
        va_gw=`eval echo '$'TEAM_VA${i}_GW`
        if [ -n "$va_name" -a -n "$va_vlan" ]; then
            if [ $DO_START -eq 1 ]; then
                if [ -n "$va_ip" -a -n "$va_netmask" ]; then
                    ifconfig $va_name $va_ip netmask $va_netmask >/dev/null 2>&1
                    if [ "$va_gw" != "" ]; then
                        route add default gw $va_gw >/dev/null 2>&1
                    fi
                else
                    bring_up $va_name >/dev/null 2>&1
                fi
                if [ $? -eq 0 ]; then
                    VA_BOUND=` expr $VA_BOUND + 1 `
                fi 
            else
                ifconfig $va_name down >/dev/null 2>&1
            fi
        fi
        i=`expr $i + 1`
    done

    # if none of the VA is bound, error out
    if [ $DO_START -eq 1 -a $VA_BOUND -eq 0 ]; then
        start_cleanup
    fi
fi
