# kernel selection and installation -*-perl-*-

$kernel_root = "images";
$kernel_version_file = "version.idx";
$kernel_index_file = "image.idx";

$kernel_version_text = "";
$kernel_version_dir = "";
$kernel_version = "";

sub install_kernel {

    local ( $kernel );

    $res = 0;

    # Now we always copy the kernel from the boot floppy
    &mount_bootdisk();

    # the boot floppy should already be mounted
    &rhs_infobox("Installing Kernel",
<<EOM
>
Copying kernel from boot floppy.
>
EOM
		 , 60);

    $res += &invoke("cp -f /bootdisk/vmlinuz $fsmount/vmlinuz");

    $res += &invoke("rdev $fsmount/vmlinuz $root_dev");
    # Standard video mode
    $res += &invoke("rdev -v $fsmount/vmlinuz -1");
    # Mount / read-only
    $res += &invoke("rdev -R $fsmount/vmlinuz 1");
    # No ramdisk
    $res += &invoke("rdev -r $fsmount/vmlinuz 0");

    if ($res) {
	&rhs_msgbox("Error",
<<EOM
>
The kernel was NOT installed properly.
>
Are you sure the BOOT floppy is inserted?
>
EOM
		    , 60);
	return 0;
    } else {
#	if ($dev_series_selected) {
#	    &install_kernel_sources;
#	}
	return 1;
    }
  
    &umount_bootdisk();
}

sub select_kernel_version {
    local ( @version_text, %version_text_to_dir );
    local ( $dir, $version, $text );
    local ( $max_version_index );

    @version_text = ();
    %version_text_to_dir = ();

    $max_version_index = "";

    open(FD, "$rhspath/$kernel_root/$kernel_version_file");
    while (<FD>) {
	chop;
	($dir, $version, $text) = split(/\s+/, $_, 3);
	if ((! $max_version_index) ||
	    ($version_text_to_dir{$max_version_index} < $dir)) {
	    $max_version_index = $text;
	}
	$version_text_to_dir{$text} = $dir;
	$version_text_to_version{$text} = $version;
	$version_text[@version_text] = $text;
    }
    close(FD);

    if ($express_install) {
	$dialog_result = $max_version_index;
    } else {
	if (! &rhs_menul("Kernel Version",
<<EOM
>
Please select a kernel version.
>
EOM
			 , 70, scalar(@version_text), @version_text)) {
	    return 0;
	}
    }

    $kernel_version_text = $dialog_result;
    $kernel_version_dir = $version_text_to_dir{$dialog_result};
    $kernel_version = $version_text_to_version{$dialog_result};

    return 1;
}

sub install_kernel_sources {

    local ( $rpp, $rhsmountpath_save );

    if (! $express_install) {
	if (! &rhs_yesno("Notice",
<<EOM
>
You have installed a kernel from your boot floppy.
If this is your own custom kernel, and is a different
version than is available on the Red Hat CD, you will
not be able to install the kernel sources at this time.
>
Do you wish to install kernel sources (you will have
to choose a version)?
>
EOM
			 , 50)) {
	    return 0;
	}
    }

    # select kernel version
    if (! &select_kernel_version) {
	return 0;
    }

    &rhs_infobox("Installing Kernel Sources",
<<EOM
>
Installing sources for Linux kernel version $kernel_version.
>
This may take a minute or two...
>
EOM
		 , 60);

    # install kernel

    # Move the RHS stuff filesystem to a place under the
    # installation filesystem
    $rhsmountpath_save = $rhsmountpath;
    &_remount_rhs ("$fsmount/$install_rhs_mount_point");

    $rpp = "linux-$kernel_version-src.rpp";
#    &invoke("cp -f $rhspath/$rpppath/$rpp $fsmount/tmp");

    if ($pid = fork) {
	waitpid($pid, 0);
    } else {
	chdir $fsmount;
	chroot $fsmount;
	&invoke("rpp-install /$install_rhs_mount_point/$rpppath/$rpp");
	exit(0);
    }

#    unlink("$fsmount/tmp/$rpp");

    # make symlink
    chdir("$fsmount/etc");
    symlink("../usr/src/kernel/linux-$kernel_version", "linux-source");

    chdir("/");
    &_remount_rhs($rhsmountpath_save);

    return 1;
}

##########################
1;
