#!/bin/sh
##
## "main" procedure
##

MODLIST=`lsmod | grep ath_hal | cut -f1,0 -d" "`

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

##
## Bring in the default environmental variables
##

. /etc/ath/apcfg

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

##
## For safety, delete all /tmp nodes we may re-create
##

rm -rf /tmp/br*
rm -rf /tmp/bc*
rm -rf /tmp/ap*
rm -rf /tmp/top*

##
## Determine the number of radios installed
##

NUMRADIO=`grep -c 168c /proc/pci`

##
## Make sure the number is 1 or 2.  Any other is invalid
##

if [ $NUMRADIO -gt 2 -o $NUMRADIO -lt 1 ]; then
    echo "INVALID CONFIGURATION, RADIO NOT INSTALLED"
    exit 255
fi

if [ "${AP_STARTMODE}" = "dual" ]; then
    if [ $NUMRADIO = 1 ]; then
        AP_STARTMODE=standard
    else
        AP_STARTMODE=multi
    fi
fi

##
## Now, process the modes
##

if [ "${AP_STARTMODE}" = "standard" ]; then
    makeVAP ap "$AP_SSID" $AP_RADIO_ID:$AP_RFPARAM
    if [ $? != 0 ]; then
        echo "Unable to create VAP!"
        exit
    fi
    activateVAP ath0:$AP_RADIO_ID 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_RADIO_ID:$AP_RFPARAM
    if [ $? != 0 ]; then
        echo "Unable to create VAP!"
        exit
    fi

    activateVAP ath0:$AP_RADIO_ID 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
##

WEP_INTERFACE=0
if [ "${AP_STARTMODE}" = "multi" ]; then
    if [ "${AP_SSID}" != "" ]; then
	    if [ "${AP_SECMODE}" = "WEP" ]; then
               WEP_INTERFACE="${AP_RADIO_ID}"
	    fi
        makeVAP $AP_MODE "$AP_SSID" $AP_RADIO_ID:$AP_RFPARAM 100
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_2}" != "" ]; then
	    if [ "${AP_SECMODE_2}" = "WEP" ]; then
            echo $WEP_INTERFACE | grep "${AP_RADIO_ID_2}" > /dev/null
		    if [ $? -eq 0 ]; then
			    echo "Unable to create additional WEP VAP"
				exit 255
			else
                WEP_INTERFACE="${WEP_INTERFACE}:${AP_RADIO_ID_2}"
			fi
		fi
        makeVAP $AP_MODE_2 "$AP_SSID_2" $AP_RADIO_ID_2:$AP_RFPARAM_2	200
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_3}" != "" ]; then
	    if [ "${AP_SECMODE_3}" = "WEP" ]; then
            echo $WEP_INTERFACE | grep "${AP_RADIO_ID_3}" > /dev/null
		    if [ $? -eq 0 ]; then
			    echo "Unable to create additional WEP VAP"
				exit 255
			else
                WEP_INTERFACE="${WEP_INTERFACE}:${AP_RADIO_ID_3}"
			fi
		fi
        makeVAP $AP_MODE_3 "$AP_SSID_3" $AP_RADIO_ID_3:$AP_RFPARAM_3 300
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    if [ "${AP_SSID_4}" != "" ]; then
	    if [ "${AP_SECMODE_4}" = "WEP" ]; then
            echo $WEP_INTERFACE | grep "${AP_RADIO_ID_4}" > /dev/null
		    if [ $? -eq 0 ]; then
			    echo "Unable to create additional WEP VAP"
				exit 255
			else
                WEP_INTERFACE="${WEP_INTERFACE}:${AP_RADIO_ID_4}"
			fi
		fi
        makeVAP $AP_MODE_4 "$AP_SSID_4" $AP_RADIO_ID_4:$AP_RFPARAM_4 400
        if [ $? != 0 ]; then
            echo "Unable to create VAP!"
            exit
        fi
    fi

    activateVAP ath0:$AP_RADIO_ID br0 $AP_SECMODE $AP_SECFILE 

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

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

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

IS_WPA=`set | grep WPA`
IS_WPS=`set | grep "WPS_ENABLE" | grep 1`

##
## Now, make the topology file
##

if [ "${IS_WPA}" != "" -o "${IS_WPS}" != "" ]; then
    echo "Making Topology File . . ."
    # for vlan case we should not be adding br0 
    echo -e "bridge br0" > /tmp/topology.conf
    echo -e "{" >> /tmp/topology.conf
    echo -e "\tipaddress ${AP_IPADDR}" >> /tmp/topology.conf
    echo -e "\tipmask ${AP_NETMASK}" >> /tmp/topology.conf
    cat /tmp/br0 >> /tmp/topology.conf
    echo -e "\tinterface eth0" >> /tmp/topology.conf
    echo -e "\tinterface eth1" >> /tmp/topology.conf
    echo -e "}" >> /tmp/topology.conf

    if [ -f /tmp/aplist0 ]; then
        echo "radio wifi0" >> /tmp/topology.conf
        echo "{" >> /tmp/topology.conf

        if [ -f /tmp/aplist0 ]; then
            echo -e "\tap" >> /tmp/topology.conf
            echo -e "\t{" >> /tmp/topology.conf
            cat /tmp/aplist0 >> /tmp/topology.conf
            echo -e "\t}" >> /tmp/topology.conf
        fi

        echo "}" >> /tmp/topology.conf
    fi

    if [ -f /tmp/aplist1 ]; then
        echo "radio wifi1" >> /tmp/topology.conf
        echo "{" >> /tmp/topology.conf
        
        if [ -f /tmp/aplist1 ]; then
            echo -e "\tap" >> /tmp/topology.conf
            echo -e "\t{" >> /tmp/topology.conf
            cat /tmp/aplist1 >> /tmp/topology.conf
            echo -e "\t}" >> /tmp/topology.conf
        fi
        
        echo "}" >> /tmp/topology.conf
    fi

    #
    # Start hostapd & wsc_supplicant.  Check for the
    # appropriate file lists to determine if they need
    # to be started . . .
    #
    # Note that /var/run is statically linked to /tmp . . .
    #

    if [ -f /tmp/aplist0 -o -f /tmp/aplist1 ]; then
        hostapd /var/run/topology.conf &
    fi

fi
