require("rsmdict")
lfs = require("lfs")

local P = {};
P.__index = P;

local web_modules_path = "/srv/http/modsxml"

local function command_exec(cmd)
  local f = io.popen(cmd) 
  value = f:read("*a") or "none"
  f:close()
  return value
end


local function log(msg)
    command_exec("logger -t web_modules "..msg)
end
   

local function getObject(key, value)
  local profile_cfg = {}
  profile_cfg.objid =  RSM.RSM_TagID.TAG_METALIST
  profile_cfg[RSM.RSM_TagID.TAG_METALIST_NAME] = "web_modules"
  profile_cfg[RSM.RSM_TagID.TAG_CAP_LABEL] = key
  profile_cfg[RSM.RSM_TagID.TAG_CAP_VALUE] = value 
  return profile_cfg
end

local function getWebModules()
  log("getWebModules:")

  local msg = {}  
  local msgCount = 0;
  for file in lfs.dir(web_modules_path) do
    log(" - "..file)
    msg[msgCount] = getObject("", file)
    msgCount = msgCount + 1
  end
  
  return msg
end

--Convert the config file into an object message and return that.
function P.getConfig(cfg)
    log("GET")
    return getWebModules();
end


--Function to lookup and set a specific object(s)
--and then to update the cofig file with those values
function P.setConfig(cfg)
end

function P.deleteConfig(cfg)
end

function P.replaceConfig(cfg)    
end



P.__tostring = function () return "Web Modules package"; end

local web_module = {};
setmetatable(web_module, P);
return web_module;