use install2;
use fs;
use commands;
use detect_devices;
use devices;
use run_program;
use modules;
use list_modules;
use log;

log::l("Installing viaraid patch for Mandrake 9.2");

package detect_devices;
# New ID of your controllers (provided by your driver)
$detect_devices::pcitable_addons = <<'EOF';
# add here lines conforming the pcitable format (0xXXXX\t0xXXXX\t"\w+"\t".*")
0x1106	0x3249	"viamraid"	"VIA Technologies| VT6421 Raid Adapter"
0x1106	0x3164	"viamraid"	"VIA Technologies| VT6410 ATA Raid Adapter"
0x1106	0x3149	"viamraid"	"VIA Technologies| VT6420/8237 SATA Raid Adapter"
EOF

package list_modules;
$l{disk}{'hardware_raid'} = [
      if_(arch() =~ /^sparc/, qw(pluto)),
      if_(arch() !~ /alpha/ && arch() !~ /sparc/,
        qw(DAC960 dpt_i2o megaraid aacraid cciss cpqarray gdth i2o_block),
	qw(cpqfc qla2200 qla2300 ataraid hptraid silraid pdcraid pdc-ultra),
        qw(ips ppa imm),
# Add your module(s) here like viamraid (make sure that your modules are on pcitable module field)
	qw(viamraid),
      ),
			     ];
package install2;
undef *setupSCSI;
*setupSCSI = sub {
    my ($clicked, $_ent_number, $auto) = @_;

    if (!$::live && !$::g_auto_install && !$o->{blank} && !$::testing && !$::uml_install) {
	-s modules::cz_file() or die \N("Can't access kernel modules corresponding to your kernel (file %s is missing), this generally means your boot floppy in not in sync with the Installation medium (please create a newer boot floppy)", modules::cz_file());
    }

# add-on part
    my $floppy = detect_devices::floppy();
    fs::mount(devices::make($floppy), "/floppy", 'vfat', 0);
    commands::cp("/floppy/viamraid_BOOT.o", "/tmp/viamraid.o");    
    commands::cp("/floppy/viamraid_up.o", "/tmp/viamraid_up.o");    
    fs::umount('/floppy');
    log::l("Insmoding viamraid module\n");
    run_program::run(["/usr/bin/modprobe", "modprobe"], "sd_mod");    
    run_program::run(["/usr/bin/insmod_", "insmod"], '-f', "/tmp/viamraid.o");    

    installStepsCall($o, $auto, 'setupSCSI', $clicked);
};

undef *installPackages;
*installPackages = sub {
    my ($_clicked, $ent_number, $auto) = @_;

    installStepsCall($o, $auto, 'readBootloaderConfigBeforeInstall') if $ent_number == 1;

    installStepsCall($o, $auto, 'beforeInstallPackages');
    installStepsCall($o, $auto, 'installPackages');
    installStepsCall($o, $auto, 'afterInstallPackages');

# Copy modules on a right place
    log::l("Installing viamraid modules\n");
    my $kver="2.4.22-10mdk";
    system("gzip -c /tmp/viamraid_up.o > $o->{prefix}/lib/modules/${kver}/kernel/drivers/scsi/viamraid.o.gz");
};

use bootloader;
package bootloader;
undef *mkinitrd;
*mkinitrd = sub {
    my ($kernelVersion, $initrdImage) = @_;

    $::testing || -e "$::prefix/$initrdImage" and return 1;

    my $loop_boot = loopback::prepare_boot();

    modules::load('loop');
    run_program::rooted($::prefix, "depmod", "-a", "-F", "/boot/System.map-" . $kernelVersion, $kernelVersion);
    if (!run_program::rooted($::prefix, "mkinitrd", "-v", "-f", $initrdImage, "--ifneeded", $kernelVersion)) {
	unlink("$::prefix/$initrdImage");
	die "mkinitrd failed";
    }
    loopback::save_boot($loop_boot);

    -e "$::prefix/$initrdImage";
};
