#!/opt/lnxall_app/bin/lua

local uci = require "uci"
local invoker = require "invoker"

local gfmt = string.format
local g_config = "/etc/config/dnsconfig"
local g_dns = { [1] = "8.8.8.8", [2] = "114.114.114.114" }
local g_outtime = 12
local g_renum = 3

local function current_dns_load()
	local ucidns = uci.cursor('/etc/config')
	if not ucidns then
		io.stderr:write("Error, uci system has corrupted.\n")
		io.stderr:flush()
		return false
	end

	local dns1 = ucidns:get('dnsconfig', "@lnxdnscfg[0]", "dns1")
	local dns2 = ucidns:get('dnsconfig', "@lnxdnscfg[0]", "dns2")
	local outtime = ucidns:get('dnsconfig', "@lnxdnscfg[0]", "outtime")
	local attempts = ucidns:get('dnsconfig', "@lnxdnscfg[0]", "attempts")	
	ucidns:close(); ucidns = nil
	if invoker.isipv4(dns1) then g_dns[1] = dns1 end
	if invoker.isipv4(dns2) then g_dns[2] = dns2 end
	if outtime then 
		outtime = tonumber(outtime) 
		if outtime and outtime > 1 and outtime < 13 then g_outtime = outtime end
	end
	if attempts then 
		attempts = tonumber(attempts)
		if attempts and attempts > 0 and attempts < 6 then g_renum = attempts end
	end
	return true
end

local function mainfunc(arg1, arg2, arg3, arg4)
	local dowrite = false
	current_dns_load()
	if arg1 or arg2 then
		if invoker.isipv4(arg1) and g_dns[1] ~= arg1 then
			dowrite = true
			io.stdout:write(gfmt("Updating DNS1 from %s => %s\n", g_dns[1], arg1))
			io.stdout:flush()
			g_dns[1] = arg1
		end
		if invoker.isipv4(arg2) and g_dns[2] ~= arg2 then
			dowrite = true
			io.stdout:write(gfmt("Updating DNS1 from %s => %s\n", g_dns[2], arg2))
			io.stdout:flush()
			g_dns[2] = arg2
		end
	end

	if arg3 then
		arg3 = tonumber(arg3)
		if arg3 and arg3 > 1 and arg3 < 13 then
			if g_outtime ~= arg3 then
				dowrite = true
				io.stdout:write(gfmt("Updating outtime from %s => %s\n", g_outtime, arg3))
				io.stdout:flush()
				g_outtime = arg3
			end
		else
			io.stderr:write("Error,Outtime need number between 2-12\n")
			io.stderr:flush()
		end
	end
	if arg4 then
		arg4 = tonumber(arg4)
		if arg4 and arg4 > 0 and arg4 < 6 then
			if g_renum ~= arg4 then
				dowrite = true
				io.stdout:write(gfmt("Updating attempts from %s => %s\n", g_renum, arg4))
				io.stdout:flush()
				g_renum = arg4
			end
		else
			io.stderr:write("Error,Attempts need number between 1-5\n")
			io.stderr:flush()
		end
	end

	if dowrite then
		local ddat = gfmt("config lnxdnscfg\n\toption dns1 '%s'\n\toption dns2 '%s'\n\toption outtime '%s'\n\toption attempts '%s'\n",
			g_dns[1], g_dns[2], g_outtime, g_renum)
		local fh = io.open(g_config, "wb")
		if not fh then
			io.stderr:write(gfmt("Error, failed to open file for writing: %s\n", g_config))
			io.stderr:flush()
			os.exit(1)
		end
		fh:write(ddat); fh:close(); fh = nil
		io.stdout:write("Restarting DNSWATCHER service...\n")
		io.stdout:flush()
		invoker.invoke(0, "systemctl", "restart", "dnswatcher")
	end

	io.stdout:write(gfmt("Current DNS settings:\n\tDNS1: %s\n\tDNS2: %s\n\ttimeout: %s\n\tattempts: %s\n",
		g_dns[1], g_dns[2], g_outtime, g_renum))
	io.stdout:flush()
end

mainfunc(arg[1], arg[2], arg[3], arg[4])
os.exit(0)
