/lib/gluon/upgrade/200-wireless
---
#!/usr/bin/lua

local util = require 'gluon.util'
local uci = require('luci.model.uci').cursor()
local site = require 'gluon.site_config'
local sysconfig = require 'gluon.sysconfig'

-- Initial
if not sysconfig.gluon_version then
uci:delete_all('wireless', 'wifi-iface')
end

local function get_channel(radio, config)
if uci:get_bool('wireless', radio, 'preserve_channel') then
return uci:get('wireless', radio, 'channel')
else
return config.channel
end
end

local function is_channel_preserved(radio)
if uci:get_bool('wireless', radio, 'preserve_channel') then
return uci:get('wireless', radio, 'preserve_channel')
else
return '0'
end
end

local function configure_radio(radio, index, config)
if config then
local channel = get_channel(radio, config)
local preserve_channel = is_channel_preserved(radio)

uci:delete('wireless', radio, 'disabled')

uci:set('wireless', radio, 'channel', channel)
uci:set('wireless', radio, 'preserve_channel', preserve_channel)
uci:set('wireless', radio, 'htmode', 'HT20')
uci:set('wireless', radio, 'country', site.regdom)
end
end

util.iterate_radios(configure_radio)

uci:save('wireless')
uci:commit('wireless')
---