#!/usr/bin/haserl
<% echo -en "content-type: text/html\r\n\r\n" %>
<!DOCTYPE html>
<html lang="de">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<%
name="$(uci -q get freifunk.@settings[0].name)"
echo -n "<title>Freifunk${name:+ - $name}</title>"
%>
<link rel="stylesheet" href="/index.css">
</head>
<body>
<noscript>
	<strong>JavaScript required</strong>
</noscript>
<div class="header">
	<div class="menu">
<%
url=$(uci -q get freifunk.@settings[0].community_url)
community="$(echo -n $url | cut -d\/ -f3)"
if [ -n "$url" -a -n "$community" ]; then
	echo "		<a class=\"community\" href=\"$url\">$community</a>"
fi

echo "		<ul>"

contact="$(uci -q get freifunk.@settings[0].contact)"
if [ -n "$contact" ]; then
	echo "			<li><a href='#'>Kontakt: $contact</a></li>"
fi

lat="$(uci -q get freifunk.@settings[0].latitude)"
lon="$(uci -q get freifunk.@settings[0].longitude)"
if [ -n "$lat" -a -n "$lon" ]; then
	echo "			<li><a href=\"https://www.openstreetmap.org?mlat=$lat&mlon=$lon&zoom=17\">Position: ${lat:0:8}N, ${lon:0:8}E</a></li>"
fi
%>			<li><form action="https://<% echo -n "$HTTP_HOST" %>"><button type="submit" class="login-button">Login</button></form></li>
		</ul>
	</div>
</div>
<div class="header">
	<h1><% uci -q get freifunk.@settings[0].name | tr -d '\n' %></h1>
	<h2>Firmware Version <% uci -q get freifunk.@settings[0].version | tr -d '\n' %></h2>
</div>
<div class="content">
	<div class="pure-g">
		<div class="pure-u-md-1-3">
			<h2>Neighboring nodes</h2>
			<h3><% (batctl neighbors -H 2> /dev/null | grep  -v '^No' | cut -c -17 | sort | uniq | wc -l) %></h3>
		</div>
		<div class="pure-u-md-1-3">
			<h2>All nodes</h2>
			<h3><% echo -n $((`batctl transglobal -H 2> /dev/null | grep '^ [^ ]' | cut -b 39-55 | sort | uniq | wc -l`+1)) %></h3>
		</div>
		<div class="pure-u-md-1-3">
			<h2>Local clients</h2>
			<h3><%
				echo -n $(batctl translocal -H 2> /dev/null | awk -v macs="$(cat /sys/class/net/*/address)" 'BEGIN{c=0} {if (index(macs, $1) == 0){ c+=1; }} END {print(c)}')
			%>
			</h3>
		</div>
	</div>
</div>

<%
. /lib/ff_shared.sh

echo '<div class="content">'
echo '	<h2 class="content-subhead">Neighboring nodes:</h2>'
echo '	<ul>'

macs="$(batctl neighbors | tail -n +3 | grep  -v '^No' | cut -c -17 | sort | uniq)"
if [ -n "$macs" ]; then
	prefix="$(uci get network.globals.ula_prefix)"
	i=1
	for mac in $macs; do
		echo "		<li><h3><a href=\"http://[$(ula_addr $prefix $mac)]\" class=\"neighbor\">Neighboring node $i</a></h3></li>"
		i=$((i+1))
	done
else
	echo '		<li><h3>No neighbors found.</h3></li>'
fi

echo '	</ul>'
echo '</div>'


max=$(uci -q get freifunk.@settings[0].service_display_max)
if [ -n "$max" -a "$max" != "0" ]; then
	addr_prefix="${ula_prefix%%::*}:"

	echo '<div class="content">'
	echo '	<h2 class="content-subhead">List of local offers in the free radio network:</h2>'
	echo '	<ul>'
	link_list=$(
		printf "$(alfred -r 91 | head -$max)" | awk -F'["\\]+' -v addr_prefix="$addr_prefix" '{ if($5 == "link" && match($7, "^[][ #[:alnum:]_\/.:]{1,128}$") && $9 == "label" && match($11, "^[][ [:alnum:]_\/.:]{1,32}$") && match($7, addr_prefix "|\.ff[a-z]{0,3}")) printf("			<li><h3><a href=\"%s\">%s</a></h3></li>\n", $7, $11) }'
	)
	[ -n "$link_list" ] && echo "$link_list" || echo '		<li><h3>No offers found.</h3></li>'
	echo '	</ul>'
	echo '</div>'
fi
%>

<script>

var own_data = <% /usr/sbin/print_map.sh %>;

function createCORSRequest(method, url) {
	var xhr = new XMLHttpRequest();
	if ("withCredentials" in xhr) {
		// XHR for Chrome/Firefox/Opera/Safari.
		xhr.open(method, url, true);
	} else if (typeof XDomainRequest != "undefined") {
		// XDomainRequest for IE.
		xhr = new XDomainRequest();
		xhr.open(method, url);
	} else {
		// CORS not supported.
		xhr = null;
	}
	return xhr;
}

var neighbors = document.getElementsByClassName("neighbor");
Array.prototype.forEach.call(neighbors, function(el) {
	var xhr = createCORSRequest('GET', el.href + "/cgi-bin/data");
	xhr.onload = function() {
		var neigh_data = JSON.parse(xhr.responseText);

		// set node name
		if ('name' in neigh_data) {
			el.textContent = neigh_data.name;
		}

		// add connection quality
		var qual = -1;
		if ('links' in neigh_data && 'links' in own_data) {
			Array.prototype.forEach.call(neigh_data.links, function(neigh_link) {
				Array.prototype.forEach.call(own_data.links, function(own_link) {
					if (own_link.dmac == neigh_link.smac) {
						qual = Math.max(qual, own_link.qual, neigh_link.qual);
					}
				});
			});
		}

		if (qual >= 0) {
			var e = document.createTextNode(" (" + qual.toFixed(0) + "%)");
			el.parentNode.appendChild(e);
		}
	};

	xhr.send();
});
</script>

</body>
</html>
