#!/usr/bin/lua

local platform = require 'gluon.platform'
local sysconfig = require 'gluon.sysconfig'

local json = require 'jsonc'
local uci = require('simple-uci').cursor()
local unistd = require 'posix.unistd'

local board_data = json.load('/etc/board.json')
local network_data = (board_data or {}).network

local function iface_exists(ifaces)
	if not ifaces then return nil end
	local ifnames = table.concat(ifaces, ' ')

	for _, iface in ipairs(ifaces) do
		if unistd.access('/sys/class/net/' .. iface:gsub('%..*$', '')) then
			return ifnames
		end
	end
end

local lan_data = network_data.lan or {}
local wan_data = network_data.wan or {}

local lan_interfaces = lan_data.ports
local wan_interfaces = wan_data.ports

-- In case we have a single port for either WAN or LAN,
-- add it to the respective empty table, as devices and
-- ports is XOR in board.json

if lan_data.device ~= nil then
	lan_interfaces = {lan_data.device}
end

if wan_data.device ~= nil then
	wan_interfaces = {wan_data.device}
end

local lan_ifname = iface_exists(lan_interfaces)
local wan_ifname = iface_exists(wan_interfaces)

if platform.match('ath79', 'generic', {
	'dlink,dap-2695-a1',
	'tplink,cpe210-v1',
	'tplink,cpe210-v2',
	'tplink,cpe510-v1',
	'tplink,wbs210-v1',
	'tplink,wbs210-v2',
	'tplink,wbs510-v1',
	'ubnt,unifi-ap-pro',
}) then
	lan_ifname, wan_ifname = wan_ifname, lan_ifname
elseif platform.match('ath79', 'generic', {
	'ubnt,unifi-ap-outdoor-plus',
}) then
	-- Temporary solution to separate interfaces in bridged default setup
	lan_ifname, wan_ifname = 'eth0', 'eth1'
elseif platform.match('ath79', 'generic', {
	'ubnt,unifiac-mesh-pro',
	'ubnt,unifiac-pro',
}) then
	lan_ifname, wan_ifname = 'eth0.2', 'eth0.1'
elseif platform.match('ipq40xx', 'generic', {
	'avm,fritzbox-7530',
}) then
	lan_ifname, wan_ifname = 'lan2 lan3 lan4', 'lan1'
elseif platform.match('ipq806x', 'generic', {
	'ubnt,unifi-ac-hd',
}) then
	lan_ifname, wan_ifname = 'eth1', 'eth0'
elseif platform.match('ramips', 'mt7621', {
	'netgear,wac104',
}) then
	lan_ifname, wan_ifname = 'lan2 lan3 lan4', 'lan1'
elseif platform.match('ramips', 'mt7621', {
	'tplink,eap615-wall-v1',
}) then
	lan_ifname, wan_ifname = 'lan1 lan2 lan3', 'lan0'
elseif platform.match('lantiq', 'xrx200', {
	'arcadyan,vgv7510kw22-nor',
}) then
	lan_ifname, wan_ifname = 'lan1 lan2 lan3 lan4', 'wan'
elseif platform.match('realtek', 'rtl838x', {
	'd-link,dgs-1210-10p',
}) then
	lan_ifname, wan_ifname = 'lan2 lan3 lan4 lan5 lan6 lan7 lan8 lan9 lan10', 'lan1'
end

if wan_ifname and lan_ifname then
	sysconfig.wan_ifname = wan_ifname
	sysconfig.lan_ifname = lan_ifname
	sysconfig.single_ifname = nil
else
	sysconfig.wan_ifname = nil
	sysconfig.lan_ifname = nil
	sysconfig.single_ifname = lan_ifname or wan_ifname
end

-- Delete all UCI device sections of type 'bridge'
-- as well as the ones starting with 'br-'.
-- Preserve all others to apply MAC address stored in UCI
uci:delete_all('network', 'device', function(dev)
	return (dev.type == 'bridge' or dev.name:match('^br-'))
end)

uci:delete_all('network', 'interface')

uci:save('network')
