#!/bin/sh

isSsidEnabled() {
	local ssid="$1"
	eval local enabled="\$SSID${ssid}_ENABLED"
	[ "$enabled" == 1 ] && echo 1
}

isSsidInBridgeMode() {
	local ssid="$1"
	eval local mode="\$SSID${ssid}_MODE"
	[ "$mode" == "B" ] && echo 1
}

getSsidInf() {
	echo "br-wifi${1}"
}

getSsidWirelessInf() {
	for radio in `seq 1 $(getRadioCount)` ; do
		infName="wlan${ssid}${radio} $infName"
	done
	echo "$infName"
}

getEnabledSsidList() {
	local list=""
  for instance in `seq 1 "$NO_SSID"` ; do
    eval local enabled="\$SSID${instance}_ENABLED"
    [ "$enabled" == 1 ] && list="$list $instance";
  done

  echo $list
}

getRadioCount() {
	echo "$WIFI_DEVS" | tr ' ' '\n' | wc -l
}

_getRadio() {
	echo "$WIFI_DEVS" | tr ' ' '\n' | head -n $1 | tail -n 1
}

getRadioName() {
	[ ! -z "$1" ] && echo `_getRadio $1` | awk -F',' '{print $1}'
}

getRadioMode() {
	[ ! -z "$1" ] && echo `_getRadio $1` | awk -F',' '{print $2}'
}

getWifiSpeed() {
	channel=`iw $1 info | grep channel | awk '{print $2}'`
	[ $channel -gt 12 ] && echo 300 || echo 100
}

goOffline() {
	ssids=`getEnabledSsidList`
	radios=`getRadioCount`
	for ssid in $ssids ; do
		for radio in `seq 1 $radios` ; do
			mode=`getRadioMode $radio`
			[ ! -z "$mode" ] && suffix="_${mode}" || suffix=""
			uci set "wireless.ssid${ssid}${suffix}.disabled=1"
		done
	done
}