require("rsmdict")

--The Objects that is supported by the Script
local obj_ids = {
                   RSM.RSM_TagID.TAG_BUS_TEMPLATES                    
                }
                
local templates = {}

local snmp_conf_file="/etc/rsm/sddconf.xml"
local mdd_conf_file="/etc/rsm/mddconf.xml"
local ddd_conf_file="/etc/rsm/dddconf.xml"

local snmp_TemplateFiles= {}
local mdd_TemplateFiles= {}
local ddd_TemplateFiles= {}

local snmp_TemplatePath= ""
local mdd_TemplatePath= ""
local ddd_TemplatePath= ""

function print_table(t,p)

   for k,v in pairs(t) do
     print(p,k,v)
     if(type(v)=="table") then 
         print_table(v,p.."\t")
     end
  end
end


--Retuen the Object List
function getObjects()
 return obj_ids;
end

--Convert the config file into an object message nd return that.
--Actually supposed to check the request object to find the exact object if any to 
--return. We jippo that by always returning all of them
function getConfig(cfg)
  load_templates()
  msg = templates_to_msg()
  return msg
end

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

function scandir(directory)
  local i, t, popen = 0, {}, io.popen
    for filename in popen('ls -a "'..directory..'"'):lines() do
        i = i + 1
        t[i] = filename
    end
    return t
end

function writefile(fileaddres,value)
  local f = io.open(fileaddres,"w+")
  f:write(value)
  f:close()
  return true
end

function load_templates() 
  --print("Loading pf data...") 
  templates = {}
  
  --Get the file names of all the templates
  snmpConfig = command_exec("cat "..snmp_conf_file)
  snmp_TemplateFiles = {}  
  if (snmpConfig ~= 0) then    
    snmp_TemplatePath = snmpConfig:match('<path%s+name=%"templates%"%s->(.-)</path>') or ""    
    if (snmp_TemplatePath ~= "") then
      snmp_TemplateFiles = scandir(snmp_TemplatePath)
      table.remove(snmp_TemplateFiles,1)
      table.remove(snmp_TemplateFiles,1)
    end
  end
  
  mddConfig = command_exec("cat "..mdd_conf_file)
  mdd_TemplateFiles = {}  
  if (mddConfig ~= 0) then    
    mdd_TemplatePath = mddConfig:match('<path%s+name=%"templates%"%s->(.-)</path>') or ""    
    if (mdd_TemplatePath ~= "") then
      mdd_TemplateFiles = scandir(mdd_TemplatePath)
      table.remove(mdd_TemplateFiles,1)
      table.remove(mdd_TemplateFiles,1)
    end
  end
  
  dddConfig = command_exec("cat "..ddd_conf_file)
  ddd_TemplateFiles = {}  
  if (dddConfig ~= 0) then    
    ddd_TemplatePath = dddConfig:match('<path%s+name=%"templates%"%s->(.-)</path>') or ""    
    if (ddd_TemplatePath ~= "") then
      ddd_TemplateFiles = scandir(ddd_TemplatePath)
      table.remove(ddd_TemplateFiles,1)
      table.remove(ddd_TemplateFiles,1)
    end
  end
  
--[[print("List of SNMP devices:")
print_table(snmpFiles,0)
print("List of SNMP templates:")
print_table(templateFiles,0)]]--

--Get the details of each template file
if(snmp_TemplateFiles ~= 0) then
  for idx, snmpTemplateFile in pairs(snmp_TemplateFiles) do
    cfg_snmp = command_exec("cat "..snmp_TemplatePath .. "/" .. snmpTemplateFile)   
      templates[snmpTemplateFile] = {}
      templates[snmpTemplateFile].filename = snmpTemplateFile
      templates[snmpTemplateFile].device = "SNMP"
      templates[snmpTemplateFile].make = cfg_snmp:match("<make>%s-(.-)%s-</make>") or "?"
      templates[snmpTemplateFile].model = cfg_snmp:match("<model>%s-(.-)%s-</model>") or "?"
      templates[snmpTemplateFile].type = cfg_snmp:match("<type>%s-(.-)%s-</type>") or "Unknown"
      templates[snmpTemplateFile].version = cfg_snmp:match("<version>%s-(.-)%s-</version>") or ""            
  end    
end

if(mdd_TemplateFiles ~= 0) then
  for idx, mddTemplateFile in pairs(mdd_TemplateFiles) do
    cfg_mdd = command_exec("cat "..mdd_TemplatePath .. "/" .. mddTemplateFile)   
      templates[mddTemplateFile] = {}
      templates[mddTemplateFile].filename = mddTemplateFile
      templates[mddTemplateFile].device = "ModBus"
      templates[mddTemplateFile].make = cfg_mdd:match("<make>%s-(.-)%s-</make>") or mddTemplateFile
      templates[mddTemplateFile].model = cfg_mdd:match("<model>%s-(.-)%s-</model>") or "?"
      templates[mddTemplateFile].type = cfg_mdd:match("<type>%s-(.-)%s-</type>") or "Unknown"
      templates[mddTemplateFile].version = cfg_mdd:match("<version>%s-(.-)%s-</version>") or ""            
  end    
end

if(ddd_TemplateFiles ~= 0) then
  for idx, dddTemplateFile in pairs(ddd_TemplateFiles) do
    cfg_ddd = command_exec("cat "..ddd_TemplatePath .. "/" .. dddTemplateFile)   
      templates[dddTemplateFile] = {}
      templates[dddTemplateFile].filename = dddTemplateFile
      templates[dddTemplateFile].device = "DLMS"
      templates[dddTemplateFile].make = cfg_ddd:match("<make>%s-(.-)%s-</make>") or dddTemplateFile
      templates[dddTemplateFile].model = cfg_ddd:match("<model>%s-(.-)%s-</model>") or "?"
      templates[dddTemplateFile].type = cfg_ddd:match("<type>%s-(.-)%s-</type>") or "Unknown"
      templates[dddTemplateFile].version = cfg_ddd:match("<version>%s-(.-)%s-</version>") or ""            
  end    
end

              
end

function templates_to_msg()
  local msg = {}
  template_crl = 0  
  
  print("**Templates to message....")
  --print("Load Message: ")
  --Send Subnet info
  for idx,tf in pairs(templates) do
     template_cfg = {}
     template_cfg.objid =  RSM.RSM_TagID.TAG_BUS_TEMPLATES
     template_cfg[RSM.RSM_TagID.TAG_BUS_DEVICE] = tf.device 
     template_cfg[RSM.RSM_TagID.TAG_BUS_TEMPLATE] = tf.filename              
     template_cfg[RSM.RSM_TagID.TAG_BUS_MAKE] = tf.make
     template_cfg[RSM.RSM_TagID.TAG_BUS_MODEL] = tf.model
     template_cfg[RSM.RSM_TagID.TAG_BUS_TYPE] = tf.type
     template_cfg[RSM.RSM_TagID.TAG_BUS_VERSION] = tf.version   
     
     print("Template Filename: "..template_cfg[RSM.RSM_TagID.TAG_BUS_TEMPLATE]) 
               
     msg[template_crl] = template_cfg
     template_crl = template_crl+1
     
  end  
  
  return msg
end



--Testing....

--print("Testing...")
--print("Get Config: ")

--print_table(getConfig(),0)
--[[
local testCfg = {}
local obj ={}
local obj1 ={}

obj.objid = RSM.RSM_TagID.TAG_BUS_SNMP
obj[RSM.RSM_TagID.TAG_BUS_SERIALNUM] = "0x7FA41A01"     
obj[RSM.RSM_TagID.TAG_BUS_LABEL] = "Test SNMPpppp"
--obj[RSM.RSM_TagID.TAG_BUS_TEMPLATE] = 9600
--obj[RSM.RSM_TagID.TAG_BUS_HOST] = 5
--obj[RSM.RSM_TagID.TAG_BUS_COMMUNITY] = 1
--obj[RSM.RSM_TagID.TAG_BUS_VERSION] = 1


obj1.objid = RSM.RSM_TagID.TAG_BUS_SNMP
--obj1[RSM.RSM_TagID.TAG_BUS_SERIALNUM] = ""     
obj1[RSM.RSM_TagID.TAG_BUS_LABEL] = "test new snmp"
obj1[RSM.RSM_TagID.TAG_BUS_TEMPLATE] = "psc3.xml"
obj1[RSM.RSM_TagID.TAG_BUS_HOST] = "10.15.3.20"
obj1[RSM.RSM_TagID.TAG_BUS_COMMUNITY] = "public"
obj1[RSM.RSM_TagID.TAG_BUS_VERSION] = "2c"

testCfg[0] = obj
testCfg[1] = obj1

setConfig(testCfg)
print_table(getConfig(),0)]]--





























