c70110c
-- Convenience Lua functions that can be used within rpm macros
c70110c
c70110c
-- Set a spec variable
c70110c
-- Echo the result if verbose
353f8b9
local function explicitset(rpmvar, value, verbose)
c70110c
  local value = value
c70110c
  if (value == nil) or (value == "") then
c70110c
    value = "%{nil}"
c70110c
  end
c70110c
  rpm.define(rpmvar .. " " .. value)
c70110c
  if verbose then
c70110c
    rpm.expand("%{echo:Setting %%{" .. rpmvar .. "} = " .. value .. "}")
c70110c
  end
c70110c
end
c70110c
c70110c
-- Unset a spec variable if it is defined
c70110c
-- Echo the result if verbose
353f8b9
local function explicitunset(rpmvar, verbose)
c70110c
  if (rpm.expand("%{" .. rpmvar .. "}") ~= "%{" .. rpmvar .. "}") then
c70110c
    rpm.define(rpmvar .. " %{nil}")
c70110c
    if verbose then
c70110c
      rpm.expand("%{echo:Unsetting %%{" .. rpmvar .. "}}")
c70110c
    end
c70110c
  end
c70110c
end
c70110c
c70110c
-- Set a spec variable, if not already set
c70110c
-- Echo the result if verbose
353f8b9
local function safeset(rpmvar, value, verbose)
c70110c
  if (rpm.expand("%{" .. rpmvar .. "}") == "%{" .. rpmvar .. "}") then
c70110c
    explicitset(rpmvar,value,verbose)
c70110c
  end
c70110c
end
c70110c
c70110c
-- Alias a list of rpm variables to the same variables suffixed with 0 (and vice versa)
c70110c
-- Echo the result if verbose
353f8b9
local function zalias(rpmvars, verbose)
c70110c
  for _, sfx in ipairs({{"","0"},{"0",""}}) do
c70110c
    for _, rpmvar in ipairs(rpmvars) do
c70110c
      local toalias = "%{?" .. rpmvar .. sfx[1] .. "}"
c70110c
      if (rpm.expand(toalias) ~= "") then
c70110c
        safeset(rpmvar .. sfx[2], toalias, verbose)
c70110c
      end
c70110c
    end
c70110c
  end
c70110c
end
c70110c
353f8b9
-- Takes a list of rpm variable roots and a suffix and alias current<root> to
353f8b9
-- <root><suffix> if it resolves to something not empty
353f8b9
local function setcurrent(rpmvars, suffix, verbose)
353f8b9
  for _, rpmvar in ipairs(rpmvars) do
353f8b9
    if (rpm.expand("%{?" .. rpmvar .. suffix .. "}") ~= "") then
353f8b9
      explicitset(  "current" .. rpmvar, "%{" .. rpmvar .. suffix .. "}", verbose)
353f8b9
    else
353f8b9
      explicitunset("current" .. rpmvar,                                  verbose)
353f8b9
    end
353f8b9
  end
353f8b9
end
353f8b9
c70110c
-- Echo the list of rpm variables, with suffix, if set
c70110c
local function echovars(rpmvars, suffix)
c70110c
  for _, rpmvar in ipairs(rpmvars) do
c70110c
    rpmvar = rpmvar .. suffix
c70110c
    local header = string.sub("  " .. rpmvar .. ":                                               ",1,21)
c70110c
    rpm.expand("%{?" .. rpmvar .. ":%{echo:" .. header .. "%{?" .. rpmvar .. "}}}")
c70110c
  end
c70110c
end
c70110c
c70110c
-- Returns an array, indexed by suffix, containing the non-empy values of
c70110c
-- <rpmvar><suffix>, with suffix an integer string or the empty string
c70110c
local function getsuffixed(rpmvar)
c70110c
  local suffixes = {}
c70110c
  zalias({rpmvar})
c70110c
  for suffix=0,9999 do
c70110c
    local value = rpm.expand("%{?" .. rpmvar .. suffix .. "}")
c70110c
    if (value ~= "") then
c70110c
      suffixes[tostring(suffix)] = value
c70110c
    end
c70110c
  end
c70110c
  -- rpm convention is to alias no suffix to zero suffix
c70110c
  -- only add no suffix if zero suffix is different
c70110c
  local value = rpm.expand("%{?" .. rpmvar .. "}")
c70110c
  if (value ~= "") and (value ~= suffixes["0"]) then
c70110c
     suffixes[""] = value
c70110c
  end
c70110c
  return suffixes
c70110c
end
c70110c
c70110c
-- Returns the list of suffixes, including the empty string, for which
c70110c
-- <rpmvar><suffix> is set to a non empty value
c70110c
local function getsuffixes(rpmvar)
c70110c
  suffixes = {}
c70110c
  for suffix in pairs(getsuffixed(rpmvar)) do
c70110c
    table.insert(suffixes,suffix)
c70110c
  end
c70110c
  table.sort(suffixes,
c70110c
             function(a,b) return (tonumber(a) or 0) < (tonumber(b) or 0) end)
c70110c
  return suffixes
c70110c
end
c70110c
c70110c
-- Returns the suffix for which <rpmvar><suffix> has a non-empty value that
c70110c
-- matches best the beginning of the value string
c70110c
local function getbestsuffix(rpmvar, value)
c70110c
  local best         = nil
c70110c
  local currentmatch = ""
c70110c
  for suffix, setvalue in pairs(getsuffixed(rpmvar)) do
c70110c
  if (string.len(setvalue) > string.len(currentmatch)) and
c70110c
     (string.find(value, "^" .. setvalue)) then
c70110c
      currentmatch = setvalue
c70110c
      best         = suffix
c70110c
    end
c70110c
  end
c70110c
  return best
c70110c
end
c70110c
5335ee5
-- https://github.com/rpm-software-management/rpm/issues/581
5335ee5
-- Writes the content of a list of rpm variables to a macro spec file.
5335ee5
-- The target file must contain the corresponding anchors.
5335ee5
-- For example writevars("myfile", {"foo","bar"}) will replace:
5335ee5
--   @@FOO@@ with the rpm evaluation of %{foo} and
5335ee5
--   @@BAR@@ with the rpm evaluation of %{bar}
5335ee5
-- in myfile
68a8a3b
local function writevars(macrofile, rpmvars)
5335ee5
  for _, rpmvar in ipairs(rpmvars) do
5335ee5
    print("sed -i 's\029" .. string.upper("@@" .. rpmvar .. "@@") ..
5335ee5
                   "\029" .. rpm.expand(  "%{" .. rpmvar .. "}" ) ..
5335ee5
                   "\029g' " .. macrofile .. "\n")
5335ee5
  end
5335ee5
end
5335ee5
c70110c
return {
c70110c
  explicitset   = explicitset,
c70110c
  explicitunset = explicitunset,
c70110c
  safeset       = safeset,
c70110c
  zalias        = zalias,
353f8b9
  setcurrent    = setcurrent,
c70110c
  echovars      = echovars,
c70110c
  getsuffixed   = getsuffixed,
c70110c
  getsuffixes   = getsuffixes,
c70110c
  getbestsuffix = getbestsuffix,
5335ee5
  writevars     = writevars,
c70110c
}