#!/usr/bin/env lua
--Copyright (c) 2019 www.mleaf.org. All rights reserved.
--微信公众号【WiFi物联网】

ubus = require('ubus')

local json = require "luci.json"

local conn = ubus.connect()
if not conn then
	error("Failed to connect to ubus")
end

local mwol_ubus = conn:call("uci", "get", {config = "mwol"})

local mwol_table = mwol_ubus["values"]

local mwol_conf = {}
local mwo_enable

function get_mwol_conf(mTable)

	for key, val in pairs(mTable) do
		if string.match(key, '%.[%a]*', 1) == nil then
			if key == "enable" then
				mwo_enable = val
			elseif key == "sslenable" then
				if val == "1" then
					mwol_conf["ssl"] =  true
				else
					mwol_conf["ssl"] =  false
				end
			elseif key=="encryptionenable" then
				if val == "1" then
					mwol_conf["encryption"] =  true
				else
					mwol_conf["encryption"] =  false
				end
			elseif key == "port" then
				mwol_conf[key] = tonumber(val)
			else
				mwol_conf[key] = val
			 end
		end
	end
end

for key, val in pairs(mwol_table) do

	if key == "mwol_conf" then
		get_mwol_conf(val)
	end
end

jsonStr = json.encode(mwol_conf)
print(jsonStr)
