#!/usr/bin/lua

local client_bridge = require 'gluon.client_bridge'
local site = require 'gluon.site'
local sysconfig = require 'gluon.sysconfig'

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


local next_node = site.next_node({})


uci:section('network', 'device', 'local_node_dev', {
	type = 'veth',
	name = 'local-node',
	macaddr = client_bridge.next_node_macaddr(),
	peer_name = 'local-port',
	peer_macaddr = sysconfig.primary_mac,
})


local ip4, ip6

if next_node.ip4 then
	local plen = site.prefix4():match('/%d+$')
	ip4 = next_node.ip4 .. plen
end

if next_node.ip6 then
	ip6 = next_node.ip6 .. '/128'
end

uci:section('network', 'interface', 'local_node', {
	ifname = 'local-node',
	proto = 'static',
	ipaddr = ip4,
	ip6addr = ip6,
	ip6deprecated = true,
})

uci:save('network')


uci:delete('dhcp', 'local_node')
uci:section('dhcp', 'dhcp', 'local_node', {
	interface = 'local_node',
	ignore = true,
})

uci:save('dhcp')
