#!/usr/bin/luaweb

function printf(fmt,...)
  local s
  
  --print(_VERSION)
      
  s = fmt:format(...)
      
  if (string.sub(s, -1, -1) == "\n") then
     s = string.sub(s, 1, -2)
  end
      
  print(s)
  if DOFLUSH==true then
      io.flush ()
   end
end

dataDir = os.getenv("DATA_DIR") or "/data"

local f = io.open(dataDir .. "/register.lua", "r")
if f ~= nil then
   f:close()
   dofile(dataDir .. "/register.lua")
end

doDebug = false
instanceNr=0
taskInitialName="printregistered"

function doPrint(list)
   for k,v in pairs(list) do
      if v.enabled == "yes" then
         if doDebug then
            dumpTable(0,v)
         end

         if v.mode == "u" then
           sMode = "UDP"
         else
           sMode = "TCP"
         end
         
         printf('"%s","%s","%s","%s","%s","%s","%s","%s","%s","%s"',
                v.enabled or "NIL",
                v.fromDisplayName or "NIL",
                v.fromAddress or "NIL",
                v.expiresTime or "NIL",
                v.expires or "NIL",
                v.port or "NIL",
                v.number or "NIL",
                v.ipAddress or "NIL",
                sMode or "NIL",
                v.contactAddress or "NIL"
                )
      end
   end
end

printf("Enabled,Display Name,Address,Expires at,Expires (sec),Port,Number,IP Address,Mode,Contact Address")

if _m ~= nil then
   doPrint(_m)
end


