#!/usr/bin/perl

use Config;
use MDK::Common;
Config->import;
my ($arch) = $Config{archname} =~ /(.*?)-/;

# move stuff to new "kernel" directory
-d 'kernel' or die "directory kernel is missing\n";
rename 'all.kernels', 'kernel/all.kernels';
rename 'all.modules', 'kernel/all.modules';

my $default_append = "ramdisk_size=128000 root=/dev/ram3";
# full acpi support for amd64, enough acpi support for x86 ht, no acpi for others
my $default_acpi = $arch =~ /i.86/ ? "acpi=ht" : $arch =~ /x86_64/ ? "acpi=off" : "acpi=off";
my $default_vga = "vga=788";

my $instdir = "mdk-stage1";
my $tmp_mnt = '/tmp/drakx_mnt';
my $tmp_mnt_initrd = '/tmp/drakx_mnt2';
my $mke2fs = "/sbin/mke2fs -q -m 0 -F -s 1";

my $sudo;
if ($>) {
    $sudo = "sudo";
    $ENV{PATH} = "/sbin:/usr/sbin:$ENV{PATH}";
}

sub __ { print @_, "\n"; system(@_) }
sub _ { __ @_; $? and die }

_ "$sudo mkdir -p $tmp_mnt $tmp_mnt_initrd";
mkdir "images";

chomp(my $main = `cat kernel/all.kernels/.main`);
my @kernels = grep { /^2/ } all('kernel/all.kernels');


my @all_images = (
		  if_($arch =~ /i.86/, 'cdrom.img', 'cdrom-changedisk.img', 'pcmcia.img', 'isolinux', 'boot.iso', 'hd_grub.img', 'network.img', 'network_drivers.img'),
		  if_($arch =~ /x86_64/, 'cdrom.img', 'isolinux', 'boot.iso'),
		  if_($arch =~ /ia64/, 'all.img'),
		 );

my @images = @ARGV ? @ARGV : map { "images/$_" } @all_images;

foreach my $img (@images) {
    my ($type, $I, $extension) = $img =~ m!([^/]*)(64)?\.([^.]*)$!;

    if ($img =~ /hd_grub/) {
	hd_grub($tmp_mnt, $img);
    } elsif ($img =~ /isolinux/) {
	isolinux($main, @kernels);

	if (my ($tftpboot) = grep { -e $_ } qw(/tftpboot /var/lib/tftpboot)) {
	    system("/bin/cp -f isolinux/alt0/* $tftpboot 2>/dev/null");
	}
    } elsif ($img =~ /boot.iso/) {
	boot_iso($img);
    } elsif ($img =~ /drivers/) {
	drivers($type, $I, "$img-$_") foreach @kernels;
	rename("$img-$main", $img);
    } elsif ($extension eq 'rdz') {
	initrd($tmp_mnt_initrd, $type, $I, "$img-$_") foreach @kernels;
    } elsif ($extension eq 'img') {
	print STDERR "calling boot_img_$arch for $img\n";
	$::{"boot_img_$arch"}->($tmp_mnt, $type, $I, "$img-$_", glob("kernel/all.kernels/$_/boot/vmlinu*")) foreach @kernels;
	rename("$img-$main", $img);
    } else {
	die "unknown image $img";
    }
}

sub install_stripped { _ "strip $_[0]"; _ "$sudo install $_[0] $_[1]" }

sub initrd {
    my ($mnt, $type, $I, $img) = @_;
    my $tmp = "$ENV{HOME}/tmp/initrd";
    my $tar = "$instdir/stage1-data/stage1.tar.bz2";

    __ "$sudo umount $tmp $mnt 2>/dev/null";
    _ "dd if=/dev/zero of=$tmp bs=1k count=" . ($arch =~ /ia64/ ? ($type eq "all" ? 16386 : 16384) : ($type eq "all" ? 5000 : 2000));
    _ "$mke2fs $tmp";
    _ "$sudo mount -t ext2 $tmp $mnt -o loop";

    _ "$sudo tar xjC $mnt -f $tar";
    symlinkf "/tmp/stage2/lib64", "$mnt/lib64" if $arch =~ /x86_64/;

    {
	my $install = ${{
	    network => "stage1-network",
	    network_gigabit => "stage1-network",
	    network_usb => "stage1-network-usb",
	    network_gigabit_usb => "stage1-network-usb",
	    cdrom => "stage1-cdrom",
	    hd => "stage1-disk",
	    hdcdrom_usb => "stage1-medias-usb",
	}}{$type} || 'stage1-full';
	install_stripped("$instdir/init", "$mnt/sbin");
	install_stripped("$instdir/$install", "$mnt/sbin/stage1");
    }

    if (member($type, qw(network network_gigabit_usb all blank))) {
	install_stripped("$instdir/ppp/pppd-bin", "$mnt/sbin/pppd");
	install_stripped("$instdir/rp-pppoe/pppoe-bin", "$mnt/sbin/pppoe");
	_ "$sudo mknod $mnt/dev/ppp c 108 0";
	_ "$sudo mknod $mnt/dev/ptyp0 c 2 0";
	_ "$sudo mknod $mnt/dev/ttyp0 c 3 0";
    }

    if (member($type, qw(pcmcia all network)) && $arch !~ /ppc|ia64|x86_64/) {
	 _ "$sudo cp -a /etc/pcmcia $mnt/etc";
	 _ "cp $mnt/etc/pcmcia/config /tmp/pcmcia_config_tmp";
	 _ "tools/patch_pcmcia_config.pl /tmp/pcmcia_config_tmp kernel/all.modules/$main/modules.dep";
	 _ "sudo mv /tmp/pcmcia_config_tmp $mnt/etc/pcmcia/config";
    }
    my ($ext) = $img =~ /rdz-(.*)/ or die "bad initrd name ($img)";
    my $modz = "kernel/all.modules$I/$ext";
    _ "$sudo cp -f $modz/${type}_modules.mar $mnt/modules/modules$I.mar";
    _ "$sudo cp -f $modz/modules.dep $mnt/modules/";
    _ "$sudo umount $mnt";

# Workaround for vfat-loop bug (quite touchy)
    _ "gzip -9f $tmp";
    _ "cp -f $tmp.gz $img";
    _ "rm -f $tmp.gz";
#    _ "gzip -9 -c $tmp > $img";
#    _ "rm -f $tmp";
}

sub entries_append {
    my ($type) = @_;

    my $automatic = "";
    $automatic = "automatic=method:cdrom" if $type eq "cdrom";
    $automatic = "automatic=method:disk" if $type eq "hd";

    my @simple_entries = (
	linux => $default_vga,
	vgalo => "vga=785",
	vgahi => "vga=791",
	vga16 => "vga16",
	text => "text",
	patch => "patch $default_vga",
	expert => "expert $default_vga",
	rescue => "rescue rw",
    );
    my @entries = (
        (map { $_->[0] => "$automatic $default_acpi $_->[1]" } group_by2(@simple_entries)),
	acpi => "$automatic $default_vga",
	if_(member($type, "cdrom", "all"), oem => "automatic=method:cdrom $default_vga $default_acpi rescue oem rw",),
        if_($type eq "all", all => "pcmcia $default_vga $default_acpi"),
    );

    map { [ $_->[0], "$default_append $_->[1]" ] }
      group_by2(@entries);
}

sub boot_img_i386 {
    my ($mnt, $type, $I, $img, $kernel) = @_;

    __ "$sudo umount $mnt 2>/dev/null";

    if ($type eq "hd") {
	_ "bunzip2 -c $instdir/init-data/msgboot.img.bz2 > $img";
    } elsif ($type eq "all") {
	_ "bunzip2 -c $instdir/init-data/msgboot-graphicallogo-2880.img.bz2 > $img";
    } else {
	_ "bunzip2 -c $instdir/init-data/msgboot-graphicallogo.img.bz2 > $img";
    }


    _ "$sudo mount -t vfat -o umask=0 $img $mnt -o loop";
    _ "cat $kernel > $mnt/vmlinuz";
    bmp_to_msg('isolinux-graphic-simple.bmp', "$mnt/boot.msg", 27, 27);

    (my $rdz = $img) =~ s/\.img/.rdz/;
    (my $initrd_type = $type) =~ s/-changedisk//;
    initrd($tmp_mnt_initrd, $initrd_type, $I, $rdz);
    my $short_type = substr($type, 0, 8);
    eval { _ "cp -f $rdz $mnt/$short_type.rdz" };
    if ($@) {
	unlink "$mnt/$short_type.rdz";
	my $avail = (split ' ', `df $mnt`)[-3];
	my $s = int((-s $rdz) / 1024);

	my ($ext) = $img =~ /\.img-(.*)/ or die "bad initrd name ($img)";
	my $dir = "kernel/all.modules/$ext";
	my $pcitable = cat_('/usr/share/ldetect-lst/pcitable');
	my @l = sort { $a <=> $b } map {
	    my ($name) = /(\S+)\.k?o$/ or die "bad file in .mar";
	    my $gz_size = chomp_(`gzip -c $dir/$_ | wc -c`);
	    sprintf "%7d  %6d      %s\n", $gz_size, listlength($pcitable =~ /"$name"/g), $name;
	} split(' ', `mdk-stage1/mar/mar -l $dir/${type}_modules.mar`);
	output('.not-enough-room', "gz size - pcitable - name\n", @l);

	warn sprintf("not enough room for $rdz: need %dKB (available %dKB < needed %dKB)\n", $s - $avail, $avail, $s);
	die qq(check file ".not-enough-room" to see modules size and pcitable usage\n);
    }
    unlink $rdz;

    my $timeout = 72;
    output "$mnt/syslinux.cfg",
"default linux
prompt 1
timeout $timeout
display boot.msg
F1 help.msg
F2 advanced.msg
F3 boot.msg
" . join('', map {
"label $_->[0]
  kernel vmlinuz
  append initrd=$short_type.rdz $_->[1]
" } entries_append($type));

    _ "sync";
    _ "df $mnt";
    _ "$sudo umount $mnt";
}

# alias to x86 variant, slightly bigger with images though
sub boot_img_x86_64 { &boot_img_i386 }

sub boot_img_alpha {
    my ($mnt, $type, $I, $img) = @_;

    __ "$sudo umount $mnt 2>/dev/null";
    _ "dd if=/dev/zero of=$img bs=1k count=1440";
    _ "$mke2fs $img";
    _ "/sbin/e2writeboot $img /boot/bootlx";
    _ "$sudo mount -t ext2 $img $mnt -o loop";
    _ "cp -f vmlinux.gz $mnt";
    -f "$type.rdz" ? _ "cp -f $type.rdz $mnt" : initrd($tmp_mnt_initrd, $type, $I, "$mnt/$type.rdz");

    mkdir "$mnt/etc", 0777;
    output("$mnt/etc/aboot.conf", 
"0:vmlinux.gz initrd=$type.rdz rw $default_append $type
1:vmlinux.gz initrd=$type.rdz rw $default_append text $type
");
    _ "sync";
    _ "df $mnt";
}

sub boot_img_ia64 {
    my ($mnt, $type, $_I, $img, $kernel) = @_;
	my $rdz = $img; $rdz =~ s/\.img/.rdz/;

    __ "$sudo umount $mnt 2>/dev/null";
    _ "dd if=/dev/zero of=$img bs=1k count=16384";
    _ "mkdosfs $img";
    _ "$sudo mount -t vfat $img $mnt -o loop,umask=000";
    _ "$sudo cp -f $kernel $mnt/vmlinux";
    _ "cp -f $rdz $mnt/$type.rdz";
    _ "$sudo cp -f tools/ia64/elilo.efi $mnt";
	output("$mnt/elilo.conf", qq(
prompt
timeout=50

image=vmlinux
        label=linux
        root=/dev/ram3
        initrd=$type.rdz
        append=" ramdisk_size=120000"
        read-only

image=vmlinux
        label=rescue
        root=/dev/ram3
        initrd=$type.rdz
        append=" rescue ramdisk_size=120000"
"));
    _ "sync";
    _ "df $mnt";

}

sub boot_img_sparc {
    my ($mnt, $type, $I, $_img) = @_;
    if ($type =~ /^live(.*)/) {
	#- hack to produce directly into /export the needed file for cdrom boot.
	my $dir = "/export";
	my $boot = "boot"; #- non-absolute pathname only!

	_ "mkdir -p $dir/$boot";
	_ "cp -f /boot/cd.b /boot/second.b $dir/$boot";
	_ "cp -f vmlinux$1 $dir/$boot/vmlinux$1";
	-f "live$1.rdz" ? _ "cp -f live$1.rdz $dir/$boot" : initrd($tmp_mnt_initrd, $type, $I, "$dir/$boot/live$1.rdz");

	output("$dir/$boot/silo.conf", qq(
partition=1
default=linux
timeout=100
read-write
message=/$boot/boot.msg
image="cat /$boot/boot.msg"
  label=1
  single-key
image="cat /$boot/general.msg"
  label=2
  single-key
image="cat /$boot/expert.msg"
  label=3
  single-key
image="cat /$boot/rescue.msg"
  label=4
  single-key
image="cat /$boot/kickit.msg"
  label=5
  single-key
image="cat /$boot/param.msg"
  label=6
  single-key
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=linux
  alias=install
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 root=/dev/ram3"
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=text
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 text root=/dev/ram3"
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=expert
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 expert root=/dev/ram3"
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=ks
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 ks root=/dev/ram3"
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=rescue
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 rescue rw root=/dev/ram3"
image[sun4u]=/$boot/vmlinux64
  label=linux
  alias=install
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 root=/dev/ram3"
image[sun4u]=/$boot/vmlinux64
  label=text
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 text root=/dev/ram3"
image[sun4u]=/$boot/vmlinux64
  label=expert
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 expert root=/dev/ram3"
image[sun4u]=/$boot/vmlinux64
  label=ks
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 ks root=/dev/ram3"
image[sun4u]=/$boot/vmlinux64
  label=rescue
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 rescue rw root=/dev/ram3"
"));

	output("$dir/$boot/README", "
To Build a Bootable CD-ROM, try:
  mkisofs -R -o t.iso -s /$boot/silo.conf /export
");
    } elsif ($type =~ /^tftprd(.*)/) {
	my $dir = "/export";
	my $boot = "images";
	my $setarch = $1 ? "sparc64" : "sparc32";

	_ "mkdir -p $dir/$boot";
	-f "$type.rdz" or initrd($tmp_mnt_initrd, $type, $I, "$type.rdz");
	_ "cp -f vmlinux$1.aout $dir/$boot/$type.img";
	_ "$setarch kernel$1/src/arch/sparc$1/boot/piggyback $dir/$boot/$type.img kernel$1/boot/System.map $type.rdz";
    } elsif ($type =~ /^tftp(.*)/) {
	my $dir = "/export";
	my $boot = "images";

	_ "mkdir -p $dir/$boot";
	_ "cp -f vmlinux$1.aout $dir/$boot/$type.img";
    } else {
	my $dir = "floppy";
	__ "$sudo umount $mnt 2>/dev/null";
	_ "rm -rf $dir";
	_ "mkdir -p $dir";
	_ "cp -f /boot/fd.b /boot/second.b $dir";
	_ "cp -f vmlinuz$I $dir/vmlinux$I.gz";
	-f "$type.rdz" ? _ "cp -f $type.rdz $dir" : initrd($tmp_mnt_initrd, $type, $I, "$dir/$type.rdz");

	output("$dir/boot.msg", "
Welcome to Mandrake Linux 7.1

Press <Enter> to install or upgrade a system 7mMandrake Linux7m
");

	output("$dir/silo.conf", qq(
partition=1
default=linux
timeout=100
read-write
message=/boot.msg
image=/vmlinux$I.gz
  label=linux
  initrd=/$type.rdz
  append="ramdisk_size=128000 $type root=/dev/ram3"
"));
	_ "genromfs -d $dir -f /dev/ram -A 2048,/.. -a 512 -V 'DrakX boot disk'";
	_ "$sudo mount -t romfs /dev/ram $mnt";
	_ "silo -r $mnt -F -i /fd.b -b /second.b -C /silo.conf";
	_ "$sudo umount $mnt";
	_ "dd if=/dev/ram of=$type.img bs=1440k count=1";
	_ "sync";
	_ "$sudo mount -t romfs /dev/ram $mnt";
	_ "df $mnt";
    }
}

sub boot_img_ppc() {
	#- hack to produce directly into /export the needed file for cdrom boot.
	my $dir = "/export";
	my $boot = "boot"; #- non-absolute pathname only!
    _ "rm -rf $dir/$boot"; mkdir "$dir/$boot", 0777;
	foreach (glob("kernel/all.kernels/*")) {
		my $ext = basename($_);
		if ($ext =~ /2.4/) {
			_ "cp $_/boot/vmlinux $dir/$boot/vmlinux";
			_ "cp images/all.rdz-$ext $dir/$boot/all.gz";
		}
		if ($ext =~ /2.2/) {
			_ "cp $_/boot/vmlinux $dir/$boot/vmlinux-2.2";
			_ "cp images/all.rdz-$ext $dir/$boot/all-2.2.gz";
		}
    }
    _ "cp -f /usr/lib/yaboot/yaboot $dir/$boot/yaboot";
	
	output("$dir/$boot/ofboot.b", '<CHRP-BOOT>
<COMPATIBLE>
MacRISC
</COMPATIBLE>
<DESCRIPTION>
Mandrake Linux PPC bootloader
</DESCRIPTION>
<BOOT-SCRIPT>
" screen" output
dev screen
" "(0000000000aa00aa0000aaaaaa0000aa00aaaa5500aaaaaa)" drop 0 8 set-colors
" "(5555555555ff55ff5555ffffff5555ff55ffffff55ffffff)" drop 8 8 set-colors
device-end
3 to foreground-color
0 to background-color
" "(0C)" fb8-write drop
" Booting Mandrake Linux PPC..." fb8-write drop 100 ms
boot cd:2,\\\\yaboot
</BOOT-SCRIPT>
<OS-BADGE-ICONS>
1010
000000000000F8FEACF6000000000000
0000000000F5FFFFFEFEF50000000000
00000000002BFAFEFAFCF70000000000
0000000000F65D5857812B0000000000
0000000000F5350B2F88560000000000
0000000000F6335708F8FE0000000000
00000000005600F600F5FD8100000000
00000000F9F8000000F5FAFFF8000000
000000008100F5F50000F6FEFE000000
000000F8F700F500F50000FCFFF70000
00000088F70000F50000F5FCFF2B0000
0000002F582A00F5000008ADE02C0000
00090B0A35A62B0000002D3B350A0000
000A0A0B0B3BF60000505E0B0A0B0A00
002E350B0B2F87FAFCF45F0B2E090000
00000007335FF82BF72B575907000000
000000000000ACFFFF81000000000000
000000000081FFFFFFFF810000000000
0000000000FBFFFFFFFFAC0000000000
000000000081DFDFDFFFFB0000000000
000000000081DD5F83FFFD0000000000
000000000081DDDF5EACFF0000000000
0000000000FDF981F981FFFF00000000
00000000FFACF9F9F981FFFFAC000000
00000000FFF98181F9F981FFFF000000
000000ACACF981F981F9F9FFFFAC0000
000000FFACF9F981F9F981FFFFFB0000
00000083DFFBF981F9F95EFFFFFC0000
005F5F5FDDFFFBF9F9F983DDDD5F0000
005F5F5F5FDD81F9F9E7DF5F5F5F5F00
0083DD5F5F83FFFFFFFFDF5F835F0000
000000FBDDDFACFBACFBDFDFFB000000
000000000000FFFFFFFF000000000000
0000000000FFFFFFFFFFFF0000000000
0000000000FFFFFFFFFFFF0000000000
0000000000FFFFFFFFFFFF0000000000
0000000000FFFFFFFFFFFF0000000000
0000000000FFFFFFFFFFFF0000000000
0000000000FFFFFFFFFFFFFF00000000
00000000FFFFFFFFFFFFFFFFFF000000
00000000FFFFFFFFFFFFFFFFFF000000
000000FFFFFFFFFFFFFFFFFFFFFF0000
000000FFFFFFFFFFFFFFFFFFFFFF0000
000000FFFFFFFFFFFFFFFFFFFFFF0000
00FFFFFFFFFFFFFFFFFFFFFFFFFF0000
00FFFFFFFFFFFFFFFFFFFFFFFFFFFF00
00FFFFFFFFFFFFFFFFFFFFFFFFFF0000
000000FFFFFFFFFFFFFFFFFFFF000000
</OS-BADGE-ICONS>
</CHRP-BOOT>
');

	output("$dir/$boot/yaboot.conf", '
init-message = "\nWelcome to Mandrake Linux PPC!\nHit <TAB> for boot options.\n\n"
timeout = 150
default = install-gui
message=cd:,\\\\yaboot.msg

image = cd:,\\\\vmlinux
    label = install-gui
    root = /dev/ram3
    initrd = cd:,\\\\all.gz
    initrd-size = 34000

image = cd:,\\\\vmlinux-2.2
    label = install-gui-2.2
    root = /dev/ram3
    initrd = cd:,\\\\all-2.2.gz
    initrd-size = 34000

image = cd:,\\\\vmlinux
    label = install-text
    root = /dev/ram3
    initrd = cd:,\\\\all.gz
    initrd-size = 34000
    append = " text video=ofonly"

image = cd:,\\\\vmlinux-2.2
    label = install-text-2.2
    root = /dev/ram3
    initrd = cd:,\\\\all-2.2.gz
    initrd-size = 34000
    append = " text video=ofonly"

image = cd:,\\\\vmlinux
    label = install-gui-old
    root = /dev/ram3
    initrd = cd:,\\\\all.gz
    initrd-size = 34000
    append = " gui-old video=ofonly"

image = cd:,\\\\vmlinux-2.2
    label = install-gui-old-2.2
    root = /dev/ram3
    initrd = cd:,\\\\all-2.2.gz
    initrd-size = 34000
    append = " gui-old video=ofonly"

image = enet:0,vmlinux
    label = install-net
    root = /dev/ram3
    initrd = enet:0,all.gz
    initrd-size = 34000
    append = " video=ofonly"

image = enet:0,vmlinux-2.2
    label = install-net-2.2
    root = /dev/ram3
    initrd = enet:0,all-2.2.gz
    initrd-size = 34000
    append = " video=ofonly"
	
image = enet:0,vmlinux
    label = install-net-text
    root = /dev/ram3
    initrd = enet:0,all.gz
    initrd-size = 34000
    append = " text video=ofonly"

image = enet:0,vmlinux-2.2
    label = install-net-text-2.2
    root = /dev/ram3
    initrd = enet:0,all-2.2.gz
    initrd-size = 34000
    append = " text video=ofonly"
	
image = cd:,\\\\vmlinux
    label = rescue
    root = /dev/ram3
    initrd = cd:,\\\\all.gz
    initrd-size = 34000
    append = " rescue video=ofonly"    

image = enet:0,vmlinux
    label = rescue-net
    root = /dev/ram3
    initrd = enet:0,all.gz
    initrd-size = 34000
    append = " rescue video=ofonly"    
');

	output("$dir/$boot/yaboot.msg", '
Thanks for choosing Mandrake Linux PPC.  The following is a short
explanation of the various options for booting the install CD.

All options ending with "2.2" will use the 2.2.20-9mdkBOOT kernel.
The default syntax with no suffix uses the 2.4.18-4mdkBOOT kernel.
The default if you just hit enter is "install-gui".

install-gui:        	uses XFree86 fbdev mode
install-text:       	text based install
install-gui-old:    	old Xpmac gui
install-net:            allows you to use a minimal boot CD,
                        pulling the rest of the install from
                        a network server
install-net-text:       text mode network install
rescue:                 boots the rescue image
rescue-net:             boots the rescue image from a network server

');

}

sub bmp_to_msg {
    my ($bmp, $msg, $progress_color, $clear_color) = @_;
    #- change here for newer picture isolinux-graphic.bmp and newer parameters.
    #- for old style pictures (9.1 and previous)
    # lilo-bmp2mdk mode:0x103 progress:425,173,16,7,64+21 clear:600,800,64+59 pos:0,0 <isolinux-graphic.bmp >isolinux/boot.msg
    #- for current 9.2 pictures.
    -e $bmp and _ "lilo-bmp2mdk mode:0x103 progress:397,190,14,6,64+$progress_color clear:600,800,64+$clear_color pos:0,0 <$bmp >$msg"
}

sub isolinux {
    my ($main, @kernels) = @_;
    @kernels = ($main, grep { $_ ne $main } @kernels);
    _ "rm -rf isolinux"; mkdir "isolinux", 0777;

    each_index {
	mkdir "isolinux/alt$::i", 0777;
	my ($kernel) = glob("kernel/all.kernels/$_/boot/vmlinu*");
	_ "cp $kernel isolinux/alt$::i/vmlinuz";
	initrd($tmp_mnt_initrd, 'all', '', "images/all.rdz-$_");
	_ "mv images/all.rdz-$_ isolinux/alt$::i/all.rdz";
    } @kernels;

    bmp_to_msg('isolinux-graphic.bmp', 'isolinux/boot.msg', 18, 18);

    _ "cp /usr/lib/syslinux/isolinux.bin isolinux/isolinux.bin";
    _ "install -m 644 -D /boot/memtest* isolinux/test/memtest.bin";
    output "isolinux/isolinux.cfg", "
default linux
prompt 1
timeout 150
" . (-e "isolinux/boot.msg" ? "display boot.msg" : "display help.msg") . "
F1 help.msg
F2 advanced.msg
" . (-e "isolinux/boot.msg" && "F3 boot.msg") . "
" . join('', map {
"label $_->[0]
  kernel alt0/vmlinuz
  append initrd=alt0/all.rdz $_->[1] automatic=method:cdrom
" } entries_append('all'))
  . join('', map_index {
"label alt$::i
  kernel alt$::i/vmlinuz
  append initrd=alt$::i/all.rdz $default_append $default_acpi $default_vga
" } @kernels) .
"label memtest
  kernel test/memtest.bin
";

    output "isolinux/help.msg", (-e "isolinux/boot.msg" && pack "C*", 0x0E, 0x80, 0x03, 0x00) .
"
                  0aWelcome to 09Mandrake Linux0a install help07

In most cases, the best way to get started is to simply press the 0e<Enter>07 key.
If you experience problems with standard install, try one of the following 
install types (type the highlighted text and press 0e<Enter>07):

 o  0fvgalo07 for low resolution graphical installation.
 o  0ftext07 for text installation instead of the graphical one.
 o  0flinux07 for standard graphical installation at normal resolution.
 o  0fexpert07 for expert graphical installation at normal resolution.


To use this CD to repair an already installed system type 0frescue07
followed by 0e<Enter>07.

You can also pass some 0f<specific kernel options>07 to the Linux kernel. 
For example, try 0flinux mem=128M07 if your system has 128Mb of RAM but the default
kernel ($main) does not detect it correctly.
0cNOTE07: You cannot pass options to modules (SCSI, ethernet card) or devices
such as CD-ROM drives in this way. If you need to do so, use expert mode.

" . (-e "isolinux/boot.msg" ?
     "0c[F1-Help] [F2-Advanced Help] [F3-Main]07" :
     "0c[F1-Help] [F2-Advanced Help]07") . "\n";

    output "isolinux/advanced.msg", (-e "isolinux/boot.msg" && pack "C*", 0x0E, 0x80, 0x03, 0x00) .
"
The following install types may be used instead of previously notified :
 o  0fvgahi07 for high resolution graphical installation.
 o  0fvga1607 for 640x480 in 16 colors graphical installation.

The following options may be added on the command line :
 o  0freadonly=107 to disable editing disk partitions.
 o  0fdisplay=ackbar:007 to export display to 09ackbar07 machine screen 0 during
    installation.
 o  0fnoauto07 to disable automatic detection (generally used with 0fexpert07).
 o  0fsecurity=n07 to set security level to value 09n07.
 o  0fupdatemodules07 to use the special update floppy containing modules updates.
 o  0fpatch07 to use a patch from the floppy (file named 09patch.pl07).
 o  0fauto_install=floppy07 to enable auto_install using 09auto_inst.cfg07 file on the
    floppy.
 o  0fmemtest07 to test your computer RAM for faulty memory modules.

You can choose the following kernels :
" . join('', map_index { " o  0falt$::i07 is kernel $_\n" } @kernels) . "
" . (-e "isolinux/boot.msg" ?
     "0c[F1-Help] [F2-Advanced Help] [F3-Main]07" :
     "0c[F1-Help] [F2-Advanced Help]07") . "\n";
}

sub hd_grub {
    my ($mnt, $img) = @_;
    my $mapfile = '/tmp/device.map.tmp';

    my @grub_files = map { "/boot/grub/$_" } qw(stage1 stage2);
    my $size = 40_000 + sum(map { -s $_ } @grub_files);

    __ "$sudo umount $mnt 2>/dev/null";
    _ "dd if=/dev/zero of=$img bs=1 count=$size";
    _ "mkdosfs $img";
    _ "$sudo mount -t vfat -o umask=0 $img $mnt -o loop";
    _ "cp @grub_files $mnt";

    output("$mnt/menu.lst", <<EOF);
timeout 0
default 0
fallback 1

title Mandrake Install

root (hd0,0)
kernel /cooker/isolinux/alt0/vmlinuz $default_append $default_acpi $default_vga automatic=method:disk
initrd /cooker/isolinux/alt0/all.rdz

title Help

pause To display the help, press <space> until you reach "HELP END"
pause .
pause Please see http://qa.mandrakesoft.com/hd_grub.cgi for a friendlier solution
pause .
pause To specify the location where Mandrake Linux is copied,
pause choose "Mandrake Install", and press "e".
pause Then change "root (hd0,0)". FYI:
pause - (hd0,0) is the first partition on first bios hard drive (usually hda1)
pause - (hd0,4) is the first extended partition (usually hda5)
pause - (hd1,0) is the first partition on second bios hard drive
pause Replace /cooker to suits the directory containing Mandrake Linux
pause .
pause HELP END
EOF

    _ "$sudo umount $mnt";

    output($mapfile, "(fd0) $img\n");

    open(my $G, "| grub --device-map=$mapfile --batch");
    print $G <<EOF;
root (fd0)
install /stage1 d (fd0) /stage2 p /menu.lst
quit
EOF
    close $G;
    unlink $mapfile;
}

sub boot_iso {
    my ($iso) = @_;

    my $cfg = cat_('isolinux/isolinux.cfg') or die 'isolinux missing';
    $cfg =~ s/automatic=method:\w+,?//gm;

    eval { rm_rf('.boot_iso') };
    mkdir_p('.boot_iso/isolinux');
    _ "cd .boot_iso/isolinux ; ln -s ../../isolinux/* .";
   
    my $cfg_file = '.boot_iso/isolinux/isolinux.cfg';
    unlink $cfg_file;
    output($cfg_file, $cfg);

    _ "mkisofs -r -f -J -cache-inodes -V 'Mdk Boot ISO' -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o $iso .boot_iso";
    rm_rf('.boot_iso');
}

sub drivers {
    my ($type, $I, $img) = @_;

    _ "dd if=/dev/zero of=$img bs=1k count=1440";
    _ "$mke2fs $img";
    _ "$sudo mount -t ext2 $img $tmp_mnt -o loop";

    my ($ext) = $img =~ /img-(.*)/ or die "bad image name ($img)";
    _ "$sudo cp -f kernel/all.modules$I/$ext/${type}_modules.mar $tmp_mnt/modules$I.mar";

    _ "sync";
    _ "df $tmp_mnt";

    _ "$sudo umount $tmp_mnt";
}