<%#
Opennet Firmware

Copyright 2015 Lars Kruse <devel@sumpfralle.de>

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%>

<%
require("luci.model.opennet.funcs")
require("luci.model.opennet.urls")
require("luci.tools.webadmin")
require("nixio.fs")

repo_url = on_function("get_default_opennet_opkg_repository_base_url")
configured_repo_url = on_function("get_configured_opennet_opkg_repository_base_url")
default_repo_url = on_function("get_default_opennet_opkg_repository_base_url")
function checked_if_true(value)
	if value then
		return 'checked="checked"'
	else
		return ""
	end
end
%>

<% if not on_bool_function("has_opennet_dns") then %>
	<div class="alert-message">
		<p><%:Opennet-DNS service is not available, yet (or quite slow). It is required for many features.%></p>
		<p><%:Please verify that you are connected to the network. Opennet-DNS should be available a few minutes after connecting to the Opennet mesh network.%></p>
		<p><%= luci.i18n.translatef('Take a look at the <a href="%s">Status</a> page and verify that you are connected to at least one neighbour.', on_url("status")) %></p>
	</div>
<% end %>

<%
-- alle Module mit uebersetzbarer Beschreibung sollten hier aufgelistet werden
-- Module, die hier fehlen, werden ohne Beschreibungstext angezeigt
local on_module_descriptions = {}
on_module_descriptions["on-openvpn"] = luci.i18n.translate("Access the Internet")
on_module_descriptions["on-usergw"] = luci.i18n.translate("Share your local internet connection with other members")
on_module_descriptions["on-captive-portal"] = luci.i18n.translate("Provide unrestricted internet access via Opennet to your vicinity")
on_module_descriptions["on-monitoring"] = luci.i18n.translate("Publish statistical data of network usage")
on_module_descriptions["on-goodies"] = luci.i18n.translate("Useful tools for network and system analysis")
on_module_descriptions["on-olsr2"] = luci.i18n.translate("Support for OLSRv2 and IPv6")
on_module_descriptions["on-olsr2-netjsongraph"] = luci.i18n.translate("Visualization for OLSRv2")
%>

<h2><a id="content" name="content"><%:Modules and Features%></a></h2>
<%= html_display_message_list(on_errors, "error") %>
<%= html_display_message_list(on_hints, "info") %>

<form method="post" action="<%=REQUEST_URI%>" enctype="multipart/form-data">
<div class="cbi-map"><fieldset class="cbi-section">
	<legend><%:Status of Modules%></legend>
		<div class="cbi-section-descr">
			<%:The Opennet Firmware consists of multiple modules providing distinct features within the network. Please pick which features should be enabled for this device.%>
			<br/>
			<%:Installation of modules may take quite a while (up to 30s) - please be patient.%>
		</div>

		<%# Quelle: /usr/lib/lua/luci/view/opkg.htm %>
		<%# Zeige freien Speicherplatz an. Inhalt wird durch Javascript hinzugefuegt. Stichwort: cbi-progressbar %>
		<div class="cbi-progressbar" title="<%:waiting%>">
                        <div>&#160;</div>
                </div> 

		<table class="cbi-section-table">
		<tr class="cbi-section-table-titles">
			<th class="cbi-section-table-cell"><%:Name%></th>
			<th class="cbi-section-table-cell"><%:Installed%></th>
			<th class="cbi-section-table-cell"><%:Enabled%></th>
			<th class="cbi-section-table-cell"><%:Action%></th>
			<th class="cbi-section-table-cell"><%:Description%></th>
		</tr>
		<% for _, mod_name in ipairs(line_split(on_function("get_on_modules"))) do
			local is_installed = on_bool_function("is_package_installed", { mod_name })
			local is_enabled = on_bool_function("is_on_module_installed_and_enabled", { mod_name })
			local action_label
			local action_url
		%>
			<tr class="cbi-section-table-row">
				<td class="cbi-section-table-cell"><%= mod_name %></td>
				<td class="cbi-section-table-cell opennet-bool" status="<%= is_installed and "y" or "" %>" />
				<td class="cbi-section-table-cell">
					<input class="cbi-input-checkbox" value="1" type="checkbox"
						 id="<%= mod_name %>" name="<%= mod_name %>_enabled"
						 <%= is_enabled and 'checked="checked"' or '' %>
						 <%= is_installed and "" or 'disabled="disabled"' %> />
				</td>
				<td class="cbi-section-table-cell">
					<% if is_installed then
						action_label = "remove"
						action_url = REQUEST_URI .. "?remove=" .. mod_name
					else
						action_label = "install"
						action_url = REQUEST_URI .. "?install=" .. mod_name
					end %>
					<a href="<%= action_url %>"><%= action_label %></a>
				</td>
				<td class="cbi-section-table-cell"><%= on_module_descriptions[mod_name] or mod_name %></td>
			</tr>
		<% end %>
		</table>
</fieldset></div>

<div class="cbi-map"><fieldset class="cbi-section">
<legend><%:Package Sources%></legend>
	<div class="cbi-section-descr"><%:Choose your preferred source of packages depending on your network connection.%></div>
	<table class="cbi-section-table">
		<tr class="cbi-section-table-row">
			<td class="cbi-section-table-cell">
				<label class="cbi-value-title" title="<%= repo_url %>"><%:Default%></label>
			</td>
			<td class="cbi-section-table-cell">
				<input class="cbi-input-radio" type="radio" name="repository_url" id="repository_url"
					value="<%= repo_url %>" <%= checked_if_true(repo_url == configured_repo_url) %> />
			</td>
			<td class="cbi-section-table-cell">
				<label><a href="<%= default_repo_url %>"><%= default_repo_url %></a></label>
			</td>
		</tr>
		<tr class="cbi-section-table-row">
			<td class="cbi-section-table-cell">
				<label class="cbi-value-title" for="repository_url_custom"><%:Custom%></label>
			</td>
			<td class="cbi-section-table-cell">
				<input class="cbi-input-radio" type="radio" name="repository_url" id="repository_url_custom"
					value="custom"	<%= checked_if_true(repo_url ~= configured_repo_url) %> />
			</td>
			<td class="cbi-section-table-cell">
				<input type="text" name="repository_url_custom" value="<%= configured_repo_url %>" />
			</td>
		</tr>
	</table>
</fieldset></div>

<% if not is_string_empty(modules_log) then %>
	<div class="cbi-map"><fieldset class="cbi-section">
	<legend><%:Package Installation Protocol%></legend>
		<div class="cbi-section-descr"><%:Review the details of installations and removals.%></div>
		<div class="cbi-section-node">
			<textarea xmlns="http://www.w3.org/1999/xhtml" readonly="readonly"
				rows="10" cols="110" wrap="off"><%=luci.util.pcdata(modules_log)%></textarea>
		</div>
	</fieldset></div>
<% end %>

<div class="cbi-page-actions">
	<input class="cbi-button cbi-button-save" type="submit" name="save" value="<%:Save%>" />
</div>
</form>

<script type="text/javascript">
<%# Quelle: /www/luci-static/resources/view/opkg.js %>
<%# freien Speicher berechnen %>
L.get('admin/system/opkg/statvfs', null, function(xhr, stat) {
        var pg = document.querySelector('.cbi-progressbar'),
            total = stat.blocks || 0,
            free = stat.bfree || 0;
        pg.firstElementChild.style.width = Math.floor(total ? ((100 / total) * free) : 100) + '%';
        pg.setAttribute('title', '%s (%.1024mB)'.format(pg.firstElementChild.style.width, free * (stat.frsize || 0)));
        
    });
</script>

<%+footer%>
