#!/usr/bin/lua

local site = require 'gluon.site'
local util = require 'gluon.util'

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


local enabled

-- Delete old broker config section (remove in 2019)
if not uci:get('tunneldigger', 'mesh_vpn') then
	if uci:get_first('tunneldigger', 'broker', 'interface') == 'mesh-vpn' then
		enabled = uci:get_first('tunneldigger', 'broker', 'enabled')
	end

	-- In the usual case (no migration from old tunneldigger package), the
	-- enabled state is set in the 500-mesh-vpn script

	uci:delete_all('tunneldigger', 'broker')
end

uci:section('tunneldigger', 'broker', 'mesh_vpn', {
	enabled = enabled,
	uuid = util.node_id(),
	interface = 'mesh-vpn',
	bind_interface = 'br-wan',
	group = 'gluon-mesh-vpn',
	broker_selection = 'usage',
	address = site.mesh_vpn.tunneldigger.brokers(),
})

uci:save('tunneldigger')
