-- This script will clean up all kernels older than current version.
--
-- This script must be plugged in the following slots:
--
--   Scripts::AptGet::Upgrade
--   Scripts::AptGet::DistUpgrade
--   Scripts::Synaptic::Upgrade
--   Scripts::Synaptic::DistUpgrade
--
-- Author: Daisuke SUZUKI <daiuske@linux.or.jp>


if confget("RPM::Remove-Old-Kernels/b", "true") == "false" then
  return
end

-- remove old kernel and kernel-devel
knames = {'kernel', 'kernel%-pae', 'kernel%-smp', 'kernel%-devel', 'kernel%-pae%-devel', 'kernel%-smp%-devel'}

-- get current running kernel version
function get_running_kernel_ver()
    local inp = io.popen("uname -r")
    local currentver = nil
    for line in inp.lines(inp) do
        currentver = string.gsub(line,"pae$","")
    end
    if currentver then
        return currentver
    else
        return 0
    end
end

-- get kernel version of vmlinuz.
function get_current_kernel_ver()
    local inp = io.popen("readlink /boot/vmlinuz")
    local oldver = nil
    for line in inp.lines(inp) do
        oldver = string.gsub(string.gsub(line,"pae$",""),"^vmlinuz%-","")
    end
    if oldver then
        return oldver
    else
        return 0
    end
end

-- get kernel version of vmlinuz.old
function get_old_kernel_ver()
    local inp = io.popen("readlink /boot/vmlinuz.old")
    local oldver = nil
    for line in inp.lines(inp) do
        oldver = string.gsub(string.gsub(line,"pae$",""),"^vmlinuz%-","")
    end
    if oldver then
        return oldver
    else
        return 0
    end
end

function get_latest(kname)
    pkgs = pkglist()
    latest = "0"
    for i, pkg in ipairs(pkgs) do
        if not pkgisvirtual(pkg) and string.find(pkgname(pkg), kname.."#") then
            ver = pkgvercur(pkg)
            if not ver then
                ver = pkgvercand(pkg)
            end
            if ver and verstrcmp(verstr(ver), latest) > 0 then
                latest = verstr(ver)
                latestpkg = pkg
            end
        end
    end
    return latestpkg
end

function get_latest_kernel_ver()
    latestver = nil
    latestver = verstr(pkgvercur(get_latest("kernel")))
    if latestver then
        return latestver
    else
        return 0
    end
end

-- remove all kernels older than current or vmlinuz.old
function remove_old_kernels()
    rkver = get_running_kernel_ver()
    ckver = get_current_kernel_ver()
    lkver = get_latest_kernel_ver()
    okver = get_old_kernel_ver()
    pkgs = pkglist()
    for i, pkg in ipairs(pkgs) do
        ver = pkgvercur(pkg)
	if not pkgisvirtual(pkg) and ver then
            for j, kname in ipairs(knames) do
                if string.find(pkgname(pkg), kname..'#') then
                    if ( verstrcmp(verstr(ver), rkver) < 0 and
                         verstrcmp(verstr(ver), ckver) < 0 and
                         verstrcmp(verstr(ver), lkver) < 0 and
                         verstrcmp(verstr(ver), okver) < 0 ) then
                        markremove(pkg)
                    end
                end
            end
	end
    end
end

-- 
if script_slot == "Scripts::AptGet::DistUpgrade" or
   script_slot == "Scripts::AptGet::Upgrade" or
   script_slot == "Scripts::Synaptic::DistUpgrade" or
   script_slot == "Scripts::Synaptic::Upgrade" then
    remove_old_kernels()
end

-- vim:ts=4:sw=4:et
