#!/usr/bin/perl

use MDK::Common;
use list_modules;

my $RPMS = '/export/media/main';
my $rpm = 'rpm --nosignature';


my $MOVE = $ARGV[0] eq '--move' && shift;

@ARGV <= 1 or die "usage: ./update_kernel [--move] [<kernel rpm>]\n";


mkdir 'all.kernels'; # make sure "all.kernels" directory exists
eval { rm_rf('all.modules') }; #- not used anymore

my $main = chomp_(cat_('all.kernels/.main'));
my $main_BOOT = chomp_(cat_('all.kernels/.main-BOOT'));

if (@ARGV) {
    install_kernel($ARGV[0]);
} else {
    update_kernel_from_repository($RPMS, '2.6', 0);
    update_kernel_from_repository($RPMS, '2.6', 1);
#    update_kernel_from_repository($RPMS, '2.4', 1);
}


sub system_verbose { print join(' ', @_), "\n"; system(@_) }
sub sys { &system_verbose; $? and die }

sub rpm2version {
    my ($kernel_rpm) = @_;
    `$rpm -qpl $kernel_rpm` =~ m!/boot/vmlinuz-(.*)! && $1 or die "can't find vmlinuz in $kernel_rpm\n";
}

sub update_kernel_from_repository {
    my ($RPMS, $main_ver, $is_BOOT) = @_;
    my $rpm_wildcard = 'kernel-' . ($is_BOOT ? 'BOOT-' : '') . $main_ver . '*.rpm';
    my ($kernel_rpm) = my @kernels = glob("$RPMS/$rpm_wildcard");
    @kernels <= 1 or die "more than one kernel match $rpm_wildcard";
    @kernels >= 1 or die "no kernel match $rpm_wildcard";

    install_kernel($kernel_rpm, 1);
}

sub install_kernel {
    my ($kernel_rpm, $o_update) = @_;

    my $kern_ver = rpm2version($kernel_rpm);
    my $dir = "all.kernels/$kern_ver";

    -d $dir and return if $o_update;

    my $is_BOOT = $kern_ver =~ /BOOT/;
    my ($main_ver) = $kern_ver =~ /^(2\.\d)/;
    if (my @previous = grep { ($is_BOOT xor !/BOOT/) && /^\Q$main_ver/ } all('all.kernels')) {
	warn "Removing previous kernels ", join(' ', @previous), "\n";
        rm_rf("all.kernels/$_") foreach @previous;
    }

    if (!$main || !-d "all.kernels/$main") {
	$main = $kern_ver;
	output('all.kernels/.main', "$main\n");
    }
    if ($is_BOOT && (!$main_BOOT || !-d "all.kernels/$main_BOOT")) {
	$main_BOOT = $kern_ver;
	output('all.kernels/.main-BOOT', "$main_BOOT\n");
    }

    warn "Installing kernel $kern_ver\n";

    eval { rm_rf($dir) };
    mkdir_p("$dir/modules");
    sys("rpm2cpio $kernel_rpm | (cd $dir ; cpio -id)");

    rename "$dir/boot/vmlinuz-$kern_ver", "$dir/vmlinuz" or die "can't find vmlinuz\n";

    sys("find $dir -name '*.gz' | xargs gunzip");

    my $ext = module_extension($kern_ver);

    open(my $F, "find $dir -name '*.$ext' |");
    my $file; while ($file = <$F>) {
	chomp($file);
	rename $file, "$dir/modules/" . basename($file) or warn "conflict for $file\n";
    }

    if (kernel_is_26($kern_ver)) {
        warn "no stripping on 2.6 since it breaks modules\n";
    } else {
        print STDERR "stripping $kern_ver: ";
        sys("./strip_modules $dir/modules/*.$ext");
        print STDERR "done\n";
    }

    sys('perl', 'modules.pl', 'remove_unneeded_modules', $kern_ver) if $kern_ver !~ /BOOT/;

    sys('perl', 'modules.pl', 'make_modules_per_image', $kern_ver);
    sys('perl', 'modules.pl', 'make_modules_description', $kern_ver) if $kern_ver eq $main;

    if (!$MOVE) {
        print STDERR "packdrake $kern_ver: ";
        sys("cd $dir/modules ; ls *.$ext | packdrake -b9s ../modules.cz 400000");
        print STDERR "done\n";
    }

    eval { rm_rf("$dir$_") } foreach qw(/boot /lib /usr /modules);
}