#!/bin/sh /etc/rc.common

. /lib/functions.sh

START=90

log() {
	wifilog "Hotspot Initialize" "$@"
}

ifname1="ifname"
if [ -e /etc/newstyle ]; then
	ifname1="device"
fi

do_zone() {
	local config=$1
	local name
	local network

	config_get name $1 name
	config_get network $1 network
	newnet="$network"
	if [ $name = wan ]; then
		WWAN=$(echo "$network" | grep "wwan2")
		if [ -z "$WWAN" ]; then
			newnet="$newnet wwan2 wwan5"
			uci_set firewall "$config" network "$newnet"
			uci_commit firewall
			/etc/init.d/firewall restart
		fi
	fi
}

do_radio() {
	local config=$1
	local channel
	local hwmode

	config_get channel $1 channel
	config_get hwmode $1 hwmode
	config_get band $1 band
	fr="1"
	if [ -z "$hwmode" ]; then
		if [ "$band" = "2g" ]; then
			fr="0"
		fi
	else
		hw=$(echo "$hwmode" | grep "g")
		if [ ! -z "$hw" ]; then
			fr="0"
		fi
	fi
	if [ "$fr" = "0" ]; then
			uci set wireless.wwan2=wifi-iface
			uci set wireless.wwan2.device=$config
			uci set wireless.wwan2.network="wwan2"
			uci set wireless.wwan2.mode="sta"
			uci set wireless.wwan2.ssid="Hotspot Manager Interface"
			uci set wireless.wwan2.encryption="none"
			uci set wireless.wwan2.disabled="1"
			uci commit wireless
	else
			uci set wireless.wwan5=wifi-iface
			uci set wireless.wwan5.device=$config
			uci set wireless.wwan5.network="wwan5"
			uci set wireless.wwan5.mode="sta"
			uci set wireless.wwan5.ssid="Hotspot Manager Interface"
			uci set wireless.wwan5.encryption="none"
			uci set wireless.wwan5.disabled="1"
			uci commit wireless
	fi
}

start()
{
log "Hotspot Firewall"
	if [ -e /usr/lib/custom/wififix ]; then
		/usr/lib/custom/wififix
	fi
	PRO=$(uci get network.wwan2.proto)
	if [ -z $PRO ]; then
		uci set network.wwan2=interface
		uci set network.wwan2.proto=dhcp 
		uci set network.wwan2.metric='2'
		uci set network.wwan26=interface
		uci set network.wwan26.proto=dhcpv6 
		uci set network.wwan26.metric='2'
		uci set network.wwan26.${ifname1}='@wwan2'
		uci set network.wwan26.reqaddress='try'
		uci set network.wwan26.reqprefix='auto'
	fi
	PRO=$(uci get network.wwan5.proto)
	if [ -z $PRO ]; then
		uci set network.wwan5=interface
		uci set network.wwan5.proto=dhcp 
		uci set network.wwan5.metric='2'
		uci set network.wwan56=interface
		uci set network.wwan56.proto=dhcpv6 
		uci set network.wwan56.metric='2'
		uci set network.wwan56.${ifname1}='@wwan5'
		uci set network.wwan56.reqaddress='try'
		uci set network.wwan56.reqprefix='auto'
		uci commit network
	fi
	config_load firewall
	config_foreach do_zone zone
	config_load wireless
	config_foreach do_radio wifi-device
	uci set travelmate.global.lost="0"
	uci set travelmate.global.delay="30"
	uci set travelmate.global.state='0'
	uci commit travelmate
	wifi up
}


