<%
local uci = require "luci.model.uci"
local fs = require "nixio.fs"

local nws = {}
uci.inst_state:foreach("network", "interface", function(s)
	nws[#nws+1] = s
end)

if uci.inst_state:get("network", "lan", "_ipchanged") and
uci.inst_state:revert("network", "lan", "_ipchanged") then
	include("niu/network/warn_ip_change")
end

local wanon = uci.inst_state:get("network", "wan", "proto")
local wanup = uci.inst_state:get("network", "wan", "up")
local wanip = uci.inst_state:get("network", "wan", "ipaddr")

local leasefn
uci.inst:foreach("dhcp", "dnsmasq",
 function(section)
 	leasefn = section.leasefile
 end
)

if leasefn then
	local ln = fs.access(leasefn, "r") and io.lines(leasefn)
	leasefn = 0
	while ln and ln() do
		leasefn = leasefn + 1
	end
end

local arps
if fs.access("/proc/net/arp", "r") then
	local ln = io.lines("/proc/net/arp")
	arps = -1
	while ln and ln() do
		arps = arps + 1
	end
	arps = arps >= 0 and arps
end
%>

<table class="dbstattbl">
<% if wanon and wanon ~= "none" then %>
<tr>
<% if wanup then %>
<th>Uplink Address:</th>
<td><%=wanip%></td>
<% else %>
<th>Uplink:</th>
<td><span style="color: darkred"><em>offline</em></span>
</td>
<% end %>

</tr>
<% end %>

<tr>
<th>Local Address:</th><td>
<%=uci.inst_state:get("network", "lan", "ipaddr")%>
</td>
</tr>

<% if arps then %>
<tr>
<th>Active IP-Devices:</th><td><%=arps%><% if leasefn then %>
(<%=leasefn%> assigned)
<% end %></td>
</tr>
<% end %>

</table>
<br />