#!/usr/bin/lua

local site = require 'gluon.site'
local wireless = require 'gluon.wireless'
local hash = require 'hash'
local uci = require('simple-uci').cursor()


local function configure_sae(vif)
	uci:set('wireless', vif, 'encryption', 'sae')
	uci:set('wireless', vif, 'key', site.wifi.mesh.sae_passphrase() or hash.md5(site.prefix6()))
end

wireless.foreach_radio(uci, function(radio)
	local radio_name = radio['.name']
	local vif = 'mesh_' .. radio_name
	local enable = site.wifi.mesh.sae(false)

	if uci:get('wireless', vif) then
		uci:delete('wireless', vif, 'encryption')
		uci:delete('wireless', vif, 'key')

		if enable then
			configure_sae(vif)
		end
	end
end)

uci:save('wireless')
