-- This is a helper script to check installation of reboot-required 
-- packages.
-- This script runs just after changes are commited to rpm database.

filename = "/usr/lib/update-watch/reboot.list"


-- Function to extract real package name from allow-duplicated packages.
function realname(name)
    local s, e, name = string.find(name, "(.+)#")
    return name
end

-- Open list of packages that requires system reboot,
-- Then read it and store package name into local table.
local reboot = {}

file = io.open(filename, "r")
if not file then
    print("error: can't open configuration file at "..filename)
    return
else
    for line in io.lines(filename) do
        local s, e = string.find(line, "reboot:")
        if e then
            table.insert(reboot, string.sub(line, e+1))
        end
    end
end


-- Check packages that is installed just now, and compare it with
-- extraced list, Then check if reboot is required.
local found = false

for i, pkg in pairs(pkglist()) do
    if statinstall(pkg) then
        local name = realname(pkgname(pkg))
        if not name then
            name = pkgname(pkg)
        end
        for i, name_r in pairs(reboot) do
            if name == name_r then
                found = true
            end
       end
    end
end

if found then
    -- print ("reboot required.")
    os.execute("touch /var/run/reboot-required")
end
