#!/usr/bin/lua

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

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

debug = 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 debug 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 getPrioInitialValue(index)
    if (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Pri1" then
        return "0"
    elseif (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Pri2" then
        return "1"
    elseif (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "S0" then
        return "2"
    elseif (config.struct_Box.string_IsdnClockMasterPriority[index] or "Intern") == "Intern" then
        return "8"
    else
        return "8"
    end
end

-- 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 debug 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]="0"
      index=index+1
   end

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

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

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


