summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--config/templates/cyberdata-template.lua59
1 files changed, 44 insertions, 15 deletions
diff --git a/config/templates/cyberdata-template.lua b/config/templates/cyberdata-template.lua
index 515cceb..11206ba 100644
--- a/config/templates/cyberdata-template.lua
+++ b/config/templates/cyberdata-template.lua
@@ -6,32 +6,61 @@ local values = ...
xml = require("LuaXml")
-local init_cfg = xml.load("/var/www/provisioning/htdocs/CyberData/init.cfg")
+local InitFile = "/var/www/provisioning/htdocs/CyberData/init.cfg"
+
+local function findSection(xmlobj, name)
+ -- Cannot use xmlobj:find() because we don't want recursion
+ for i,s in ipairs(xmlobj) do
+ if s:tag() == name then
+ return s
+ end
+ end
+ return null
+end
+
+local function findOrAppendSection(xmlobj, name)
+ return findSection(xmlobj, name) or xmlobj:append(name)
+end
+
+local function setValue(xmlobj, name, value)
+ if value then
+ findOrAppendSection(xmlobj, name)[1] = value
+ end
+end
+
+-- Load the initial config
+local init_cfg
+local res, err = pcall(function()
+ init_cfg = xml.load(InitFile)
+end)
+if not res and err then
+ init_cfg = xml.new("specific")
+end
-- <IPSettings>
-- <SIPSettings>
-for pg, pg_t in pairs(values) do
- -- Is it of the form regX ?
- local num = string.match(pg, 'reg(%d+)')
- if num then
- if pg_t.extension ~= "" then
- init_cfg[2]:append("SIPServer")[1] = values.device.registrar
- init_cfg[2]:append("SIPUserID")[1] = pg_t.extension
- init_cfg[2]:append("SIPAuthID")[1] = pg_t.extension
- init_cfg[2]:append("SIPAuthPassword")[1] = pg_t.password
- init_cfg[2]:append("DialoutExtension0")[1] = pg_t.hotlinedestination
- init_cfg[2]:append("DialoutID0")[1] = pg_t.callerid
- end
+if values.reg1 then
+ local sipsettings = findOrAppendSection(init_cfg, "SIPSettings")
+ if values.device then
+ setValue(sipsettings, "SIPServer", values.device.registrar)
end
+ setValue(sipsettings, "SIPUserID", values.reg1.extension)
+ setValue(sipsettings, "SIPAuthID", values.reg1.extension)
+ setValue(sipsettings, "SIPAuthPassword", values.reg1.password)
+ setValue(sipsettings, "DialoutExtension0", values.reg1.hotlinedestination)
+ setValue(sipsettings, "DialoutID0", values.reg1.callerid)
end
-- <ClockSettings>
-init_cfg[3]:append("NTPServer")[1] = values.device.sntpserver
-init_cfg[3]:append("NTPTimezone")[1] = values.device.timezone
+if values.device then
+ local clocksettings = findOrAppendSection(init_cfg, "ClockSettings")
+ setValue(clocksettings, "NTPServer", values.device.sntpserver)
+ setValue(clocksettings, "NTPTimezone", values.device.timezone)
+end
-- init_cfg:save("TEST-cyberdata.xml")