#!/bin/sh
# 29jan2011: oops. fixed WEP! *need* KeyNType for each KeyNStr. KeyNStr was also
#	incorrectly just KeyN

append DRIVERS "rt61ap"
confout=/tmp/rt61ap.dat
confold=/etc/wireless/rt61ap.dat

set_option() {
	local dev="$1"
	local key="$2"
	local val="$3"

	echo "$dev: $key=$val"
	if [ "$key" == "Default" ]; then
		touch $confout
		[ "$confold" -ef "$confout" ] || {
			echo Setting $confold to $confout
			rm $confold
			ln -s $confout $confold
		}
		echo "Default" >$confout
	else
		echo "$key=$val" >>$confout
	fi
#	iwpriv $dev set $key="$val"
}

scan_rt61ap() {
	local device="$1"
	local mainvif
	local wds

	config_get vifs "$device" vifs
	local _c=0
	for vif in $vifs; do
		config_get mode "$vif" mode
		case "$mode" in
			ap)
				# Only one vif is allowed on AP, station, Ad-hoc or monitor mode
				[ -z "$mainvif" ] && {
					mainvif="$vif"
					config_set "$vif" ifname "$device"
				}
			;;
			*) echo "$device($vif): Invalid mode, ignored."; continue;;
		esac
	done
	config_set "$device" vifs "${mainvif:+$mainvif }${wds:+$wds}"
}

disable_rt61ap() (
	local device="$1"

	set_wifi_down "$device"

	include /lib/network
	unbridge "$device"
	ifconfig $device down
	return 0
)

enable_rt61ap() {
	local device="$1"

	# needed at start of config
	set_option "$device" Default 1

	config_get channel "$device" channel
	[ -n "$channel" ] && set_option "$device" Channel "$channel"

	config_get txpower "$device" txpower
	[ -n "$txpower" ] && set_option "$device" TxPower "${txpower%%.*}"

	config_get vifs "$device" vifs
	local first=1
	for vif in $vifs; do
		config_get ifname "$vif" ifname

		config_get ssid "$vif" ssid

		config_get mode "$vif" mode

		case "$mode" in
			ap|AP) ;;
			*)	echo "Only AP mode supported with AP driver!"
				return
		esac

		[ "$first" = 1 ] && {
			set_option "$device" SSID "$ssid"

			config_get rate "$vif" rate
			[ -n "$rate" ] && set_option "$device" TxRate "${rate%%.*}"

			config_get_bool hidden "$vif" hidden 0
			set_option "$device" HideSSID "$hidden"

#			config_get frag "$vif" frag
#			[ -n "$frag" ] && iwconfig "$device" frag "${frag%%.*}"

#			config_get rts "$vif" rts
#			[ -n "$rts" ] && iwconfig "$device" rts "${rts%%.*}"

#			config_get maclist "$vif" maclist
#			[ -n "$maclist" ] && {
#				# flush MAC list
#				iwpriv "$device" maccmd 3
#				for mac in $maclist; do
#					iwpriv "$device" addmac "$mac"
#				done
#			}
#			config_get macpolicy "$vif" macpolicy
#			case "$macpolicy" in
#				allow)
#					iwpriv $device maccmd 2
#				;;
#				deny)
#					iwpriv $device maccmd 1
#				;;
#				*)
#					# default deny policy if mac list exists
#					[ -n "$maclist" ] && iwpriv $device maccmd 1
#				;;
#			esac
#			# kick all stations if we have policy explicitly set
#			[ -n "$macpolicy" ] && iwpriv $device maccmd 4
		}
#
		config_get enc "$vif" encryption
		case "$enc" in
			WEP|wep)
				for idx in 1 2 3 4; do
					config_get key "$vif" "key${idx}"
					[ -n "$key" ] && set_option "$ifname" Key${idx}Type 0 && set_option "$ifname" Key${idx}Str "${key}"
				done
				config_get key "$vif" key
				key="${key:-1}"
				case "$key" in
					[1234]) set_option "$ifname" DefaultKeyID "$key";;
#					*) iwconfig "$ifname" enc "$key";;
				esac
				set_option "$ifname" AuthMode OPEN
				set_option "$ifname" EncrypType WEP
			;;
			psk2|wpa2)
				config_get key "$vif" key
				set_option "$ifname" AuthMode WPA2PSK
				set_option "$ifname" EncrypType TKIP
				set_option "$ifname" WPAPSK $key
			;;
			psk*|wpa*)
				config_get key "$vif" key
				set_option "$ifname" AuthMode WPAPSK
				set_option "$ifname" EncrypType TKIP
				set_option "$ifname" WPAPSK $key
			;;
		esac

		local net_cfg bridge
		net_cfg="$(find_net_config "$vif")"
		[ -z "$net_cfg" ] || {
			bridge="$(bridge_interface "$net_cfg")"
			config_set "$vif" bridge "$bridge"
			start_net "$ifname" "$net_cfg"
		}
		set_wifi_up "$vif" "$ifname"

		first=0
	done
}

detect_rt61ap() {
	local dev="ra0"

	config_get type "$dev" type
	[ "$type" = rt61ap ] && continue
	cat <<EOF
config wifi-device $dev
	option type	rt61ap
	option channel  6

	# REMOVE THIS LINE TO ENABLE WIFI:
	option disabled 1

config wifi-iface
	option device	$dev
	option network	lan
	option mode	ap
	option ssid	OpenWrt
	option encryption none
EOF
}
