#!/usr/bin/lua

local uci = require('simple-uci').cursor()

-- Migration from hwmode to band (OpenWrt 21.02)
-- Use uci:foreach(), as wireless.foreach_radio() depends on band already being set
uci:foreach('wireless', 'wifi-device', function(radio)
	local radio_name = radio['.name']
	local hwmode = radio.hwmode
	if not radio.band then
		if hwmode == '11g' or hwmode == '11ng' then
			uci:set('wireless', radio_name, 'band', '2g')
		elseif hwmode == '11a' or hwmode == '11na' then
			uci:set('wireless', radio_name, 'band', '5g')
		end
	end
	uci:delete('wireless', radio_name, 'hwmode')
end)

uci:save('wireless')
