#!/usr/bin/perl

# this script takes kernels rpms from RPMS/ and create various files in all_kernels/
# for each rpms in RPMS, it does:
#   RPMS/kernel-VER-*.rpm -> 
#     all.kernels/VER/all_modules.tar  (used for all.rdz)
#     all.kernels/VER/*.mar            (used for other rdz)
#     all.kernels/VER/modules.cz       (used for stage2)
#     all.kernels/VER/modules.dep
#     all.kernels/VER/vmlinuz
#
# if RPMS is empty, this script tries to find kernels in $main_repository

use MDK::Common;
use list_modules;

my $main_repository = '/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";

eval { rm_rf('all.kernels') }; #- remove old things
mkdir 'all.kernels';

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

if (@ARGV) {
    install_kernel($ARGV[0]);
} elsif (! -d 'RPMS') {
    update_kernel_from_repository($main_repository, '2.6', 0);
    update_kernel_from_repository($main_repository, '2.6', 1);
    install_kernel(glob("$main_repository/kernel-xbox*"), 1) if arch() =~ /i.86/;
}

extract_kernel($_) foreach glob("RPMS/*.rpm");

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 ($repository, $main_ver, $is_BOOT) = @_;
    my $rpm_wildcard = 'kernel-' . ($is_BOOT ? 'BOOT-' : (arch() =~ /i.86/ ? 'i586-up-1GB-' : '')) . $main_ver . '*.rpm';
    my @kernels = glob("$repository/$rpm_wildcard");
    
    install_kernel($_, 1) foreach @kernels;
}

sub install_kernel {
    my ($kernel_rpm) = @_;
    my $basename = basename($kernel_rpm);
    warn "Installing rpm $basename in RPMS\n";
    mkdir 'RPMS';
    cp_af($kernel_rpm, "RPMS/$basename");

    if (!$main) {
	$main = rpm2version($kernel_rpm);
	output('RPMS/.main', "$main\n");
    }
    if (!$main_BOOT && $kernel_rpm =~ /BOOT/) {
	$main_BOOT = rpm2version($kernel_rpm);
	output('RPMS/.main-BOOT', "$main_BOOT\n");
    }
}

sub extract_kernel {
    my ($kernel_rpm) = @_;

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

    warn "Extracting 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);
}