diff options
-rw-r--r-- | config/templates/cyberdata-template.lua | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/config/templates/cyberdata-template.lua b/config/templates/cyberdata-template.lua index 3d3f0b8..607ccfc 100644 --- a/config/templates/cyberdata-template.lua +++ b/config/templates/cyberdata-template.lua @@ -5,8 +5,10 @@ local values = ... xml = require("LuaXml") +fs = require("acf.fs") -local InitFile = "/var/www/provisioning/htdocs/CyberData/init.cfg" +local FirmwareFile = "/var/www/provisioning/htdocs/000000cd.xml" +local InitFile = "/var/www/provisioning/htdocs/CyberData/initial.xml" local function findSection(xmlobj, name) -- Cannot use xmlobj:find() because we don't want recursion @@ -28,6 +30,20 @@ local function setValue(xmlobj, name, value) end end +function mergeXML(xmlobj, xmlobj2) + -- [0] is the name + -- [1] is the value or a table for section + if type(xmlobj2[1]) == "string" then + xmlobj[0] = xmlobj2[0] + xmlobj[1] = xmlobj2[1] + else + for i,v in ipairs(xmlobj2) do + mergeXML(findOrAppendSection(xmlobj, xmlobj2[i][0]), xmlobj2[i]) + end + end + return xmlobj +end + local function yesno(xmlobj, name, value) if value ~= nil then if (value == false) then @@ -39,21 +55,36 @@ local function yesno(xmlobj, name, 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") +local function loadConfig(file) + local cfg + local res, err = pcall(function() + --cfg = xml.load(file) + + -- Hack to allow download of config files with firmware URL, replacing $SERV with SERVER_NAME + local contents = fs.read_file(file) or "" + if ENV and ENV.SERVER_NAME then + contents = string.gsub(contents, "%$SERV", ENV.SERVER_NAME) + end + cfg = xml.eval(contents) + end) + if not res and err then + cfg = xml.new("specific") + end + return cfg end +-- Load the initial config +local cfg = loadConfig(FirmwareFile) +local init_cfg = loadConfig(InitFile) + +cfg = mergeXML(cfg, init_cfg) + -- <IPSettings> -- <SIPSettings> if values.reg1 then - local sipsettings = findOrAppendSection(init_cfg, "SIPSettings") + local sipsettings = findOrAppendSection(cfg, "SIPSettings") if values.device then setValue(sipsettings, "SIPServer", values.device.registrar) end @@ -62,13 +93,12 @@ if values.reg1 then setValue(sipsettings, "SIPAuthPassword", values.reg1.password) setValue(sipsettings, "DialoutExtension0", values.reg1.hotlinedestination) setValue(sipsettings, "DialoutID0", values.reg1.callerid) - end -- <DeviceSettings> if values.device then - local DeviceSettings = findOrAppendSection(init_cfg, "DeviceSettings") + local DeviceSettings = findOrAppendSection(cfg, "DeviceSettings") setValue(DeviceSettings, "SpeakerVolume", values.device.cyberspeakervolume) setValue(DeviceSettings, "AdminPassword", values.device.adminpassword) setValue(DeviceSettings, "RingVolume", values.device.cyberringvolume) @@ -83,19 +113,19 @@ end -- <ButtonSettings> if values.device then - local ButtonSettings = findOrAppendSection(init_cfg, "ButtonSettings") + local ButtonSettings = findOrAppendSection(cfg, "ButtonSettings") yesno (ButtonSettings, "PlayToneWhileRelayActive", values.device.playtonewhilerelayactive) end -- <ClockSettings> if values.device then - local clocksettings = findOrAppendSection(init_cfg, "ClockSettings") + local clocksettings = findOrAppendSection(cfg, "ClockSettings") setValue(clocksettings, "NTPServer", values.device.sntpserver) setValue(clocksettings, "NTPTimezone", values.device.timezone) end --- init_cfg:save("TEST-cyberdata.xml") +-- cfg:save("TEST-cyberdata.xml") -print(init_cfg) +print(cfg) %> |