#
# Taken from rpm/macros.in
#

# Add a sysuser user/group to a package. Takes a sysusers.d(5) line as
# arguments, eg `%add_sysuser g mygroup 515`.
# -b option omits the "Provides: " to support formatting the entry outside
# spec context.
%add_sysuser(-) %{lua:
    if arg[1] == '-b' then
        prefix = ''
        table.remove(arg, 1)
    else
        prefix = 'Provides: '
    end
    if #arg < 2 then
        error('not enough arguments')
    end
    if arg[1] == 'g' then
        type = 'group'
    elseif arg[1] == 'u' then
        type = 'user'
    else
        error('invalid sysuser type: '..arg[1])
    end
    name = arg[2]
    line = table.concat(arg, ' ')
    -- \0-pad source string to avoid '=' in the output
    llen = line:len()
    ulen = math.ceil(4 * (llen / 3))
    plen = 4 * math.ceil(llen / 3)
    pad = string.rep('\\0', plen-ulen)
    enc = rpm.b64encode(line..pad, 0);

    print(string.format('%s%s(%s) = %s\\n', prefix, type, name, enc))
    if type == 'user' then
        print(string.format('%s%s(%s)\\n', prefix, 'group', name))
    end
}
