require("rsmdict")


--The Objects that is supported by the Script
local obj_ids = {
                   RSM.RSM_TagID.TAG_NET_DNS_SERVER,                   
                   RSM.RSM_TagID.TAG_NET_DNS_DOMAIN
                                      
                }
local servers = {}
local domains = {}
local dns_file = ""
local dns_conf_file_test = "/usr/share/lua/kses/test"
local dns_conf_file="/etc/resolv.conf"

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_dns()
  msg = dns_to_msg()
  return msg
end

--Function to lookup and set a specific object(s)
--and then to update the cofig file with those values
function setConfig(cfg)
   --load_dns()
   servers = {}
   domains = {}
   update_dns(cfg)
   store_dns()
end

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

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

function load_dns()
  serverCount = 0
  domainCount = 0
  resolv = command_exec("cat "..dns_conf_file)
  dns_file = resolv    
  servers = {}
  domains = {}  
  
  --Add Server name Objects
  for server in (dns_file:gmatch("nameserver%s+([^%s]+)")) do --[^\r\n]+"
    serverCount = serverCount + 1
    servers[server] = {}      
    servers[server].name = server  
    servers[server].priority = serverCount           
            
  end  
  
  --Add Search Domain objects
  for domain in (dns_file:gmatch("search%s+(.-)\n")) do --search%s+(.+)
    domainCount = domainCount + 1
    domains[domain] = {}        
    domains[domain].search = domain              
                        
  end   
end

function dns_to_msg()
  local msg = {}
  if_ctr = 0
  
  --Send DNS Server names
  for idx,dnsf in pairs(servers) do    
       
     serv_cfg = {}        
     serv_cfg.objid =  RSM.RSM_TagID.TAG_NET_DNS_SERVER
     serv_cfg[RSM.RSM_TagID.TAG_NET_SERVERNAME] = dnsf.name
     serv_cfg[RSM.RSM_TagID.TAG_NET_PRIORITY] = dnsf.priority
     
     msg[if_ctr] = serv_cfg     
     if_ctr = if_ctr+1
  end
  --Sned search domains
  for idx,dnsf in pairs(domains) do
     
     dom_cfg = {}        
     dom_cfg.objid =  RSM.RSM_TagID.TAG_NET_DNS_DOMAIN
     dom_cfg[RSM.RSM_TagID.TAG_NET_DOMAINNAME] = dnsf.search  
          
     msg[if_ctr] = dom_cfg     
     if_ctr = if_ctr+1
  end
  return msg  
end

function update_dns(cfg)
   for idx,obj in pairs(cfg) do      
      print(obj[RSM.RSM_TagID.TAG_NET_SERVERNAME])
      if obj.objid and obj.objid == RSM.RSM_TagID.TAG_NET_DNS_SERVER then
          if obj[RSM.RSM_TagID.TAG_NET_SERVERNAME] then
              servers[obj[RSM.RSM_TagID.TAG_NET_SERVERNAME]]={}              
              servers[obj[RSM.RSM_TagID.TAG_NET_SERVERNAME]].name = obj[RSM.RSM_TagID.TAG_NET_SERVERNAME]      
              servers[obj[RSM.RSM_TagID.TAG_NET_SERVERNAME]].priority = obj[RSM.RSM_TagID.TAG_NET_PRIORITY]                    
          end
      elseif obj.objid and obj.objid == RSM.RSM_TagID.TAG_NET_DNS_DOMAIN then
          if obj[RSM.RSM_TagID.TAG_NET_DOMAINNAME] then
              domains[obj[RSM.RSM_TagID.TAG_NET_DOMAINNAME]]={}              
              domains[obj[RSM.RSM_TagID.TAG_NET_DOMAINNAME]].search = obj[RSM.RSM_TagID.TAG_NET_DOMAINNAME]                  
          end
      end                   
   end 
end

function store_dns()
local dns_config =  "domain ostb.co.za\n"

local serverList = {}
local count = 0

    for idx,serv in pairs(servers) do   
      count = count + 1               
       serverList[serv.priority] =  serv.name         
   end 

   for idx,dom in pairs(domains) do    
       dns_config = dns_config.."search "..dom.search.."\n"      
   end
   --[[for idx,serv in pairs(servers) do                 
       dns_config = dns_config.."nameserver "..serv.name.."\n"         
   end ]]--
   for i=1,count,1 do                 
       dns_config = dns_config.."nameserver "..serverList[i].."\n"         
   end          
        
   dns_file = dns_config        
   writefile(dns_conf_file,dns_file)
   
end  
        
  
--Testing....

--[[print("Testing...")

print("Get Config: ")
print_table(getConfig(),0)

local testCfg = {}
local obj ={}
local obj1 ={}
obj.objid = RSM.RSM_TagID.TAG_NET_DNS_SERVER
obj1.objid = RSM.RSM_TagID.TAG_NET_DNS_SERVER
obj[RSM.RSM_TagID.TAG_NET_SERVERNAME] = "10.15.1.5"
obj1[RSM.RSM_TagID.TAG_NET_SERVERNAME] = "8.8.8.8"
testCfg[1] = obj
testCfg[2] = obj1

local obj2 ={}
obj2.objid = RSM.RSM_TagID.TAG_NET_DNS_DOMAIN
obj2[RSM.RSM_TagID.TAG_NET_DOMAINNAME] = "ostb.co.za kses.net"
testCfg[3] = obj2

setConfig(testCfg)
print("Get Config: ")
print_table(getConfig(),0)]]--
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 