#!/usr/bin/luaweb
json = require("json")
omg = require("omg")

function loadConfig()
    local v,v1,v2,k,k1,k2
    t = assert(loadfile("/data/config.lua"))
    t()
    
    config.struct_DChannelMap = {}

    -- correct some config Settings
    if(config.struct_Box~=nil and config.struct_Box.struct_DChannel==nil)then
        config.struct_Box.struct_DChannel = {}
    elseif(config.struct_Box.struct_DChannel[1]==nil)then
        local t = {}
        t = config.struct_Box.struct_DChannel
        config.struct_Box.struct_DChannel = nil
        config.struct_Box.struct_DChannel = {}
        config.struct_Box.struct_DChannel[1]=t
    end

    for k,v in ipairs(config.struct_Box.struct_DChannel) do
        config.struct_DChannelMap[v.string_IlidSlot .. "." .. v.string_IlidIndex] = k
        if (string.len(config.struct_Box.struct_DChannel[k].string_SipTrunkLoginUser or "") <= 0) then
            config.struct_Box.struct_DChannel[k].string_SipTrunkLoginUser = config.struct_Box.struct_DChannel[k].string_SipTrunkRegisterUser
        end
        if (string.len(config.struct_Box.struct_DChannel[k].string_SipTrunkRegisterUser or "") <= 0) then
            config.struct_Box.struct_DChannel[k].string_SipTrunkRegisterUser = config.struct_Box.struct_DChannel[k].string_SipTrunkLoginUser
        end
        if config.struct_Box.struct_DChannel[k].struct_DeviceGroup ~= nil then
            if config.struct_Box.struct_DChannel[k].struct_DeviceGroup[1] == nil then
                t = config.struct_Box.struct_DChannel[k].struct_DeviceGroup
                config.struct_Box.struct_DChannel[k].struct_DeviceGroup = nil
                config.struct_Box.struct_DChannel[k].struct_DeviceGroup = {}
                config.struct_Box.struct_DChannel[k].struct_DeviceGroup[1]=t
            end
        end
        if config.struct_Box.struct_DChannel[k].struct_GateParams ~= nil then
            if config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTable ~= nil then
                if config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTable[1] == nil then
                    t = config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTable
                    config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTable = nil
                    config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTable = {}
                    config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTable[1]=t
                end

                for k2,v2 in pairs(config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTable) do
                    if v2.struct_DestinationId ~=nil and v2.struct_DestinationId[1] == nil then
                        t = v2.struct_DestinationId
                        v2.struct_DestinationId = nil
                        v2.struct_DestinationId = {}
                        v2.struct_DestinationId[1]=t
                    end
                    if v2.struct_DestinationList ~=nil and v2.struct_DestinationList[1] == nil then
                        t = v2.struct_DestinationList
                        v2.struct_DestinationList = nil
                        v2.struct_DestinationList = {}
                        v2.struct_DestinationList[1]=t
                    end
                end 
            end
           
            if config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTableOut ~= nil then
                if config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTableOut[1] == nil then
                    t = config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTableOut
                    config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTableOut = nil
                    config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTableOut = {}
                    config.struct_Box.struct_DChannel[k].struct_GateParams.struct_CallProcTableOut[1]=t
                end
            end
        end
    end

    for k,v in ipairs(config.struct_Box.struct_DChannel) do
        if v.string_SipTrunkIpMode ~= nil and  v.string_SipTrunkIpMode == "" then
            v.string_SipTrunkIpMode = "UDP"
        end
        if v.struct_GateParams ~= nil and v.struct_GateParams.struct_CallProcTable ~= nil then
            for k2,v2 in ipairs(v.struct_GateParams.struct_CallProcTable) do
                if v2.struct_DestinationList ~= nil then
                    v2.string_RegularExpressionForIpAddressMatch = v.string_SipTrunkRegularExpressionIpMatch
                    if v2.string_Destination == nil then
                        v2.string_Destination = "trunk"
                    end

                    v2.struct_DestinationId={}
                    for k3,v3 in ipairs(v2.struct_DestinationList) do
                        v2.struct_DestinationId[#v2.struct_DestinationId+1] = {}
                        v2.struct_DestinationId[#v2.struct_DestinationId].destination = tostring(config.struct_DChannelMap[v3.string_Id])
                        v2.struct_DestinationId[#v2.struct_DestinationId].priority = v3.priority
                    end
                end
            end
        end
    end
end

loadConfig()

if config ~= nil then
   omg.runScript(config,arg,"show config\n")
end




