#!/usr/bin/env lua

--[[
  Copyright (C) 2013-2020 LibreMesh.org
  This is free software, licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3

  Copyright 2020 Santiago Piccinini <spiccinini@altermindi.net>
  Copyright 2017 Marcos Gutierrez <gmarcos@altermindi.net>
  Copyright 2017 Nicolas Echaniz <nicoechaniz@altermundi.net>
]] --
local json = require 'luci.jsonc'
local utils = require 'lime.utils'

local limeutils = require 'lime-utils'

local function get_cloud_nodes(msg)
    local cloud = limeutils.get_cloud_nodes()
    utils.printJson(cloud)
end

local function get_mesh_ifaces(msg)
    local result = limeutils.get_mesh_ifaces()
    utils.printJson(result)
end

local function get_node_status(msg)
    local result = limeutils.get_node_status()
    utils.printJson(result)
end

local function get_notes()
    local result = limeutils.get_notes()
    utils.printJson(result)
end

local function set_notes(msg)
    local result = limeutils.set_notes(msg)
    utils.printJson(result)
end

local function get_community_settings()
    local result = limeutils.get_community_settings()
    utils.printJson(result)
end

local function get_config()
    local result = limeutils.get_config()
    utils.printJson(result)
end

local function get_upgrade_info(msg)
    local result = limeutils.get_upgrade_info()
    utils.printJson(result)
end

local function hotspot_wwan_get_status(msg)
    local result = limeutils.hotspot_wwan_get_status(msg)
    utils.printJson(result)
end

local methods = {
    get_cloud_nodes = {no_params = 0},
    get_mesh_ifaces = {no_params = 0},
    get_node_status = {no_params = 0},
    get_notes = {no_params = 0},
    set_notes = {text = 'value'},
    get_community_settings = {no_params = 0},
    get_config = {no_params = 0},
    get_upgrade_info = {no_params = 0},
    hotspot_wwan_get_status = {no_params = 0}
}

if arg[1] == 'list' then utils.printJson(methods) end

if arg[1] == 'call' then
    local msg = utils.rpcd_readline()
    msg = json.parse(msg)
    if arg[2] == 'get_cloud_nodes' then
        get_cloud_nodes(msg)
    elseif arg[2] == 'get_mesh_ifaces' then
        get_mesh_ifaces(msg)
    elseif arg[2] == 'get_node_status' then
        get_node_status(msg)
    elseif arg[2] == 'get_notes' then
        get_notes(msg)
    elseif arg[2] == 'set_notes' then
        set_notes(msg)
    elseif arg[2] == 'get_community_settings' then
        get_community_settings(msg)
    elseif arg[2] == 'get_config' then
        get_config(msg)
    elseif arg[2] == 'get_upgrade_info' then
        get_upgrade_info(msg)
    elseif arg[2] == 'hotspot_wwan_get_status' then
        hotspot_wwan_get_status(msg)
    else
        utils.printJson({error = "Method not found"})
    end
end
