#!/bin/sh
#
# Copyright(c) 2000-2003 Broadcom Corporation, all rights reserved
#
# Name:        baspteam
#
# Description: This script invokes baspcfg to add or delete a  
#              load-balancing, failover and VLAN team. This script
#              is part of Broadcom Advanced Server Program (BASP)
#              driver for Linux.
#
# Author:      Frankie Fan
#
# Created:     Dec 14, 2000
#

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

#
usage()
{
    echo "usage: $0 team-config {add|del}"
    exit 1
}
  
add_cleanup()
{
    $0 $TEAM_CFG "del"
    exit 1
}

# 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
    add)
        DO_ADD=1
        ;;
    del)
        DO_ADD=0
        ;;
    *)
        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

# add/delete the team
if [ $DO_ADD -eq 1 ]; then
   
    # add
    baspcfg $QUIET addteam $TEAM_ID $TEAM_TYPE $TEAM_NAME
    [ $? -ne 0 ] && exit 1

    # bind 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
            baspcfg $QUIET bind $TEAM_ID $pa_role $pa_name
            if [ $? -eq 0 ]; then
               PA_BOUND=` expr $PA_BOUND + 1 `
            fi
        fi
        i=`expr $i + 1`
    done

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

    # create the virtual interfaces
    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`
        if [ -n "$va_name" -a -n "$va_vlan" ]; then
            baspcfg $QUIET addva $TEAM_ID $va_vlan $va_name 
            if [ $? -eq 0 ]; then
                VA_BOUND=` expr $VA_BOUND + 1 `
            fi 
        fi
        i=`expr $i + 1`
    done

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

else
    # delete   

    # delete the virtual interfaces
    i=`expr $TEAM_VA_MAX - 1`
    VA_BOUND=0
    while [ $i -ge 0 ]; do
        va_name=`eval echo '$'TEAM_VA${i}_NAME`
        va_vlan=`eval echo '$'TEAM_VA${i}_VLAN`
        if [ -n "$va_name" -a -n "$va_vlan" ]; then
            baspcfg $QUIET delva $TEAM_ID $va_vlan 
        fi
        i=`expr $i - 1`
    done

    # unbind the physical interfaces
    i=`expr $TEAM_PA_MAX - 1`
    PA_BOUND=0
    while [ $i -ge 0 ]; 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
            baspcfg $QUIET unbind $TEAM_ID $pa_name
        fi
        i=`expr $i - 1`
    done

    # delete the team
    baspcfg $QUIET delteam $TEAM_ID 
fi

