#!/usr/bin/luaweb

json = require("json")
omg = require("omg")

file="/usr/bin/article2name.json"
log=false

function copyTableIntoOther(out,data)
   for k,v in pairs(data) do
      if (type(v) == "table") then
         if out[k]==nil or type(out[k])~="table" then
            out[k]={}
         end
         copyTableIntoOther(out[k],v)
        else
           out[k] = v
        end
    end
end

function getJsonFile(fileName,info)
    local f
    if io ~= nil and io.open ~= nil then
       f = io.open(fileName,"r")
        if f ~= nil then
           local content = f:read("*all")
            if content ~= nil then
               local i = json.decode(content)
               if i~=nil ~= nil then
                  copyTableIntoOther(info,i)
               end 
            end
        end
    end
end

helpString=omg.getHelpString(arg[0],"get article info\n")
helpString=helpString .. "\t--list\tgives a list\n"
function omg.getHelpString(name,desc)
 return helpString
end

myOptions=omg.splitCommandlineString(arg)
 
info={}
getJsonFile(file,info)


if info ~= nil then
   if myOptions.list~=nil then
      for k,v in pairs(info) do
         print(k)
      end
   else
      omg.runScript(info, arg, "show article info\n")
   end
end
