#!/bin/sh
##
## Compatability script for earlier versions.
## Note that if you are trying to create an mBSSID type of initialization scirpt,
## then another script should be used (or this one should be changed)
##
#MODLIST=`lsmod | grep ath_hal | cut -f1,0 -d" "`

#if [ "${MODLIST}" = "ath_hal" ]; then
#    echo "AP is already up"
#    exit
#fi

## UBICOM: Start bridge if it wasn't already
if [ ! -e /tmp/bridge_started ]; then
    echo "Need to start bridge first"
    sh /etc/init.d/start_br
fi
. /etc/ath/apcfg

if [ "${AP_STARTMODE}" = "standard" ]; then
    /etc/ath/makeVAP ap $AP_SSID ${AP_IFNUM}
    if [ $? != 0 ]; then
        echo "Unable to create VAP!"
        exit
    fi
    /etc/ath/activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE
fi

##
## See the activateVAP script for details on arguments.  Other configuration
## examples are as follows:
##

##
## Root AP for WDS
##

if [ "${AP_STARTMODE}" = "rootap" ]; then
    /etc/ath/makeVAP ap-wds $AP_SSID ${AP_IFNUM}
    if [ $? != 0 ]; then
        echo "Unable to create VAP!"
        exit
    fi
    /etc/ath/activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE
fi

## REPEATER

if [ "${AP_STARTMODE}" = "repeater" ]; then
    /etc/ath/makeVAP ap-wds  $AP_SSID ${AP_IFNUM}
    if [ $? != 0 ]; then
        echo "Unable to create VAP!"
        exit
    fi
    /etc/ath/makeVAP sta-wds $AP_SSID ${AP_IFNUM_2}

    if [ "${ROOTAP_MAC}" != "" ]; then
        iwconfig ath1 ap $ROOTAP_MAC
    fi

    /etc/ath/activateVAP ath1 br0 $AP_SECMODE_2 $AP_SECFILE_2
    /etc/ath/activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE
fi

##
## "VIRTUAL WIRE" client
##

if [ "${AP_STARTMODE}" = "client" ]; then
    /etc/ath/makeVAP sta-wds $AP_SSID ${AP_IFNUM}
    if [ $? != 0 ]; then
        echo "Unable to create VAP!"
        exit
    fi

    if [ "${ROOTAP_MAC}" != "" ]; then
        iwconfig ath0 ap $ROOTAP_MAC
    fi

    /etc/ath/activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE
fi

##
## mBSSID case with all types of authentication
## Note that WEP MUST be the first VAP
## This is brute force, but effective.  Note that we set the becon interval
## to 400
##

if [ "${AP_STARTMODE}" = "multi" ]; then
    if [ "${AP_SSID}" != "" ]; then
        /etc/ath/makeVAP $AP_MODE $AP_SSID ${AP_IFNUM} 400
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_2}" != "" ]; then
        /etc/ath/makeVAP $AP_MODE_2 $AP_SSID_2 ${AP_IFNUM_2}
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_3}" != "" ]; then
        /etc/ath/makeVAP $AP_MODE_3 $AP_SSID_3 ${AP_IFNUM_3}
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_4}" != "" ]; then
        /etc/ath/makeVAP $AP_MODE_4 $AP_SSID_4 ${AP_IFNUM_4}
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    #
    # Now, activate the VAPs
    #

    /etc/ath/activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE

    if [ "${AP_SSID_2}" != "" ]; then
        /etc/ath/activateVAP ath1 br0 $AP_SECMODE_2 $AP_SECFILE_2
    fi

    if [ "${AP_SSID_3}" != "" ]; then
        /etc/ath/activateVAP ath2 br0 $AP_SECMODE_3 $AP_SECFILE_3
    fi

    if [ "${AP_SSID_4}" != "" ]; then
        /etc/ath/activateVAP ath3 br0 $AP_SECMODE_4 $AP_SECFILE_4
    fi
fi



if [ "${AP_STARTMODE}" = "multivlan" ]; then
    if [ "${AP_SSID}" != "" ]; then
        /etc/ath/makeVAP $AP_MODE $AP_SSID ${AP_IFNUM} 400
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_2}" != "" ]; then
        /etc/ath/makeVAP $AP_MODE_2 $AP_SSID_2 ${AP_IFNUM_2}
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_3}" != "" ]; then
        /etc/ath/makeVAP $AP_MODE_3 $AP_SSID_3 ${AP_IFNUM_3}
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_4}" != "" ]; then
        /etc/ath/makeVAP $AP_MODE_4 $AP_SSID_4 ${AP_IFNUM_4}
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    #
    # Now, activate the VAPs
    #

    if [ "${AP_SSID}" != "" ] ; then
        /etc/ath/activateVAP ath0 br0 #$AP_SECMODE $AP_SECFILE
    fi
    if [ "${AP_SSID_2}" != "" ]; then
        /etc/ath/activateVAP ath1 br0 #$AP_SECMODE_2 $AP_SECFILE_2
    fi

    if [ "${AP_SSID_3}" != "" ]; then
        /etc/ath/activateVAP ath2 br0 #$AP_SECMODE_3 $AP_SECFILE_3
    fi

    if [ "${AP_SSID_4}" != "" ]; then
        /etc/ath/activateVAP ath3 br0 #$AP_SECMODE_4 $AP_SECFILE_4
    fi

    #configure VLANS and bridges
    brctl delif br0 eth0
    brctl delif br0 eth1
    ifconfig br0 0.0.0.0 up
    ifconfig eth0 $AP_IPADDR up

    #
    #vlan ids must be choosen. This is to provide better control on number of vaps need to be created. 
    #
    if [ "${AP_VLAN}" != "" ]; then
        sh /etc/ath/vlanvap ath0 ${AP_BRNAME:="br2"} ${AP_VLAN:="2"} ${AP_SECMODE:="NONE"} ${AP_SECFILE:="NONE"}
    fi
    
    if [ "${AP_VLAN_2}" != "" ]; then
        sh /etc/ath/vlanvap ath1 ${AP_BRNAME_2:="br3"} ${AP_VLAN_2:="3"} ${AP_SECMODE_2:="NONE"} ${AP_SECFILE_2:="NONE"}
    fi
    
    if [ "${AP_VLAN_3}" != "" ]; then
        sh /etc/ath/vlanvap ath2 ${AP_BRNAME_3:="br4"} ${AP_VLAN_3:="4"} ${AP_SECMODE_3:="NONE"} ${AP_SECFILE_3:="NONE"}
        
    fi
    
    if [ "${AP_VLAN_4}" != "" ]; then
       sh /etc/ath/vlanvap ath3 ${AP_BRNAME_4:="br5"} ${AP_VLAN_4:="5"} ${AP_SECMODE_4:="NONE"} ${AP_SECFILE_4:="NONE"}
    fi
    
fi

