<%#
 Copyright 2008 Steven Barth <steven@midlink.org>
 Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
 Copyright 2011 Manuel Munz <freifunk at somakoma dot de>
 Licensed to the public under the Apache License 2.0.
-%>

<%

has_ipv4_conf = luci.model.uci.cursor():get_first("olsrd", "olsrd", "IpVersion")
has_ipv6_conf = luci.model.uci.cursor():get_first("olsrd6", "olsrd", "IpVersion")

function write_conf(conf, file)
	local fs = require "nixio.fs"
	if fs.access(conf) then
		luci.http.header("Content-Disposition", "attachment; filename="..file)
		luci.http.prepare_content("text/plain")
		luci.http.write(fs.readfile(conf))
	end
end

conf = luci.http.formvalue()

if conf.openwrt_v4 then
	write_conf("/etc/config/olsrd", "olsrd")
	return false
end

if conf.openwrt_v6 then
	write_conf("/etc/config/olsrd6", "olsrd6")
	return false
end

if conf.conf_v4 then
	write_conf("/var/etc/olsrd.conf", "olsrd.conf")
	return false
end

if conf.conf_v6 then
	write_conf("/var/etc/olsrd6.conf", "olsrd6.conf")
	return false
end

%>

<%+header%>

<script type="text/javascript" src="<%=resource%>/cbi.js"></script>
<script type="text/javascript">//<![CDATA[

XHR.poll(10, '<%=REQUEST_URI%>/json', { },
                function(x, info)
                {
		var e;

		if (! info) {
			document.getElementById('error').innerHTML = '<%:Could not get any data. Make sure the jsoninfo plugin is installed and allows connections from localhost.%>';
			return
		}
		document.getElementById('error').innerHTML = '';

		if (e = document.getElementById('version'))
			var version;
			var date;
			if (info.v4.config.olsrdVersion != undefined) {
				version = info.v4.config.olsrdVersion
				date = info.v4.config.olsrdBuildDate
			} else if (info.v6.config.olsrdVersion != undefined) {
				version = info.v6.config.olsrdVersion
				date = info.v6.config.olsrdBuildDate
			} else {
				version = 'unknown'
				date = 'unknown'
			}		
			e.innerHTML = version + '<br />' + date;

		if (e = document.getElementById('nr_neigh'))
			var neigh = 0;
			if (info.v4.links != undefined) {
				neigh = neigh + info.v4.links.length
			}
			if (info.v6.links != undefined) {
				neigh = neigh + info.v6.links.length
			}
			e.innerHTML = neigh;


		if (e = document.getElementById('nr_hna'))
			var hna = 0;
			if (info.v4.hna != undefined) {
				hna = hna + info.v4.hna.length
			}
			if (info.v6.hna != undefined) {
				hna = hna + info.v6.hna.length
			}
			e.innerHTML = hna;


		if (e = document.getElementById('nr_ifaces'))
			var nrint = 0
			if (info.v4.interfaces != undefined) {
				nrint = nrint + info.v4.interfaces.length
			}
			if (info.v6.interfaces != undefined) {
				nrint = nrint + info.v6.interfaces.length
			}
			e.innerHTML = nrint


		if (e = document.getElementById('nr_topo'))
			var topo = 0;
			var nodes = [];

			Array.prototype.contains = function (element) {
				for (var i = 0; i < this.length; i++) {
					if (this[i] == element) {
						return true;
					}
				}
				return false;
			}

			if (info.v4.topology != undefined) {
				topo = topo + info.v4.topology.length;
				for (var i = 0; i < info.v4.topology.length; i++) {
					var destip = info.v4.topology[i].destinationIP
					if (! nodes.contains(destip) ) {
						nodes.push(destip)
					}
				}
			}

			if (info.v6.topology != undefined) {
				topo = topo + info.v6.topology.length
				for (var i = 0; i < info.v6.topology.length; i++) {
					var destip = info.v6.topology[i].destinationIP
					if (! nodes.contains(destip) ) {
						nodes.push(destip)
					}
				}

			}
			e.innerHTML = topo;

			if (e = document.getElementById('nr_nodes'))
				e.innerHTML = nodes.length;

			if (e = document.getElementById('meshfactor'))
				var meshfactor = topo / nodes.length
				e.innerHTML = meshfactor.toFixed(2)
		}
	);
//]]></script>


<div id="error" class="error"></div>

<h2 name="content">OLSR <%:Overview%></h2>

<fieldset class="cbi-section">
        <legend><%:Network%></legend>

        <table width="100%" cellspacing="10">
                <tr><td width="33%"><%:Interfaces%></td><td>
			<a href="<%=REQUEST_URI%>/interfaces">
				<span id="nr_ifaces">-<span>
			</a>
		</td></tr>
                <tr><td width="33%"><%:Neighbors%></td><td>
			<a href="<%=REQUEST_URI%>/neighbors">
				<span id="nr_neigh">-</span>
			</a>
		</td></tr>
                <tr><td width="33%"><%:Nodes%></td><td>
			<a href="<%=REQUEST_URI%>/topology">
				<span id="nr_nodes">-</span>
			</a>
		</td></tr>
                <tr><td width="33%"><%:HNA%></td><td>
			<a href="<%=REQUEST_URI%>/hna">
				<span id="nr_hna">-</span>
			</a>
		</td></tr>
                <tr><td width="33%"><%:Links total%></td><td>
			<a href="<%=REQUEST_URI%>/topology">
				<span id="nr_topo">-</span>
			</a>
		</td></tr>
                <tr><td width="33%"><%:Links per node (average)%></td><td>
			<span id="meshfactor">-</span>
		</td></tr>


        </table>
</fieldset>


<fieldset class="cbi-section">
        <legend>OLSR <%:Configuration%></legend>
        <table width="100%" cellspacing="10">
                <tr><td width="33%"><%:Version%></td><td>
			<span id="version">-<span>
		</td></tr>
                <tr><td width="33%"><%:Download Config%></td><td>
			<% if has_ipv4_conf then %>
			    <a href="<%=REQUEST_URI%>?openwrt_v4">OpenWrt (IPv4)</a>,
			<% end %>
			<% if has_ipv6_conf then %>
			    <a href="<%=REQUEST_URI%>?openwrt_v6">OpenWrt (IPv6)</a>,
			<% end %>
			<% if has_ipv4_conf then %>
			    <a href="<%=REQUEST_URI%>?conf_v4">OLSRD (IPv4)</a>,
			<% end %>
			<% if has_ipv6_conf then %>
			    <a href="<%=REQUEST_URI%>?conf_v6">OLSRD (IPv6)</a>
			<% end %>
		</td></tr>
	</table>
</fieldset>

<%+footer%>