summaryrefslogtreecommitdiffstats
path: root/make_boot_img
blob: 0cdeb13bf9c254280cc1138eb3abc36878bbe438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#!/usr/bin/perl

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

my $default_append = '';
# full acpi support for x86_64, enough acpi support for x86 ht, no acpi for others
my $default_acpi = $arch =~ /i.86/ ? "acpi=ht" : $arch =~ /x86_64/ ? "" : "acpi=off";
my $default_vga = "vga=788 splash=silent";
my $timeout = 150;

my $instdir = "mdk-stage1";
my $tmp_mnt = '/tmp/drakx_mnt';
my $tmp_mnt_initrd = '/tmp/drakx_mnt2';

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

sub __ { print @_, "\n"; system(@_) }
sub _ { __ @_; $? and die }
sub mke2fs { 
    my ($f) = @_;
    _ "/sbin/mke2fs -q -m 0 -F -s 1 $f";
    _ "/sbin/tune2fs -c 0 -U clear -T 1970010101 $f";
}

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

my $main = chomp_(cat_('kernel/RPMS/.main'));
my $main_BOOT = chomp_(cat_('kernel/RPMS/.main-BOOT'));
my @kernels = grep { !/xbox/ } all('kernel/all.kernels');
my @kernels_BOOT = grep { /BOOT/ } @kernels;

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

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

if (any { /move/ } @images) {
    -e "$instdir/init-move" or die "ERROR: $instdir not built for move\n";
} else {
    -e "$instdir/init-move" and die "ERROR: $instdir built for move\n";
}

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

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

	if (my ($tftpboot) = grep { -e $_ } qw(/tftpboot /var/lib/tftpboot)) {
	    system("/bin/cp -f isolinux/alt0/* $tftpboot");
	}
    } elsif ($img =~ /move/) {
	isolinux_move($main);

	if (my ($tftpboot) = grep { -e $_ } qw(/tftpboot /var/lib/tftpboot)) {
	    system("/bin/cp -f move/isolinux/* $tftpboot/move 2>/dev/null");
	}
    } elsif ($img =~ /boot.iso/) {
	boot_iso($img, \@kernels);
    } elsif ($img =~ /drivers/) {
	drivers($type, $I, "$img-$_") foreach @kernels_BOOT;
	rename("$img-$main_BOOT", $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"}->($type, $I, "$img-$_", "kernel/all.kernels/$_/vmlinuz") foreach $type eq 'all' ? @kernels : @kernels_BOOT;
	rename("$img-$main", $img) or rename("$img-$main_BOOT", $img);
    } else {
	die "unknown image $img";
    }
}

sub syslinux_color {
    "0" . {
	default => '7',
	blue    => '9',
	green   => 'a',
	red     => 'c',
	yellow  => 'e',
	white   => 'f',
    }->{$_[0]} || die "unknown color $_[0]\n";
}

sub syslinux_msg { 
    my ($msg_xml_file, @more_text) = @_;

    require XML::Parser;

    sub xml_tree2syslinux {
	my ($current_color, $tree) = @_;
	my (undef, @l) = @$tree;
	join('', map {
	    my ($type, $val) = @$_;
	    if ($type eq '0') {
		$val;
	    } else {
		syslinux_color($type) . xml_tree2syslinux($type, $val) . syslinux_color($current_color);
	    }
	} group_by2(@l));
    }

    print "parsing $msg_xml_file\n";
    my $tree = XML::Parser->new(Style => 'Tree')->parsefile($msg_xml_file);
    $tree->[0] eq 'document' or die "bad file $msg_xml_file\n";
    my $text = xml_tree2syslinux('default', $tree->[1]);

    pack("C*", 0x0E, 0x80, 0x03, 0x00) . ""
      . $text . join('', @more_text)
      . "\n" . syslinux_color('red') . "[F1-Help] [F2-Advanced Help] [F3-Main]" . syslinux_color('default') . "\n";
}

sub syslinux_cfg {
    my (@entries) = @_;

    my $header = <<EOF;
default linux
prompt 1
timeout $timeout
display boot.msg
F1 help.msg
F2 advanced.msg
F3 boot.msg
EOF

    my @l = map {
	$_->{append} =~ s/\s+/ /g;
	"label $_->{label}\n" .
	"  kernel $_->{kernel}\n" .
	($_->{initrd} ? "  append initrd=$_->{initrd} $_->{append}\n" : '');
    } @entries;

    $header . join('', @l);
}

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

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

    __ "$sudo umount $tmp $mnt 2>/dev/null";
    _ "dd if=/dev/zero of=$tmp bs=1k count=" . ($arch =~ /ia64/ || $type eq "all" ? 16384 : $type eq 'ka' ? 2880 : 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",
	    cdrom => "stage1-cdrom",
	}}{$type} || 'stage1-full';
	install_stripped("$instdir/init", "$mnt/sbin");
	install_stripped("$instdir/$install", "$mnt/sbin/stage1");
    }

    if (member($type, qw(network all))) {
	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/) {
	 _ "$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.kernels/$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.kernels$I/$ext";
    if ($type eq 'all') {
	__ "$sudo tar xC $mnt/modules -f $modz/${type}_modules.tar";
    } else {
	_ "$sudo cp -f $modz/${type}_modules.mar $mnt/modules/modules$I.mar";
    }
    _ "$sudo cp -f $modz/modules.dep $mnt/modules/";
    _ "$sudo cp -f /sbin/sash $mnt/tmp/sh" if $type eq 'all' && $ENV{DEBUGSTAGE1};
    _ "$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 = $type =~ /cdrom/ ? 'automatic=method:cdrom ' : '';
    $automatic .= 'changedisk ' if $type =~ /changedisk/;

    my @simple_entries = (
	linux => $default_vga,
	vgalo => "vga=785",
	vgahi => "vga=791",
	vga16 => "vga16",
	text => "text",
	patch => "patch $default_vga",
	rescue => "rescue",
    );
    my @entries = (
        (map { $_->[0] => "$automatic$default_acpi $_->[1]" } group_by2(@simple_entries)),
	acpi => "$automatic$default_vga",
    );

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

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

    syslinux_cfg(
	(map {
	    { kernel => 'alt0/vmlinuz', initrd => 'alt0/all.rdz', %$_ };
	} entries_append($type)),
	(map_index {
	    { label => "alt$::i", kernel => "alt$::i/vmlinuz", initrd => "alt$::i/all.rdz", 
	      append => "$default_append $default_acpi $default_vga" };
	} @kernels),
	{ label => 'memtest', kernel => 'test/memtest.bin' },
    );
}
sub remove_ending_zero {
    my ($img) = @_;
    _(q(perl -0777 -pi -e 's/\0+$//' ) . $img);
}

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

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

    _ "dd if=/dev/zero of=$img bs=1k count=" . ($type eq 'all' ? 8000 : $type eq 'ka' ? 2950 : 1440);
    my $mk_options = $type eq 'all' ? '' : '-f 1 -r 16 -s 2'; # keep the FAT bookkeeping as small as possible
    _ "mkdosfs $mk_options $img";
    _ "$sudo syslinux-graphic $img";

    _ "$sudo mount -t vfat -o umask=0 $img $tmp_mnt -o loop";
    _ "cat $kernel > $tmp_mnt/vmlinuz";
    _ "lilo-bmp2mdk file:isolinux-graphic" . ($type eq 'all' ? '' : '-simple') . ".bmp >$tmp_mnt/boot.msg";

    output("$tmp_mnt/help.msg", syslinux_msg('help.msg.xml'));
    output("$tmp_mnt/advanced.msg", syslinux_msg('advanced.msg.xml'));

    (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);

    output("$tmp_mnt/syslinux.cfg", 
	   syslinux_cfg(map {
			    { kernel => 'vmlinuz', initrd => "$short_type.rdz", %$_ };
			} entries_append($type)));

    eval { _ "cp -f $rdz $tmp_mnt/$short_type.rdz" };
    if ($@) {
	#- argh not enough room, try to suggest how to solve this
	unlink "$tmp_mnt/$short_type.rdz";
	my $avail = (split ' ', `df $tmp_mnt`)[-3];
	my $s = int((-s $rdz) / 1024);

	my ($ext) = $img =~ /\.img-(.*)/ or die "bad initrd name ($img)";
	my $dir = "kernel/all.kernels/$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_(`tar xOf $dir/all_modules.tar $_ | gzip | wc -c`);
	    sprintf "%7d  %6d      %s\n", $gz_size, listlength($pcitable =~ /"$name"/g), $name;
	} split(' ', `mdk-stage1/mar/mar -l $dir/${initrd_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;

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

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

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

    __ "$sudo umount $tmp_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 $tmp_mnt -o loop";
    _ "cp -f vmlinux.gz $tmp_mnt";
    -f "$type.rdz" ? _ "cp -f $type.rdz $tmp_mnt" : initrd($tmp_mnt_initrd, $type, $I, "$tmp_mnt/$type.rdz");

    mkdir "$tmp_mnt/etc", 0777;
    output("$tmp_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 $tmp_mnt";
}

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

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

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

image=vmlinux
        label=rescue
        initrd=$type.rdz
        append=" rescue ramdisk_size=120000"
"));
    _ "sync";
    _ "df $tmp_mnt";

}

sub boot_img_sparc {
    my ($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"
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=text
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 text"
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=expert
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 expert"
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=ks
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 ks"
image[sun4c,sun4d,sun4m]=/$boot/vmlinux
  label=rescue
  initrd=/$boot/live.rdz
  append="ramdisk_size=128000 rescue"
image[sun4u]=/$boot/vmlinux64
  label=linux
  alias=install
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000"
image[sun4u]=/$boot/vmlinux64
  label=text
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 text"
image[sun4u]=/$boot/vmlinux64
  label=expert
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 expert"
image[sun4u]=/$boot/vmlinux64
  label=ks
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 ks"
image[sun4u]=/$boot/vmlinux64
  label=rescue
  initrd=/$boot/live64.rdz
  append="ramdisk_size=128000 rescue"
"));

	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 $tmp_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 Mandriva Linux 7.1

Press <Enter> to install or upgrade a system 7mMandriva 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"
"));
	_ "genromfs -d $dir -f /dev/ram -A 2048,/.. -a 512 -V 'DrakX boot disk'";
	_ "$sudo mount -t romfs /dev/ram $tmp_mnt";
	_ "silo -r $tmp_mnt -F -i /fd.b -b /second.b -C /silo.conf";
	_ "$sudo umount $tmp_mnt";
	_ "dd if=/dev/ram of=$type.img bs=1440k count=1";
	_ "sync";
	_ "$sudo mount -t romfs /dev/ram $tmp_mnt";
	_ "df $tmp_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 =~ /power4/) {
			_ "cp $_/boot/vmlinuz-$ext* $dir/$boot/vmlinux-power4";
			_ "cp images/all.rdz-$ext $dir/$boot/all-power4.gz";
		}
		elsif ($ext =~ /2.6/) {
			_ "cp $_/boot/vmlinuz-$ext $dir/$boot/vmlinux";
			_ "cp images/all.rdz-$ext $dir/$boot/all.gz";
		}
		elsif ($ext =~ /2.4/) {
			_ "cp $_/boot/vmlinux $dir/$boot/vmlinux-2.4";
			_ "cp images/all.rdz-$ext $dir/$boot/all-2.4.gz";
		}
    }
    _ "cp -f /usr/lib/yaboot/yaboot $dir/$boot/yaboot";
	
	output("$dir/$boot/ofboot.b", '<CHRP-BOOT>
<COMPATIBLE>
MacRISC
</COMPATIBLE>
<DESCRIPTION>
Mandriva Linux PPC bootloader
</DESCRIPTION>
<BOOT-SCRIPT>
" screen" output
load-base release-load-area
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 Mandriva Linux PPC..." fb8-write drop 100 ms
boot cd:,\boot\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 Mandriva Linux PPC!\nHit <TAB> for boot options.\n\n"
timeout = 150
device=cd:
default = install-gui
message=/boot/yaboot.msg

image = /boot/vmlinux
    label = install-gui
    initrd = /boot/all.gz
    initrd-size = 34000
    append = " ramdisk_size=128000"

image = /boot/vmlinux-power4
    label = install-gui-power4
    initrd = /boot/all-power4.gz
    initrd-size = 34000
    append = " ramdisk_size=128000"

image = /boot/vmlinux
    label = install-text
    initrd = /boot/all.gz
    initrd-size = 34000
    append = " text ramdisk_size=128000"

image = /boot/vmlinux-power4
    label = install-text-power4
    initrd = /boot/all-power4.gz
    initrd-size = 34000
    append = " text ramdisk_size=128000"

image = /boot/vmlinux
    label = install-gui-old
    initrd = /boot/all.gz
    initrd-size = 34000
    append = " gui-old ramdisk_size=128000"

image = /boot/vmlinux-power4
    label = install-gui-old-power4
    initrd = /boot/all-power4.gz
    initrd-size = 34000
    append = " gui-old ramdisk_size=128000"

image = enet:0,vmlinux
    label = install-net
    initrd = enet:0,all.gz
    initrd-size = 34000
    append = " ramdisk_size=128000"

image = enet:0,vmlinux-power4
    label = install-net-power4
    initrd = enet:0,all-power4.gz
    initrd-size = 34000
    append = " ramdisk_size=128000"

image = enet:0,vmlinux
    label = install-net-text
    initrd = enet:0,all.gz
    initrd-size = 34000
    append = " text ramdisk_size=128000"

image = enet:0,vmlinux-power4
    label = install-net-text-power4
    initrd = enet:0,all-power4.gz
    initrd-size = 34000
    append = " text ramdisk_size=128000"

image = /boot/vmlinux
    label = rescue
    initrd = /boot/all.gz
    initrd-size = 34000
    append = " rescue ramdisk_size=128000"

image = /boot/vmlinux-power4
    label = rescue-power4
    initrd = /boot/all-power4.gz
    initrd-size = 34000
    append = " rescue ramdisk_size=128000"

image = enet:0,vmlinux
    label = rescue-net
    initrd = enet:0,all.gz
    initrd-size = 34000
    append = " rescue ramdisk_size=128000" 

image = enet:0,vmlinux-power4
    label = rescue-net-power4
    initrd = enet:0,all-power4.gz
    initrd-size = 34000
    append = " rescue ramdisk_size=128000" 
');

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

All options ending with "-power4" use the BOOT kernel for ppc 9xx and POWER4.
The default syntax with no suffix uses the BOOT kernel for ppc 6xx 7xx and 7xxx.
The default if you just hit enter is "install-gui".

install-gui:        	uses Xorg fbdev mode
install-text:       	text based install
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 VERSION {
    my ($kernels) = @_;

    map { "$_\n" }
      $ENV{DISTRIB_DESCR},
      scalar gmtime(),
      '', @$kernels;
}

sub syslinux_all_files {
    my ($dir, $kernels) = @_;

    eval { rm_rf($dir) }; mkdir_p($dir);

    @$kernels or die "syslinux_all_files: no kernel\n";

    $default_vga =~ /788/ or die 'we rely on vga=788 for bootsplash';
    my $bootspash_cfg = '/etc/bootsplash/themes/Mandriva/config/bootsplash-800x600.cfg';
    -e $bootspash_cfg or die "can't find $bootspash_cfg";

    each_index {
	mkdir "$dir/alt$::i", 0777;
	_ "cp kernel/all.kernels/$_/vmlinuz $dir/alt$::i";
	initrd($tmp_mnt_initrd, 'all', '', "images/all.rdz-$_");
	_ "splash -s -f $bootspash_cfg >> images/all.rdz-$_" if !/BOOT/;
	rename("images/all.rdz-$_", "$dir/alt$::i/all.rdz");
    } @$kernels;

    _ "lilo-bmp2mdk file:isolinux-graphic.bmp >$dir/boot.msg";

    _ "install -m 644 -D /boot/memtest* $dir/test/memtest.bin";

    output("$dir/help.msg", syslinux_msg('help.msg.xml'));
    output("$dir/advanced.msg", syslinux_msg('advanced.msg.xml', 
					     "\nYou can choose the following kernels :\n",
					     map_index { " o  " . syslinux_color('white') . "alt$::i" . syslinux_color('default') . " is kernel $_\n" } @$kernels));
}

sub isolinux {
    my ($main, @kernels) = @_;
    @kernels = ($main, grep { $_ ne $main } @kernels);

    syslinux_all_files('isolinux', \@kernels);

    _ "cp /usr/lib/syslinux/isolinux.bin isolinux/isolinux.bin";
    output("isolinux/isolinux.cfg", syslinux_cfg_all('cdrom'));

    xbox_stage1() if arch() =~ /i.86/;
}

sub xbox_stage1() {
    my $xbox_kernel = find { /xbox/ } all('kernel/all.kernels') or return;

    my $dir = 'isolinux/xbox';
    eval { rm_rf($dir) }; mkdir_p($dir);

    _ "cp kernel/all.kernels/$xbox_kernel/vmlinuz $dir";
    initrd($tmp_mnt_initrd, 'all', '', "images/all.rdz-$xbox_kernel");
    rename("images/all.rdz-$xbox_kernel", "$dir/initrd");

    _ "cp /usr/share/cromwell/xromwell-installer.xbe $dir/default.xbe";
    output("$dir/linuxboot.cfg", <<EOF);
kernel $dir/vmlinuz
initrd $dir/initrd
append root=/dev/ram3 ramdisk_size=36000 automatic=method:cdrom
EOF
}

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

    syslinux_all_files('.boot_iso/isolinux', $kernels);

    output('.boot_iso/VERSION', VERSION($kernels));	   
   
    _ "cp /usr/lib/syslinux/isolinux.bin .boot_iso/isolinux/isolinux.bin";
    output('.boot_iso/isolinux/isolinux.cfg', syslinux_cfg_all(''));

    _ "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 isolinux_move {   
    my ($main) = @_;

    _ "cp kernel/all.kernels/$main/vmlinuz move/isolinux";
    initrd($tmp_mnt_initrd, 'all', '', "move/isolinux/all.rdz-$main", '-with-busybox');
    rename "move/isolinux/all.rdz-$main", "move/isolinux/all.rdz";

    _ "lilo-bmp2mdk file:move/data/isolinux-graphic.bmp >move/isolinux/boot.msg";
    _ "cp /usr/lib/syslinux/isolinux.bin move/isolinux/isolinux.bin";
}

sub hd_grub {
    my ($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 $tmp_mnt 2>/dev/null";
    _ "dd if=/dev/zero of=$img bs=1 count=$size";
    _ "mkdosfs $img";
    _ "$sudo mount -t vfat -o umask=0 $img $tmp_mnt -o loop";
    _ "cp @grub_files $tmp_mnt";

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

title Mandriva Linux 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.mandriva.com/hd_grub.cgi for a friendlier solution
pause .
pause To specify the location where Mandriva Linux is copied,
pause choose "Mandriva Linux 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 Mandriva Linux
pause .
pause HELP END
EOF

    _ "$sudo umount $tmp_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 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.kernels$I/$ext/${type}_modules.mar $tmp_mnt/modules$I.mar";

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

    _ "$sudo umount $tmp_mnt";
}
s="hl opt">} my (%wtree, %wleaves, $size, $selected_via_click); my $parent; $parent = sub { if (my $w = $wtree{"$_[0]$e->{separator}"}) { return $w } my $s = ''; foreach (split $sep, $_[0]) { $wtree{"$s$_$e->{separator}"} ||= $tree_model->append_set($s ? $parent->($s) : undef, $build_value->($_)); $clean_image->(); $size++ if !$s; $s .= "$_$e->{separator}"; } $wtree{$s}; }; #- do some precomputing to not slowdown selection change and key press my (%precomp, @ordered_keys); mapn { my ($root, $leaf) = $_[0] =~ /(.*)$sep(.+)/ ? ($1, $2) : ('', $_[0]); my $iter = $tree_model->append_set($parent->($root), $build_value->($leaf)); $clean_image->(); my $pathstr = $tree_model->get_path_str($iter); $iter->free; $precomp{$pathstr} = { value => $leaf, fullvalue => $_[0], listvalue => $_[1] }; push @ordered_keys, $pathstr; $wleaves{$_[0]} = $pathstr; } $e->{formatted_list}, $e->{list}; $_->free foreach values %wtree; undef %wtree; my $select = sub { my ($path_str) = @_; my $path = Gtk2::TreePath->new_from_string($path_str); $tree->expand_to_path($path); $path->free; $path = Gtk2::TreePath->new_from_string($path_str); $tree->set_cursor($path, undef, 0); $tree->scroll_to_cell($path, undef, 1, 0.5, 0); $path->free; }; my $curr = $tree_model->get_iter_first; #- default value $tree->expand_all if $tree_expanded; $tree->get_selection->signal_connect(changed => sub { my ($model, $iter) = $_[0]->get_selected; $model && $iter or return; $curr->free if ref $curr; my $path = $tree_model->get_path($curr = $iter); if (!$tree_model->iter_has_child($iter)) { ${$e->{val}} = $precomp{$path->to_string}{listvalue}; &$changed; } else { $tree->expand_row($path, 0) if $selected_via_click; } $path->free; }); my ($starting_word, $start_reg) = ('', "^"); my $timeout; my $toggle = sub { if ($tree_model->iter_has_child($curr)) { my $path = $tree_model->get_path($curr); $tree->toggle_expansion($path, 0); $path->free; } else { &$may_go_to_next; } }; $tree->signal_connect(key_press_event => sub { my ($_w, $event) = @_; $selected_via_click = 0; my $c = chr($event->keyval & 0xff); $curr or return; Gtk2->timeout_remove($timeout) if $timeout; $timeout = ''; if ($event->keyval >= 0x100) { &$toggle if member($event->keyval, (Gtk2::Gdk::Event::Key->Sym_Return, Gtk2::Gdk::Event::Key->Sym_KP_Enter)); $starting_word = '' if !member($event->keyval, (Gtk2::Gdk::Event::Key->Sym_Control_L, Gtk2::Gdk::Event::Key->Sym_Control_R)); } else { my $next; if (member('control-mask', @{$event->state})) { $c eq "s" or return 1; $start_reg and $start_reg = '', return 1; $next = 1; } else { &$toggle if $c eq ' '; $next = 1 if $starting_word eq '' || $starting_word eq $c; $starting_word .= $c unless $starting_word eq $c; } my $word = quotemeta $starting_word; my ($after, $best); my $currpath = $tree_model->get_path_str($curr); foreach my $v (@ordered_keys) { $next &&= !$after; $after ||= $v eq $currpath; if ($precomp{$v}{value} =~ /$start_reg$word/i) { if ($after && !$next) { ($best, $after) = ($v, 0); } else { $best ||= $v; } } } if (defined $best) { $select->($best); } else { $starting_word = ''; } $timeout = Gtk2->timeout_add($forgetTime, sub { $timeout = $starting_word = ''; 0 }); } 0; }); $tree->signal_connect(button_press_event => sub { $selected_via_click = 1; &$double_click if !$tree_model->iter_has_child($curr) && $double_click; }); $tree, sub { my $v = may_apply($e->{format}, $_[0]); my ($model, $iter) = $tree->get_selection->get_selected; $select->($wleaves{$v} || return) if !$model || $wleaves{$v} ne $model->get_path_str($iter); $iter->free if ref $iter; }, $size; } sub create_list { my ($e, $may_go_to_next, $changed, $double_click) = @_; my $l = $e->{list}; my $list = Gtk2::List->new; $list->set_selection_mode('browse'); my $select = sub { $list->select_item($_[0]); }; my $tips = Gtk2::Tooltips->new; each_index { my $item = Gtk2::ListItem->new(may_apply($e->{format}, $_)); $item->signal_connect(key_press_event => sub { my ($_w, $event) = @_; my $c = chr($event->keyval & 0xff); &$may_go_to_next if $event->keyval < 0x100 ? $c eq ' ' : $c eq "\r" || $c eq "\x8d"; 0; }); $list->append_items($item); $item->show; if ($e->{help}) { gtkset_tip($tips, $item, ref($e->{help}) eq 'HASH' ? $e->{help}{$_} : ref($e->{help}) eq 'CODE' ? $e->{help}($_) : $e->{help}); } $item->grab_focus if ${$e->{val}} && $_ eq ${$e->{val}}; } @$l; #- signal_connect'ed after append_items otherwise it is called and destroys the default value $list->signal_connect(select_child => sub { my ($_w, $row) = @_; ${$e->{val}} = $l->[$list->child_position($row)]; &$changed; }); $list->signal_connect(button_press_event => $double_click) if $double_click; $list, sub { my ($v) = @_; eval { $select->(find_index { $_ eq $v } @$l); }; }; } sub ask_fromW { my ($o, $common, $l, $l2) = @_; my $ignore = 0; #-to handle recursivity my $mainw = ugtk2->new($common->{title}, %$o, if__($::main_window, transient => $::main_window)); #-the widgets my (@widgets, @widgets_always, @widgets_advanced, $advanced, $advanced_pack, $has_horiz_scroll, $has_scroll); my $max_width = 0; my $total_size = 0; my $tooltips = Gtk2::Tooltips->new; my $ok_clicked = sub { !$mainw->{ok} || $mainw->{ok}->get_property('sensitive') or return; $mainw->{retval} = 1; Gtk2->main_quit; }; my $set_all = sub { $ignore = 1; $_->{set}->(${$_->{e}{val}}, $_) foreach @widgets_always, @widgets_advanced; $_->{real_w}->set_sensitive(!$_->{e}{disabled}()) foreach @widgets_always, @widgets_advanced; $mainw->{ok}->set_sensitive(!$common->{callbacks}{ok_disabled}()) if $common->{callbacks}{ok_disabled}; $ignore = 0; }; my $get_all = sub { ${$_->{e}{val}} = $_->{get}->() foreach @widgets_always, @widgets_advanced; }; my $update = sub { my ($f) = @_; return if $ignore; $get_all->(); $f->(); $set_all->(); }; my $create_widget = sub { my ($e, $ind) = @_; my $may_go_to_next = sub { my (undef, $event) = @_; if (!$event || ($event->keyval & 0x7f) == 0xd) { if ($ind == $#widgets) { @widgets == 1 ? $ok_clicked->() : $mainw->{ok}->grab_focus; } else { $widgets[$ind+1]{focus_w}->grab_focus; } return 1; #- prevent an action on the just grabbed focus } }; my $changed = sub { $update->(sub { $common->{callbacks}{changed}($ind) }) }; my ($w, $real_w, $focus_w, $set, $get, $expand, $size); my $width = 0; if ($e->{type} eq 'iconlist') { $w = Gtk2::Button->new; $set = sub { gtkdestroy($e->{icon}); my $f = $e->{icon2f}->($_[0]); $e->{icon} = -e $f ? gtkcreate_img($f) : Gtk2::Label->new(may_apply($e->{format}, $_[0])); $w->add($e->{icon}); $e->{icon}->show; }; $w->signal_connect(clicked => sub { $set->(${$e->{val}} = next_val_in_array(${$e->{val}}, $e->{list})); $changed->(); }); $real_w = gtkpack_(Gtk2::HBox->new(0,10), 1, Gtk2::HBox->new(0,0), 0, $w, 1, Gtk2::HBox->new(0,0)); } elsif ($e->{type} eq 'bool') { if ($e->{image}) { $w = gtkadd(Gtk2::CheckButton->new, gtkshow(gtkcreate_img($e->{image}))); } else { $w = Gtk2::CheckButton->new($e->{text}); } $w->signal_connect(clicked => $changed); $set = sub { $w->set_active($_[0]) }; $get = sub { $w->get_active }; $width = length $e->{text}; } elsif ($e->{type} eq 'label') { $w = Gtk2::Label->new(${$e->{val}}); $set = sub { $w->set($_[0]) }; $width = length ${$e->{val}}; } elsif ($e->{type} eq 'button') { $w = Gtk2::Button->new(''); $w->signal_connect(clicked => sub { $get_all->(); $mainw->{rwindow}->hide; if (my $v = $e->{clicked_may_quit}()) { $mainw->{retval} = $v; Gtk2->main_quit; } $mainw->{rwindow}->show; $set_all->(); }); $set = sub { $w->child->set(may_apply($e->{format}, $_[0])) }; $width = length may_apply($e->{format}, ${$e->{val}}); } elsif ($e->{type} eq 'range') { my $want_scale = !$::expert; my $adj = Gtk2::Adjustment->new(${$e->{val}}, $e->{min}, $e->{max} + ($want_scale ? 1 : 0), 1, ($e->{max} - $e->{min}) / 10, 1); $adj->signal_connect(value_changed => $changed); $w = $want_scale ? Gtk2::HScale->new($adj) : Gtk2::SpinButton->new($adj, 10, 0); $w->set_size_request($want_scale ? 200 : 100, -1); $w->set_digits(0); $w->signal_connect(key_press_event => $may_go_to_next); $set = sub { $adj->set_value($_[0]) }; $get = sub { $adj->get_value }; $size = 2; } elsif ($e->{type} =~ /list/) { $e->{formatted_list} = [ map { may_apply($e->{format}, $_) } @{$e->{list}} ]; $width = max(map { length } @{$e->{list}}); if (my $actions = $e->{add_modify_remove}) { my %buttons; my $do_action = sub { @{$e->{list}} || $_[0] eq 'Add' or return; my $r = $actions->{$_[0]}->(${$e->{val}}); defined $r or return; if ($_[0] eq 'Add') { ${$e->{val}} = $r; } elsif ($_[0] eq 'Remove') { ${$e->{val}} = $e->{list}[0]; } $e->{formatted_list} = [ map { may_apply($e->{format}, $_) } @{$e->{list}} ]; my $list = $w->get_model; $list->clear; $list->append_set([ 0 => $_ ])->free foreach @{$e->{formatted_list}}; $changed->(); $buttons{$_}->set_sensitive(@{$e->{list}} != ()) foreach 'Modify', 'Remove'; }; my @actions = (N_("Add"), N_("Modify"), N_("Remove")); $width += max(map { length(translate($_)) } @actions); $has_scroll = $expand = 1; $size = 6; ($w, $set, $focus_w) = create_treeview_list($e, $may_go_to_next, $changed, sub { $do_action->('Modify') if $_[1]->type =~ /^2/ }); $e->{saved_default_val} = ${$e->{val}}; %buttons = map { my $action = $_; $action => gtksignal_connect(Gtk2::Button->new(translate($action)), clicked => sub { $do_action->($action) }); } @actions; $real_w = gtkpack_(Gtk2::HBox->new(0,0), 1, create_scrolled_window($w), 0, gtkpack__(Gtk2::VBox->new(0,0), map { $buttons{$_} } @actions)); $real_w->set_data(must_grow => 1) } else { my $quit_if_double_click = #- i'm the only one, double click means accepting @$l == 1 || $e->{quit_if_double_click} ? sub { $_[1]->type =~ /^2/ && $ok_clicked->() } : ''; my @para = ($e, $may_go_to_next, $changed, $quit_if_double_click); my $use_boxradio = exists $e->{gtk}{use_boxradio} ? $e->{gtk}{use_boxradio} : @{$e->{list}} <= 8; if ($e->{help}) { #- used only when needed, as key bindings are dropped by List (ListStore does not seems to accepts Tooltips). ($w, $set, $focus_w) = $use_boxradio ? create_boxradio(@para) : create_list(@para); } elsif ($e->{type} eq 'treelist') { ($w, $set, $size) = create_treeview_tree(@para, $e->{tree_expanded}); $e->{saved_default_val} = ${$e->{val}}; #- during realization, signals will mess up the default val :( } else { if ($use_boxradio) { ($w, $set, $focus_w) = create_boxradio(@para); } else { ($w, $set, $focus_w) = create_treeview_list(@para); $e->{saved_default_val} = ${$e->{val}}; } } if (@{$e->{list}} > (@$l == 1 ? 10 : 4) || $e->{add_modify_remove}) { $has_scroll = $expand = 1; $real_w = create_scrolled_window($w); $size = (@$l == 1 ? 10 : 4); } else { $size ||= @{$e->{list}}; } } } else { if ($e->{type} eq "combo") { $w = Gtk2::Combo->new; $w->set_use_arrows_always(1); $w->entry->set_editable(!$e->{not_edit}); $w->disable_activate; my @formatted_list = map { may_apply($e->{format}, $_) } @{$e->{list}}; $w->set_popdown_strings(@formatted_list); ($real_w, $w) = ($w, $w->entry); #- FIXME workaround gtk suckiness (set_text generates two 'change' signals, one when removing the whole, one for inserting the replacement..) my $idle; $w->signal_connect(changed => sub { $idle ||= Gtk2->idle_add(sub { undef $idle; $changed->(); 0 }); }); $set = sub { my $s = may_apply($e->{format}, $_[0]); $w->set_text($s) if $s ne $w->get_text && $_[0] ne $w->get_text; }; $get = sub { my $s = $w->get_text; my $i = eval { find_index { $s eq $_ } @formatted_list }; defined $i ? $e->{list}[$i] : $s; }; my @l = sort { $b <=> $a } map { length } @formatted_list; $has_horiz_scroll = 1; $width = $l[@l / 16]; # take the third octile (think quartile) } else { $w = Gtk2::Entry->new; $w->signal_connect(changed => $changed); $w->signal_connect(focus_in_event => sub { $w->select_region(0, -1) }); $w->signal_connect(focus_out_event => sub { $w->select_region(0, 0) }); $set = sub { $w->set_text($_[0]) if $_[0] ne $w->get_text }; $get = sub { $w->get_text }; } $w->signal_connect(key_press_event => $may_go_to_next); $w->set_visibility(0) if $e->{hidden}; } $w->signal_connect(focus_out_event => sub { $update->(sub { $common->{callbacks}{focus_out}($ind) }); }); $tooltips->set_tip($w, $e->{help}) if $e->{help} && !ref($e->{help}); $max_width = max($max_width, $width); $total_size += $size || 1; { e => $e, w => $w, real_w => $real_w || $w, focus_w => $focus_w || $w, expand => $expand, get => $get || sub { ${$e->{val}} }, set => $set || sub {}, icon_w => $e->{icon} && eval { gtkcreate_img($e->{icon}) } }; }; @widgets_always = map_index { $create_widget->($_, $::i) } @$l; my $always_total_size = $total_size; @widgets_advanced = map_index { $create_widget->($_, $::i + @$l) } @$l2; my $advanced_total_size = $total_size - $always_total_size; my $pack = create_box_with_title($mainw, @{$common->{messages}}); my ($totalwidth, $totalheight) = (0, $mainw->{box_size}); my $set_default_size = sub { if (($has_scroll || $has_horiz_scroll) && !$mainw->{isEmbedded} && !$mainw->{isWizard}) { $mainw->{rwindow}->set_default_size($totalwidth+6+$ugtk2::shape_width, $has_scroll ? $totalheight+6+3+$ugtk2::shape_width : -1); } }; my $set_advanced = sub { ($advanced) = @_; $set_default_size->() if $advanced; $update->($common->{callbacks}{advanced}) if $advanced; $advanced ? $advanced_pack->show : $advanced_pack->hide; @widgets = (@widgets_always, if_($advanced, @widgets_advanced)); $mainw->sync; #- for $set_all below (mainly for the set of clist) $set_all->(); #- must be done when showing advanced lists (to center selected value) }; my $advanced_button = [ $common->{advanced_label}, sub { my ($w) = @_; $set_advanced->(!$advanced); $w->child->set($advanced ? $common->{advanced_label_close} : $common->{advanced_label}); } ]; my $create_widgets = sub { my ($size, @widgets) = @_; my $w = create_packtable({}, map { [($_->{icon_w}, $_->{e}{label}, $_->{real_w})] } @widgets); $size && $total_size or return $w; #- do not bother computing stupid/bad things my $ratio = max($size / $total_size, 0.2); my ($possibleheight, $possiblewidth) = $mainw->{isEmbedded} ? (450, 380) : ($::windowheight * 0.8, $::windowwidth * 0.8); $possibleheight -= $mainw->{box_size}; my $wantedwidth = max(250, $max_width * 5); my $width = min($possiblewidth, $wantedwidth); my $wantedheight = ugtk2::n_line_size($size, 'various', $mainw->{rwindow}); my $height = min($possibleheight * $ratio, max(200, $wantedheight)); $totalheight += $height; $totalwidth = max($width, $totalwidth); my $has = $wantedwidth > $width || $wantedheight > $height; $has_scroll ||= $has; $has ? create_scrolled_window($w) : $w; }; my $always_pack = $create_widgets->($always_total_size, @widgets_always); my $has_scroll_always = $has_scroll; my @adv = map { warp_text($_) } @{$common->{advanced_messages}}; $advanced_pack = gtkpack_(Gtk2::VBox->new(0,0), 0, '', (map { (0, Gtk2::Label->new($_)) } @adv), 0, Gtk2::HSeparator->new, 1, $create_widgets->($advanced_total_size, @widgets_advanced)); my @help = if_($common->{interactive_help}, [ N("Help"), sub { my $message = $common->{interactive_help}->() or return; $o->ask_warn(N("Help"), $message); }, 1 ]); my $buttons_pack = ($common->{ok} || !exists $common->{ok}) && $mainw->create_okcancel($common->{ok}, $common->{cancel}, '', @help, if_(@$l2, $advanced_button)); $pack->pack_start($always_pack, 1, 1, 0); $always_pack->show; $pack->pack_start($advanced_pack, 1, 1, 0); if ($buttons_pack) { if ($::isWizard && !$mainw->{pop_it} && $::isInstall) { $buttons_pack->set_size_request($::windowwidth * 0.9 - 20, -1); $buttons_pack = gtkpack__(Gtk2::HBox->new(0,0), $buttons_pack); } $pack->pack_start($buttons_pack, 0, 0, 0); $buttons_pack->show; } gtkadd($mainw->{window}, $pack); $set_default_size->() if $has_scroll_always; $set_advanced->($common->{advanced_state}); my $widget_to_focus = $common->{focus_cancel} ? $mainw->{cancel} : @widgets && ($common->{focus_first} || !$mainw->{ok} || @widgets == 1 && member(ref($widgets[0]{focus_w}), "Gtk2::TreeView", "Gtk2::RadioButton")) ? $widgets[0]{focus_w} : $mainw->{ok}; $widget_to_focus->grab_focus if $widget_to_focus; my $check = sub { my ($f) = @_; sub { $get_all->(); my ($error, $focus) = $f->(); if ($error) { $set_all->(); if (my $to_focus = $widgets[$focus || 0]) { $to_focus->{focus_w}->grab_focus; } else { log::l("ERROR: bad entry number given to focus " . backtrace()); } } !$error; } }; $_->{set}->($_->{e}{saved_default_val} || next) foreach @widgets_always, @widgets_advanced; $mainw->main(map { $check->($common->{callbacks}{$_}) } 'complete', 'canceled'); } sub ask_browse_tree_info_refW { my ($o, $common) = @_; add2hash($common, { wait_message => sub { $o->wait_message(@_) } }); ugtk2::ask_browse_tree_info($common); } sub ask_from__add_modify_removeW { my ($o, $title, $message, $l, %callback) = @_; my $e = $l->[0]; my $chosen_element; put_in_hash($e, { allow_empty_list => 1, gtk => { use_boxradio => 0 }, sort => 0, val => \$chosen_element, type => 'list', add_modify_remove => \%callback }); $o->ask_from($title, $message, $l, %callback); } sub wait_messageW($$$) { my ($o, $title, $messages) = @_; local $::isEmbedded = 0; # to prevent sub window embedding my @l = map { Gtk2::Label->new(scalar warp_text($_)) } @$messages; my $w = ugtk2->new($title, %$o, grab => 1, if__($::main_window, transient => $::main_window)); gtkadd($w->{window}, my $hbox = Gtk2::HBox->new(0,0)); $hbox->pack_start(my $box = Gtk2::VBox->new(0,0), 1, 1, 10); $box->pack_start(shift @l, 0, 0, 4);