#!/bin/zsh

vmlinuz=(/boot/vmlinuz-*(.))
system=(/boot/System.map-*(.))
initrd=(/boot/initrd.img-*(.))
module=(/lib/modules/*(/))
tree=(/usr/src/linux-*(/))

vmlinuz=($(print -l $vmlinuz | sort -V))
keep=(${${:-/boot/vmlinuz}:A} ${${:-/boot/vmlinuz.old}:A} $vmlinuz[-1] $vmlinuz[-2] )
keep=(${(u)keep})

for k in $keep ;  do
    v=${k#/boot/vmlinuz-}

    vmlinuz=(${vmlinuz#/boot/vmlinuz-$v})
    system=(${system#/boot/System.map-$v})
    initrd=(${initrd#/boot/initrd.img-$v})
    module=(${module#/lib/modules/$v})
    tree=(${tree#/usr/src/linux-${v/-gentoo*/-gentoo}})
done


echo "#keeping $keep"
for f in $vmlinuz $system $initrd ; do echo echo rm $f ; echo rm $f ; done
for f in $module $tree ; do echo echo rm $f ; echo rm -r $f ; done


