ed796b5
-- Convenience Lua functions that can be used within rpm macros
ed796b5
ed796b5
-- Set a spec variable
ed796b5
-- Echo the result if verbose
ed796b5
local function explicitset(rpmvar,value,verbose)
ed796b5
  local value = value
ed796b5
  if (value == nil) or (value == "") then
ed796b5
    value = "%{nil}"
ed796b5
  end
ed796b5
  rpm.define(rpmvar .. " " .. value)
ed796b5
  if verbose then
ed796b5
    rpm.expand("%{echo:Setting %%{" .. rpmvar .. "} = " .. value .. "}")
ed796b5
  end
ed796b5
end
ed796b5
ed796b5
-- Unset a spec variable if it is defined
ed796b5
-- Echo the result if verbose
ed796b5
local function explicitunset(rpmvar,verbose)
ed796b5
  if (rpm.expand("%{" .. rpmvar .. "}") ~= "%{" .. rpmvar .. "}") then
ed796b5
    rpm.define(rpmvar .. " %{nil}")
ed796b5
    if verbose then
ed796b5
      rpm.expand("%{echo:Unsetting %%{" .. rpmvar .. "}}")
ed796b5
    end
ed796b5
  end
ed796b5
end
ed796b5
ed796b5
-- Set a spec variable, if not already set
ed796b5
-- Echo the result if verbose
ed796b5
local function safeset(rpmvar,value,verbose)
ed796b5
  if (rpm.expand("%{" .. rpmvar .. "}") == "%{" .. rpmvar .. "}") then
ed796b5
    explicitset(rpmvar,value,verbose)
ed796b5
  end
ed796b5
end
ed796b5
ed796b5
-- Alias a list of rpm variables to the same variables suffixed with 0 (and vice versa)
ed796b5
-- Echo the result if verbose
ed796b5
local function zalias(rpmvars,verbose)
ed796b5
  for _, sfx in ipairs({{"","0"},{"0",""}}) do
ed796b5
    for _, rpmvar in ipairs(rpmvars) do
ed796b5
      local toalias = "%{?" .. rpmvar .. sfx[1] .. "}"
ed796b5
      if (rpm.expand(toalias) ~= "") then
ed796b5
        safeset(rpmvar .. sfx[2], toalias, verbose)
ed796b5
      end
ed796b5
    end
ed796b5
  end
ed796b5
end
ed796b5
ed796b5
-- Echo the list of rpm variables, with suffix, if set
ed796b5
local function echovars(rpmvars, suffix)
ed796b5
  for _, rpmvar in ipairs(rpmvars) do
ed796b5
    rpmvar = rpmvar .. suffix
ed796b5
    local header = string.sub("  " .. rpmvar .. ":                                               ",1,21)
ed796b5
    rpm.expand("%{?" .. rpmvar .. ":%{echo:" .. header .. "%{?" .. rpmvar .. "}}}")
ed796b5
  end
ed796b5
end
ed796b5
ed796b5
-- Returns an array, indexed by suffix, containing the non-empy values of
ed796b5
-- <rpmvar><suffix>, with suffix an integer string or the empty string
ed796b5
local function getsuffixed(rpmvar)
ed796b5
  local suffixes = {}
ed796b5
  zalias({rpmvar})
ed796b5
  for suffix=0,9999 do
ed796b5
    local value = rpm.expand("%{?" .. rpmvar .. suffix .. "}")
ed796b5
    if (value ~= "") then
ed796b5
      suffixes[tostring(suffix)] = value
ed796b5
    end
ed796b5
  end
ed796b5
  -- rpm convention is to alias no suffix to zero suffix
ed796b5
  -- only add no suffix if zero suffix is different
ed796b5
  local value = rpm.expand("%{?" .. rpmvar .. "}")
ed796b5
  if (value ~= "") and (value ~= suffixes["0"]) then
ed796b5
     suffixes[""] = value
ed796b5
  end
ed796b5
  return suffixes
ed796b5
end
ed796b5
ed796b5
-- Returns the list of suffixes, including the empty string, for which
ed796b5
-- <rpmvar><suffix> is set to a non empty value
ed796b5
local function getsuffixes(rpmvar)
ed796b5
  suffixes = {}
ed796b5
  for suffix in pairs(getsuffixed(rpmvar)) do
ed796b5
    table.insert(suffixes,suffix)
ed796b5
  end
ed796b5
  table.sort(suffixes,
ed796b5
             function(a,b) return (tonumber(a) or 0) < (tonumber(b) or 0) end)
ed796b5
  return suffixes
ed796b5
end
ed796b5
ed796b5
-- Returns the suffix for which <rpmvar><suffix> has a non-empty value that
ed796b5
-- matches best the beginning of the value string
ed796b5
local function getbestsuffix(rpmvar, value)
ed796b5
  local best         = nil
ed796b5
  local currentmatch = ""
ed796b5
  for suffix, setvalue in pairs(getsuffixed(rpmvar)) do
ed796b5
  if (string.len(setvalue) > string.len(currentmatch)) and
ed796b5
     (string.find(value, "^" .. setvalue)) then
ed796b5
      currentmatch = setvalue
ed796b5
      best         = suffix
ed796b5
    end
ed796b5
  end
ed796b5
  return best
ed796b5
end
ed796b5
ed796b5
return {
ed796b5
  explicitset   = explicitset,
ed796b5
  explicitunset = explicitunset,
ed796b5
  safeset       = safeset,
ed796b5
  zalias        = zalias,
ed796b5
  echovars      = echovars,
ed796b5
  getsuffixed   = getsuffixed,
ed796b5
  getsuffixes   = getsuffixes,
ed796b5
  getbestsuffix = getbestsuffix,
ed796b5
}