#!/usr/bin/lua

local uci = require('simple-uci').cursor()


local function get_mem_total()
	for line in io.lines('/proc/meminfo') do
		local match = line:match('^MemTotal:%s+(%d+)')
		if match then
			return tonumber(match)
		end
	end
end

local max_requests = 32
if get_mem_total() < 48*1024 then
	max_requests = 16
end

uci:section('uhttpd', 'uhttpd', 'main', {
	listen_http = { '0.0.0.0:80', '[::]:80' },
	listen_https = {},

	home = '/lib/gluon/status-page/www',
	max_requests = max_requests,
})
uci:save('uhttpd')


for _, zone in ipairs({'mesh', 'loc_client'}) do
	uci:section('firewall', 'rule', zone .. '_http', {
		src = zone,
		dest_port = '80',
		proto = 'tcp',
		target = 'ACCEPT',
	})
end

uci:save('firewall')