#!/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)
##################################################################################

##################################################################################
## configure_interface
##
## This routine will take the selected channel and mode for the device, and return
## an appropriate IF_NUM string to configure the device.  This allows for "auto"
## detection of the installed board type, and proper configuration for the
## selected board
##
##   $1 - Slot number of the board being configured (0 or 1)
##   $2 - Selected Frequency Range
##   $3 - Selected operating mode (e.g. 11NG, 11A, etc)
##

configure_interface()
{
	##
	## Get the device ID string
	##

	if [ $1 = 0 ]; then
		DEVID=`grep -n 168c /proc/pci | grep "3:" | cut -b 35-38`
	else
		DEVID=`grep -n 168c /proc/pci | grep -v "3:" | cut -b 35-38`
	fi

	##
	## Switch on the device type
	##

	case $DEVID in
		"001d")	## Nala
			echo "Nala Found"
			# Construct the string
			IFNUMSTR=`echo $1:RF:$2:11G`;;

		"0023")	## Owl MB72/MB71
			echo "Owl Found"
			# Simply construct the string based on the input parameters
			IFNUMSTR=`echo $1:RF:$2:$3`;;

		"0027")	## Sowl MB82/MB81
			echo "Sowl Found"
			# Simply construct the string based on the input parameters
			IFNUMSTR=`echo $1:RF:$2:$3`;;

		"0029")	## Merlin MB91/MB92
			echo "Merlin PCI Found"
			# Simply construct the string based on the input parameters
			IFNUMSTR=`echo $1:RF:$2:$3`;;

		*)	## Unsupported
			echo "UNSUPPORTED CONFIGURATION"
			exit 255;;

	esac

	echo "STRING: $IFNUMSTR"
} 

##################################################################################
## configure_vlanvap
##
## 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

#####################################################################################
##
## "main" procedure
##

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

if [ "${MODLIST}" = "ath_hal" ]; then
    echo "AP is already up"
    echo "Wireless Modules (ath_hal) is already loaded"
    exit
fi

echo "NickChou: run apup start"

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

. /etc/ath/ath_apcfg

##
## 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
		configure_interface 0 $AP_PRIMARY_CH $AP_CHMODE
		AP_IFNUM=$IFNUMSTR
	else
		AP_STARTMODE=multi
		configure_interface 0 $AP_PRIMARY_CH $AP_CHMODE
		AP_IFNUM=$IFNUMSTR
		configure_interface 1 $AP_PRIMARY_CH_2 $AP_CHMODE_2
		AP_IFNUM_2=$IFNUMSTR
	fi
fi

##
## Now, process the modes
##

echo "NickChou: run apup: AP_STARTMODE=${AP_STARTMODE}"

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

## REPEATER

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

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

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

##
## "VIRTUAL WIRE" client
##

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

    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
        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
        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
        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
        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
    #

    ath_activateVAP ath0 br0 $AP_SECMODE $AP_SECFILE

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

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

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



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

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

    if [ "${AP_SSID_4}" != "" ]; then
        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
    if [ "${AP_AUTHIF}" = "WAN" ]; then 
        ifconfig eth0 $AP_IPADDR up
    else
        ifconfig eth1 $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

