#!/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)
##
#shell function to configure the vap for vlan
#arguments
#   $1 - $APNAME - name of the interface eg. ath0
#   $2 - $BRNAME - name of the bridge eg. br2
#   $3 - $VLANID - Id of the VLAN, eg 2
#   $4 - $SECMODE - Security mode like WPA
#   $5 - $SECFILE - like 8021x.conf
#call as
#   configure_vlanvap ath0 br2 2 WPA wpa2EAP.conf
configure_vlanvap() {
        VAPNAME=$1
        VBRNAME=$2
        VVLANID=$3
        VSECMODE=$4
        VSECFILE=$5
        #verify sec args 
        if [ ${VSECMODE} != "NONE" ]; then
            if [ ${VSECFILE} = "NONE" ]; then
                echo "No security file specified for $VSECMODE on $VAPNAME"
                exit 1
            fi
        fi
        #add tags on both eth0, eth1 and athx
        VESSID=`iwconfig ${VAPNAME} | grep ESSID | cut -f2 -d\"`
        brctl addbr $VBRNAME
        brctl delif br0 $VAPNAME
        vconfig add $VAPNAME $VVLANID
        vconfig add eth0 $VVLANID
        vconfig add eth1 $VVLANID
        brctl addif $VBRNAME $VAPNAME.$VVLANID
        brctl addif $VBRNAME eth0.$VVLANID
        brctl addif $VBRNAME eth1.$VVLANID
        ifconfig $VAPNAME.$VVLANID up
        ifconfig eth0.$VVLANID up
        ifconfig eth1.$VVLANID up
        ifconfig $VBRNAME up
        #change the configuration file for proper bridge= and interface= lines
        if [ "${VSECMODE}" != "NONE" ]; then
            #WPA
            set -x
            if [ "${VSECMODE}" = "WPA" ]; then
                sed -e 's/CHANGE_ME/'${VESSID}'/g' /etc/ath/${VSECFILE} | sed -e 's/ath0/'${VAPNAME}'/g' | sed -e 's/br0/'${VBRNAME}'/g' > /tmp/sec${VAPNAME}
                hostapd -B /tmp/sec${VAPNAME}
            fi

            #WEP
            if [ "${VSECMODE}" = "WEP" ]; then
               if [ "${VAPNAME}" != "ath0" ]; then
                  echo "WEP not allowed on VAPs other than ath0"
                  echo "Configuration Denied"
                  exit 1
               else
                  sed -e 's/APNAME/VAPNAME/g' /etc/ath/WEP.conf > /tmp/sec${VAPNAME}
                  . /tmp/sec${VAPNAME}
               fi
            fi
        fi
} 
#end configure_vlanvap
MODLIST=`lsmod | grep ath_hal | cut -f1,0 -d" "`

if [ "${MODLIST}" = "ath_hal" ]; then
    echo "AP is already up"
    exit
fi
. /etc/ath/apcfg

WAN_IF=${WAN_IF:=eth0}
LAN_IF=${LAN_IF:=eth1}

if [ "${AP_STARTMODE}" = "standard" ]; then
    makeVAP ap $AP_SSID ${AP_IFNUM}
    if [ $? != 0 ]; then
        echo "Unable to create VAP!"
        exit
    fi
    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
    makeVAP ap-wds $AP_SSID ${AP_IFNUM}
    if [ $? != 0 ]; then
        echo "Unable to create VAP!"
        exit
    fi
    activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE
fi

## REPEATER

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

    ROOTAP_SSID=${ROOTAP_SSID:=$AP_SSID}

    if [ "${ROOTAP_SSID}" = "any" -a  "${ROOTAP_MAC}" = "" ]; then
        echo "ROOTAP_MAC should be set if ROOTAP_SSID=any"
        exit 1
    else
        makeVAP sta-wds $ROOTAP_SSID ${AP_IFNUM_2}
    fi

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

    activateVAP ath1 br0 $AP_SECMODE_2 $AP_SECFILE_2
    activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE
fi

##
## "VIRTUAL WIRE" client
##

if [ "${AP_STARTMODE}" = "client" ]; then
    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

    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
        makeVAP $AP_MODE $AP_SSID ${AP_IFNUM} 400
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_2}" != "" ]; then
        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
        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
        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
    #

    activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE

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

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

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



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

    if [ "${AP_SSID_2}" != "" ]; then
        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
        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
        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
        activateVAP ath0 br0 #$AP_SECMODE $AP_SECFILE
    fi
    if [ "${AP_SSID_2}" != "" ]; then
        activateVAP ath1 br0 #$AP_SECMODE_2 $AP_SECFILE_2
    fi

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

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

    #configure VLANS and bridges
    brctl delif br0 ${WAN_IF}
    brctl delif br0 ${LAN_IF}
    ifconfig br0 0.0.0.0 up
    if [ "${AP_AUTHIF}" = "WAN" ]; then
        ifconfig ${WAN_IF} $AP_IPADDR up
    else
        ifconfig ${LAN_IF} $AP_IPADDR up
    fi

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

