#!/usr/bin/lua

dofile("/usr/fb/bin/sipisdn/lib.lua")

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

doDebug = false

function getProtocolStringAndDirection(protocol)
    if tonumber(protocol) == 2 then
        return "PP","TE"
    elseif tonumber(protocol) == 3 then
        return "PMP","TE"
    elseif tonumber(protocol) == 6 then
        return "PPNA","TE"
    elseif tonumber(protocol) == 7 then
        return "PPNT","NT"
    elseif tonumber(protocol) == 8 then
        return "PPNTNA","NT"
    else 
        return "PMP","TE"
    end
end

function isActiveAndTE(type,slot,index)
    for k,v in ipairs(config.struct_Box.struct_DChannel) do
        
        local protocoll,direction = getProtocolStringAndDirection(v.long_Protocol or 2)
        
        if v.string_IlidType == type and v.string_IlidIndex == index then
            if v.bool_CfgValid and direction == "TE" then
                if doDebug then
                    printf("found %s %s.%s\n", 
                           v.string_IlidType or "NIL",
                           v.string_IlidSlot or "NIL",
                           v.string_IlidIndex or "NIL"
                       )
                end
                return true 
            end
        end
    end
    return false
end

function getHardwareIndex(kind,ilidIndex,offset)
   for k,v in ipairs(config.struct_Box.struct_DChannel) do
      if config.struct_Box.struct_DChannel[k].string_IlidType == kind and 
         config.struct_Box.struct_DChannel[k].string_IlidIndex == ilidIndex then
            local index = tonumber(config.struct_Box.struct_DChannel[k].string_IlidSlot)-1+offset
            return tostring(index)
      end
   end
   return "8"
end

function getPrioInitialValue(index)
    if (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Pri1" then
        return getHardwareIndex("S2M","a",0)
    elseif (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Pri2" then
        return getHardwareIndex("S2M","b",1)
    elseif (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Pri3" then
        return getHardwareIndex("S2M","a",2)
    elseif (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Pri4" then
        return getHardwareIndex("S2M","b",3)
    elseif (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Bri" then
        return getHardwareIndex("S0","a",0)
    elseif (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Intern" then
        return "8"
    else
        return "8"
    end
end

if config~=nil and config.struct_Box~=nil and config.struct_Box.struct_DChannel~=nil then
    -- correct some config Settings
    if(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

    if doDebug then
        for k,v in ipairs(config.struct_Box.struct_DChannel) do
            local protocoll,direction = getProtocolStringAndDirection(v.long_Protocol or 2)
            printf("active=%s, ilidType='%s', protocol='%s' direction='%s'\n", 
                   tostring(v.bool_CfgValid or "NIL"),
                   v.string_IlidType or "NIL",
                   protocoll or "NIL",
                   direction or "NIL"
               )
        end
    end

    local prio= {}
    local index = 1

    if config.struct_Box.string_IsdnClockMaster == "manual" then
        prio[1]=getPrioInitialValue(1)
        prio[2]=getPrioInitialValue(2)
        prio[3]=getPrioInitialValue(3)
    else
        prio[1]="8"                                 
        prio[2]="8"
        prio[3]="8"

        if isActiveAndTE("S2M",nil,"a") then
            prio[index]=getHardwareIndex("S2M","a",0)
            index=index+1
        end

        if isActiveAndTE("S2M",nil,"b") then
            prio[index]=getHardwareIndex("S2M","b",1)
            index=index+1
        end

        if isActiveAndTE("S0",nil,"a") then
            prio[index]=getHardwareIndex("S0","a",0)
            index=index+1
        end
    end

    out = ""
    for k,v in ipairs(prio) do
        out=out .. v .. " "
    end
    print(out)
end

