<%#
Opennet Firmware

Copyright 2010 Rene Ejury <opennet@absorb.it>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

-%>
<%+header%>
<h2><a id="content" name="content"><%:Opennet ID%></a></h2>
<%+cbi/apply_xhr%>

<%
local uci = require "luci.model.uci"
local cursor = uci.cursor()
local form_id = luci.http.formvalue("form_id")
local on_id = cursor:get("on-core", "settings", "on_id")
local devices = { "on_eth_", "on_wifi_" }


if (form_id) then
	function split(str)
		local t = { }
		str:gsub("%d+", function (w) table.insert(t, w) end)
		return t
	end
	local t = split(form_id)
	
	local replace_id = false
	if #t == 1 then
		t[2] = t[1]
		t[1] = "1"
		replace_id = true
	elseif #t == 2 then
		replace_id = true
	elseif form_id:match("^(%d+)%.(%d+)%.(%d+)%.(%d+)$") then
		t[1] = t[3]
		t[2] = t[4]
		replace_id = true
	end

	if replace_id then
		on_function("set_opennet_id", {form_id})
	else
		form_id = on_id
	end
else
	form_id = on_id
end
%>

<h3><%:Opennet-ID / Network ID%></h3>
	<div class="cbi-map">
		<fieldset class="cbi-section">
			<form method="post" action="<%=REQUEST_URI%>" enctype="multipart/form-data">
				<p><%=luci.i18n.translatef('Great that you decided to take part in Opennet. Register the IP of your Access-Point in the %sOpennet-Wiki%s and aquire a free IP-Address. Please read the information %sin the wiki%s for further details.', '<a href="https://wiki.opennet-initiative.de/wiki/Opennet_Nodes">', '</a>', '<a href="https://wiki.opennet-initiative.de/wiki/Mitmachen">', '</a>')%></p>
			<p><%:Please enter your Opennet-ID (1.X or 2.X etc.) or the IP-Address of your first Opennet-Network-Interface, any change will reconfigure your network settings immediately.%></p>
			<fieldset class="cbi-section-node">
				<div class="cbi-value">
					<label class="cbi-value-title">
						<input type="value" name="form_id" value="<%=form_id%>" />
					</label>
					<div class="cbi-value-field">
						<input type="submit" class="cbi-button cbi-button-apply" value="<%:use%>"
						       onclick="return confirm('<%:Be aware that the network will be restarted immediately,\n and you might loose the contact to your router.\n\nAre you really wan\'t to update the network-id?%>')" />
					</div>
				</div>
			</fieldset>
			</form>
		</fieldset>
	</div>

<h3><%:related network addresses%></h3>
	<div class="cbi-map">
		<fieldset class="cbi-section">
			<fieldset class="cbi-section-node">
				<table class="cbi-section-table">
				<%
				for index = 1, #devices do
					local count = 0
					while (cursor:get_all("network", devices[index]..count)) do
						local v = cursor:get_all("network", devices[index]..count)
						%>
						<tr id="cbi-network-lan" class="cbi-section-table-row">
							<th><h3><%=v[".name"]%> (<%:device%> <%=v.ifname%>)</h3></th>
							<td class="cbi-value-field"><div id="cbi-network-lan-ipaddr"><%=v.ipaddr%> / <%=v.netmask%></div></td>
						</tr>
						<%
						count = count + 1
					end
				end
				local free = cursor:get_all("network", "free")
				if free then
						%>
						<tr id="cbi-network-lan" class="cbi-section-table-row">
							<th><h3><%=free[".name"]%> (<%:device%> <%=free.ifname%>)</h3></th>
							<td class="cbi-value-field"><div id="cbi-network-lan-ipaddr"><%=free.ipaddr%> / <%=free.netmask%></div></td>
						</tr>
						<%
				end				
				%>
				</table>
			</fieldset>
		</fieldset>
	</div>
<%+footer%>
