#! /usr/bin/env texlua
-- texlive/tlpdb.tlu
-- $Id: tlpdb.tlu 9699 2008-07-22 08:58:59Z preining $
-- 
-- Copyright (C) 2008 Norbert Preining
-- This file is licensed under the GNU General Public License version 2
-- or any later version.
--
-- TODO: tag evaluation of docfiles lines ...
-- 
-- providing the tlpdb as a lua table
--
-- format
--  tlpobj = tlpdb[packagename]
--  tlpobj.name string
--  tlpobj.revision string
--  tlpobj.category string
--  tlpobj.shortdesc string
--  tlpobj.longdesc string
--  tlpobj.catalogue string
--  tlpobj.cataloguedata table indexed by various catalogue values
--  tlpobj.containersize string
--  tlpobj.srccontainersize string
--  tlpobj.doccontainersize string
--  tlpobj.containermd5 string
--  tlpobj.srccontainermd5 string
--  tlpobj.doccontainermd5 string
--  tlpobj.binfiles table indexed by archs giving table indexed by numbers
--  tlpobj.binsize table indexed by archs giving strings
--  tlpobj.docfiles = table indexed by numbers
--  tlpobj.docfiledata[docfile][tag] = value
--  tlpobj.docsize string
--  tlpobj.srcfiles = table indexed by numbers
--  tlpobj.srcsize string
--  tlpobj.runfiles = table indexed by numbers
--  tlpobj.runsize string
--  tlpobj.depend = table indexed by numbers
--  tlpobj.execute = table indexed by numbers
--
--*-Lua-*-

function read_tlpdb(root)
  local fn = root..'/tlpkg/texlive.tlpdb'
  if (lfs.isfile(fn)) then -- tlpdb exists
    local started = false
    local lastcmd = ""
    local arch
    local size
    local line
    local tlpobj = {}
    local name
    local tlpdb = {}
    for line in io.lines(fn) do
      if string.match(line, '^%s*$') then
        if started then
          -- we have encountered an empty line while creating a tlpobj, so
          -- stop that tlpobj, add it to the tlpdb, and restart
          --tlpdb[name] = tlpobj
          tlpdb[name] = tlpobj
          tlpobj = {}
          --print ("adding "..name.." to tlpdb")
          started = false
        end
      else
        if string.match(line, '^ ') then
          if lastcmd == 'runfiles' or
             lastcmd == 'binfiles' or
             lastcmd == 'docfiles' or
             lastcmd == 'srcfiles' or
             lastcmd == 'execute' or
             lastcmd == 'depend' then
            line = string.gsub(line, '^ ',  lastcmd.."continued ")
          else
            io.stderr:write('Error reading tlpdb: continuation of '..lastcmd..' not allowed in line\n>>'..line..'<<\n')
            os.exit(1)
          end
        end
        local first
        local rest
        first, rest = string.match(line,'^(%S+)%s*(.*)%s*$')
        --print ("DEBUG: line = "..line)
        --print ("DEBUG: first = "..first)
        lastcmd = first
        if first == "name" then
          tlpobj.name = rest
          name = rest
          started = true
        elseif first == "revision" then
          tlpobj.revision = rest
        elseif first == "category" then
          tlpobj.category = rest
        elseif first == "shortdesc" then
          tlpobj.shortdesc = rest
        elseif first == "catalogue" then
          tlpobj.catalogue = rest
        elseif string.match(first,'^catalogue%-') then
          --local data = string.match(first,'^catalogue-(.*)$')
          local cdata = string.gsub(first,'^catalogue%-', '')
          --print("DEBUG: cdata = "..cdata..", first = "..first)
          if not(tlpobj.cataloguedata) then
            tlpobj.cataloguedata = {}
          end
          tlpobj.cataloguedata[cdata] = rest
        elseif first == "containersize" then
          tlpobj.containersize = rest
        elseif first == "srccontainersize" then
          tlpobj.srccontainersize = rest
        elseif first == "doccontainersize" then
          tlpobj.doccontainersize = rest
        elseif first == "containermd5" then
          tlpobj.containermd5 = rest
        elseif first == "srccontainermd5" then
          tlpobj.srccontainermd5 = rest
        elseif first == "doccontainermd5" then
          tlpobj.doccontainermd5 = rest
        elseif first == "runfiles" then
          local rsize = string.match(rest,'size=([0-9]*)')
          if (rsize) then
            tlpobj.runsize = rsize
          else
            if #rest > 0 then
              io.stderr:write('Unknown tag in line\n>>'..line.."<<\n")
              os.exit(1)
            end
          end
        elseif first == "docfiles" then
          local dsize = string.match(rest,'size=([0-9]*)')
          if (dsize) then
            tlpobj.docsize = dsize
          else
            if #rest > 0 then
              io.stderr:write('Unknown tag in line\n>>'..line.."<<\n")
              os.exit(1)
            end
          end
        elseif first == "srcfiles" then
          local ssize = string.match(rest,'size=([0-9]*)')
          if (ssize) then
            tlpobj.srcsize = ssize
          else
            if #rest > 0 then
              io.stderr:write('Unknown tag in line\n>>'..line.."<<\n")
              os.exit(1)
            end
          end
        elseif first == "binfiles" then
          arch = ""
          local barch
          local bsize
          for aaa in string.gmatch(rest,'%S+') do
            if string.match(aaa,'^arch=(%S*)$') then
              barch = string.match(aaa,'^arch=(%S*)$')
            elseif string.match(aaa,'^size=(%S*)$') then
              bsize = string.match(aaa,'^size=(%S*)$')
            else
              io.stderr:write('Unknown tag in line\n>>'..line.."<<\n")
              os.exit(1)
            end
          end
          if barch and bsize then
            if not(tlpobj.binsize) then
              tlpobj.binsize = {}
            end
            tlpobj.binsize[barch] = bsize
            arch = barch
          else
            io.stderr:write('Incomplete binfile tags in line\n>>'..line.."<<\n")
            os.exit(1)
          end
        elseif first == "runfilescontinued" then
          if not(tlpobj.runfiles) then
            tlpobj.runfiles = {}
          end
          table.insert(tlpobj.runfiles,rest)
          lastcmd = "runfiles"
        elseif first == "docfilescontinued" then
          local dfile,tags = string.match(rest,'^(%S+)%s*(.*)%s*$')
          if not(tlpobj.docfiles) then
            tlpobj.docfiles = {}
          end
          table.insert(tlpobj.docfiles,dfile)
          for aaa,bbb in string.gmatch(tags,'(%S+)=(%b"")') do
            if not(tlpobj.docfiledata) then
              tlpobj.docfiledata = {}
            end
            if aaa == "details" or aaa == "language" then
              if not(tlpobj.docfiledata[dfile]) then
                tlpobj.docfiledata[dfile] = {}
              end
              tlpobj.docfiledata[dfile][aaa] = bbb
            else
              io.stderr:write('Error reading tlpdb: unknown docfile tag in line\n>>'..line.."<<\n")
              os.exit(1)
            end
          end
          lastcmd = "docfiles"
        elseif first == "srcfilescontinued" then
          if not(tlpobj.srcfiles) then
            tlpobj.srcfiles = {}
          end
          table.insert(tlpobj.srcfiles,rest)
          lastcmd = "srcfiles"
        elseif first == "binfilescontinued" then
          if not(tlpobj.binfiles) then
            tlpobj.binfiles = {}
          end
          if not(tlpobj.binfiles[arch]) then
            tlpobj.binfiles[arch] = {}
          end
          table.insert(tlpobj.binfiles[arch],rest)
          lastcmd = "binfiles"
        elseif first == "depend" then
          if not(tlpobj.depend) then
            tlpobj.depend = {}
          end
          table.insert(tlpobj.depend,rest)
        elseif first == "longdesc" then
          if tlpobj.longdesc then
            tlpobj.longdesc = tlpobj.longdesc.." "..rest
          else
            tlpobj.longdesc = rest
          end
        elseif first == "execute" then
          if not(tlpobj.execute) then
            tlpobj.execute = {}
          end
          table.insert(tlpobj.execute,rest)
        else
          print("first = "..first)
          io.stderr:write('Error reading tlpdb: unknown directive in line\n>>'..line.."<<\n")
          os.exit(1)
        end
        if not(first) then
          io.stderr:write('Error reading tlpdb: missing directive in line\n>>'..line.."<<\n")
          os.exit(1)
        end
      end
    end
    return tlpdb
  else
    print (fn..' not found.')
  end
end


-- Local Variables:
-- lua-indent-level: 2
-- tab-width: 2
-- indent-tabs-mode: nil
-- End:
-- vim:set tabstop=2 expandtab: #
