summaryrefslogtreecommitdiffstats
path: root/drakpxelinux.pl
blob: 701739aa3f542869803756424b3b1d804d64e107 (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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
#!/usr/bin/perl
#
# Copyright (C) 2004 by Mandrakesoft aginies _ateuh_ mandrakesoft.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# Quick configuration of PXE menu parameters
# use with care developement release....
# thx R1 for test and some debug

my $version = "0.9";

use lib qw(/usr/lib/libDrakX);
use standalone;
use strict;
use common;
use MDK::Common;
use ugtk2 qw(:ask :helpers :wrappers :create :dialogs);
use interactive;

use constant FALSE => 0;
use constant TRUE => 1;

# ie of entry menu in PXE:
#label linux
#        KERNEL images/vmlinuz
#        APPEND initrd=images/all.rdz automatic=method:http,interface:eth0,network:dhcp,server:10.0.1.33,directory:/install/ ramdisk_size=64000 root=/dev/ram3 rw vga=788 display=:0

# default VAR
my $conf = "/var/lib/tftpboot/X86PC/linux/pxelinux.cfg/default";
#my $conf = "/tmp/tr";
my $HOSTNAME = chomp_(`hostname`);
my $TFTPDIR = "/var/lib/tftpboot";
my $CLIENTPATH = '/X86PC/linux';
my $X86 = $TFTPDIR . $CLIENTPATH;
my $IMGPATH = $X86 . '/images';
my $PXEHELP = $X86 . '/help.txt';
my $PXEMESSAGE = $X86 . '/messages';
my $pxeconf = '/etc/pxe.conf';
my $SYSLINUXPATH = '/usr/lib/syslinux/';
my $MEMDISK = $SYSLINUXPATH . '/memdisk';
my $XINETDDIR = "/etc/xinetd.d";

my $interface = 'eth0';
my ($IPSERVER) = `/sbin/ip addr show dev $interface` =~ /^\s*inet\s+(\d+\.\d+\.\d+\.\d+)/m;
my $DOMAINNAME = chomp_(`dnsdomainname`);

my $help = "
PXE Label: the name displayed in PXE menu (a word/number)
Server:    IP address of server, which contains installation directory
Kernel:    memdisk or vmlinuz
Initrd:    network boot image (network.img ) or all.rdz
Interface: network interface used for the installation process
Network:   DHCP or an IP address
Directory: full path to MDK install server directory
Method:    installation method: choose NFS or HTTP
Ramsize:   ramsize parameter on boot disk
Display:   export display on another computer (ie: 10.0.1.33:0)
VGA:       if you encounter any problem with VGA, please adjust
";


my %help = (
	    'initrd' => N("network boot image (network.img ) or all.rdz"),
	    'kernel' => N("memdisk in case of network.img, or vmlinuz"),
	    'vga' => N("if you encounter any problem with VGA, please adjust"),
	    'interface' => N("network interface used for the installation process"),
	    'info' => N("the name displayed in PXE menu (a word/number)"),
	    'network' => N("dhcp or an IP address"),
	    'directory' => N("full path to MDK install server directory"),
	    'automatic' => N("installation method: choose NFS or HTTP"),
	    'ramsize' => N("ramsize parameter on boot disk"),
	    'display' => N("export display on another computer (ie: 10.0.1.33:0)"),
	    'option' => N("apic nolapic acpi=off initrd=/bin/shell"),
	    'server' => N("IP address of server, which contains installation directory"),
	    'labels' => N("list all PXE entry, selected one is the default boot"),
	    'wizardsrv' => N("Launch a wizard to setup a PXE server"),
	    'editb' => N("Edit the PXE entry selected with a dialog box"),
	    'removepxe' => N("Remove the PXE entry selected"),
	    'addpxe' => N("Launch a Wizard to add a PXE entry "),
	   );

sub set_help_tip {
  my ($entry, $key) = @_;
  gtkset_tip(new Gtk2::Tooltips, $entry, formatAlaTeX($help{$key}));
}

if (!-f $conf) {
  err_dialog(N("Error !", "missing $conf\n\nPlease install pxe package.")) and die;
} else {
  save_config($conf);
}

my @listpxe;
my @list_method = qw(nfs http);
my @list_ram = qw(48000 32000 64000 1280000);
my @list_vga = qw(normal 785 788 791 794 text);
my @list_eth = qw(eth0 eth1 eth2);

use constant COLUMN_LABEL => 0;
use constant COLUMN_INFO => 1;
use constant COLUMN_KERNEL => 2;
use constant COLUMN_INITRD => 3;
use constant COLUMN_AUTOMATIC => 4;
use constant COLUMN_INTERFACE => 5;
use constant COLUMN_NETWORK => 6;
use constant COLUMN_SERVER => 7;
use constant COLUMN_DIRECTORY => 8;
use constant COLUMN_RAMDISK => 9;
use constant COLUMN_VGA => 10;
use constant COLUMN_DISPLAY => 11;
use constant COLUMN_OPTION => 12;
use constant NUM_COLUMNS => 13;

# get all values from default PXE file
sub get_items() {
  my $info; my $label; my $kernel; my $initrd;
  foreach (cat_($conf)) {
    if (any { /^label/ } cat_($conf)) {
      if (/^label/) { ($label) = /^label\s(.*)/ }
      if (/KERNEL/) { ($kernel) = /KERNEL\s(.*)/ }
      my ($initrd, $automatic, $interface, $network, $server, $directory, $ramdisk, $vga, $display) = m!\s*APPEND\sinitrd=(.*?rdz)\s\bautomatic=method\b:(nfs|http),interface:(eth0|eth1|eth2),network:(dhcp|10.0.1.33),server:(.*?),directory:(.*?)\s\bramdisk_size\b=(.*?)\sroot=/dev/ram3\srw\svga=(.*?)\sdisplay=(.*?)\s!;
      # in case of parameter not on this order (previous pxe default file)
      $initrd or my ($init) = m!\s*APPEND\s\binitrd\b=(.*?)\s!;
      $ramdisk or my ($ramdis) = m!\bramdisk_size\b=(\d+)\s!;
      $automatic or my ($automati) = m!\bautomatic\b=method:(nfs|http)!;
      $vga or my ($vg) = m!\bvga\b=(.*?)\s!;
      $display or my ($displa) = m!\bdisplay\b=(.*?)\s!;
      $interface or my ($interfac) = m!\binterface\b:(eth0|eth1|eth2)!;
      $network or my ($networ) = m!\bnetwork\b:(dhcp|10.0.1.33)!;
      $server or my ($serve) = m!\bserver\b:(.*?),!;
      $directory or my ($director) = m!\bdirectory\b:(.*?)\s!;
      my $initall; my $ramdiskall; my $autoall; my $vgaall; my $displayall; my $interfaceall; my $networkall; my $serverall; my $directoryall;
      if ($initrd) { $initall = $initrd } else { $initall = $init }
      if ($ramdisk) { $ramdiskall = $ramdisk } else { $ramdiskall = $ramdis }
      if ($automatic) { $autoall = $automatic } else { $autoall = $automati }
      if ($vga) { $vgaall = $vga } else { $vgaall = $vg }
      if ($display) { $displayall = $display } else { $displayall = $displa }
      if ($interface) { $interfaceall = $interface } else { $interfaceall = $interfac }
      if ($network) { $networkall = $network } else { $networkall = $networ }
      if ($server) { $serverall = $server } else { $serverall = $serve }
      if ($directory) { $directoryall = $directory } else { $directoryall = $director }
      my ($allopt) = m!\s*APPEND\s(.*)!;
      my ($autocomp) = m!\bautomatic\b=(.*?)\s!;
      my ($devram) = m!root=/dev/ram3\srw!;
      my $optionsall = join(' ', grep { ! /$devram/ and ! /initrd=$initall/ and ! /display=$displayall/ and ! /ramdisk_size=$ramdiskall/ and ! /automatic=$autocomp/ and ! /vga=$vgaall/ } split(' ', $allopt));
      if ($label and $kernel and $initall) {
	my $information = get_information($label);
	# now push data in @
	push @listpxe, {
			label => $label,
			info => $information,
			kernel => $kernel,
			initrd => $initall,
			automatic => $autoall,
			interface => $interfaceall,
			network => $networkall,
			server => $serverall,
			directory => $directoryall,
			ramdisk => $ramdiskall,
			vga => $vgaall,
			display => $displayall,
			option => $optionsall,
		       },;
      }
    }
  }
}


# get info from help.txt
sub get_information {
  my ($label) = @_;
  my $line = cat_($PXEHELP);
  my ($information) = $line =~ /\b$label\b\s: (.*)/;
  $information and return $information;
}

sub list_label_pxe() {
  my @labels;
  foreach (cat_($conf)) {
    my ($label) = /label\s(.*)/;
    $label and push @labels, $label;
  }
  @labels;
}

# set new default boot PXE
sub set_new_default {
  my ($default) = @_;
  substInFile {
    s/^DEFAULT.*/DEFAULT $default/;
  } $conf;
}

sub get_default_pxe {
  my ($defaultpxe) = cat_($conf) =~ /DEFAULT\s+(\S+)/;
  return $defaultpxe;
}

sub get_default_prompt() {
  my $line = cat_($conf);
  my ($prompt) = $line =~ /^PROMPT\s(\d+)/;
  $prompt and return $prompt;
}

sub get_default_timeout() {
  my ($time) = cat_($conf) =~ /^TIMEOUT\s(\d+)/;
  return $time;
}

# write conf in default file
sub write_conf_pxe {
  my $default = get_default_pxe;
  my $prompt = get_default_prompt;
  my $time = get_default_timeout;
  output($conf, <<EOF);
# DO NOT EDIT auto_generated by drakpxelinux.pl
PROMPT 10
DEFAULT $default
DISPLAY messages
TIMEOUT 50

label local
    LOCALBOOT 0
EOF

  foreach my $a (@listpxe) {
    if ($a->{interface} and ! $a->{display}) {
      append_to_file($conf, "
label $a->{label}
      KERNEL $a->{kernel}
      APPEND initrd=$a->{initrd} automatic=method:$a->{automatic},interface:$a->{interface},network:$a->{network},server:$a->{server},directory:$a->{directory} ramdisk_size=$a->{ramdisk} root=/dev/ram3 rw vga=$a->{vga}
");
    } elsif ($a->{interface} and $a->{display}) {
      append_to_file($conf, "
label $a->{label}
     KERNEL $a->{kernel}
     APPEND initrd=$a->{initrd} automatic=method:$a->{automatic},interface:$a->{interface},network:$a->{network},server:$a->{server},directory:$a->{directory} ramdisk_size=$a->{ramdisk} root=/dev/ram3 rw vga=$a->{vga} display=$a->{display}
");
      # case of .img image
    } else {
      append_to_file($conf, "
label $a->{label}
      KERNEL $a->{kernel}
      APPEND initrd=$a->{initrd}
");
    }
  }
}

sub create_model {
  get_items();
  my $model = Gtk2::ListStore->new(("Glib::String") x13);
  foreach my $a (@listpxe) {
    my $iter = $model->append;
    $model->set($iter,
		 COLUMN_LABEL, $a->{label},
		 COLUMN_INFO, $a->{info},
		 COLUMN_KERNEL, $a->{kernel},
		 COLUMN_INITRD, $a->{initrd},
		 COLUMN_AUTOMATIC, $a->{automatic},
		 COLUMN_INTERFACE, $a->{interface},
		 COLUMN_NETWORK, $a->{network},
		 COLUMN_SERVER, $a->{server},
		 COLUMN_DIRECTORY, $a->{directory},
		 COLUMN_RAMDISK, $a->{ramdisk},
		 COLUMN_VGA, $a->{vga},
		 COLUMN_DISPLAY, $a->{display},
		 COLUMN_OPTION, $a->{option},
		),
	      }
  return $model;
}

# wizard to add an entry in PXE menu
sub wizard_add_entry {
  my ($widget, $treeview) = @_;
  my $model = $treeview->get_model;

  local $::isEmbedded = 0;
  my $in = 'interactive'->vnew('su');
  undef $::WizardTable;
  undef $::WizardWindow;
  $::isWizard = 1;
  use wizards;
  my $WPXENAME = "Mandrakeclustering";
  my $WINFO = "install Mandrakeclustering";
  my $WALLRDZ = "/home/nis/install/clic/isolinux/alt0/all.rdz";
  my $WVMLINUZ = "/home/nis/install/clic/isolinux/alt0/vmlinuz";
  my $w = wizards->new;
  my $wiz = {
	     name => N("Add PXE entry"),
	     pages => {
		       welcome => {
				   name => N("Add an all.rdz boot image") . N("To boot through network, network computer need a boot image. Morever we need to name this image, so each boot image is related to a name in PXE menu. So user can choose wich image he wants to boot through PXE.") . "\n\n" . N("For technical reason, in case of multiple boot image, it's more simple to boot network computer through a kernel (vmlinuz), and provide one file with all drivers needed (in our case all.rdz).") ,
				   next => 'addimg',
				  },
		       addimg => {
				  name => N("At the end of this wizard, the all.rdz image and kernel vmlinuz will be copied into \n$IMGPATH.\n\nPXE menu list will be updated with this new entry"),
				  data => [
					   { label => "PXE label:", val => \$WPXENAME,
					     help => N("name displayed in PXE menu (please provide a word or a number, with no space)") },
					   { label => "PXE information:", val => \$WINFO,
					     help => N("PXE information is used to explain the rule of the boot image,\nie:\nMandrake 10 rescue disk\nMandrake cooker install via http") },
					   { label => "Full path to all.rdz image source:", val => \$WALLRDZ,
					     help => N("Provide the full path to all.rdz image source") },
					   { label => "Full path to vmlinuz source:", val => \$WVMLINUZ,
					     help => N("Provide the full path to vmlinuz kernel source") },
					  ],
				  complete => sub {
				    if (any { /^$WPXENAME :/ } cat_($PXEHELP)) {
				      err_dialog(N("Error !"), N("Found a similar entry in PXE list labeled : $WPXENAME.\nChoose another label please")) and return 'addimg';
				    }
				    if ($WPXENAME != /^\d+$/) {
				      err_dialog(N("Error !"), N("PXE label should be a name/number without space. Please adjust")) and return 'addimg';
				    }
				  },
				  next => 'endadd',
				  post => sub {
				    my $w = $in->wait_message(N("add a PXE entry"), N("ad a PXE entry in your PXE server configuration..."));
				    add_in_help($WPXENAME, $WINFO);
				    my $vmlinuzpxe = basename($WVMLINUZ) . "-$WPXENAME";
				    cp_af($WVMLINUZ, "$IMGPATH/$vmlinuzpxe");
				    cp_af($WALLRDZ, "$IMGPATH/$WPXENAME.rdz");
				    push @listpxe, {
					      label => $WPXENAME,
					      info => $WINFO,
					      kernel => "images/$vmlinuzpxe",
					      initrd => "images/$WPXENAME.rdz",
					      automatic => "",
					      interface => "",
					      network => "",
					      server => "",
					      directory => "",
					      ramdisk => "128000",
					      vga => "",
					      display => "",
					      option => "",
					     };
				    my $iter = $model->append;
				    $model->set($iter,
						COLUMN_LABEL, $listpxe[-1]{label},
						COLUMN_INFO, $listpxe[-1]{info},
						COLUMN_KERNEL, $listpxe[-1]{kernel},
						COLUMN_INITRD, $listpxe[-1]{initrd},
						COLUMN_AUTOMATIC, $listpxe[-1]{automatic},
						COLUMN_INTERFACE, $listpxe[-1]{interface},
						COLUMN_NETWORK, $listpxe[-1]{network},
						COLUMN_SERVER, $listpxe[-1]{server},
						COLUMN_DIRECTORY, $listpxe[-1]{directory},
						COLUMN_RAMDISK, $listpxe[-1]{ramdisk},
						COLUMN_VGA, $listpxe[-1]{vga},
						COLUMN_DISPLAY, $listpxe[-1]{display},
						COLUMN_OPTION, $listpxe[-1]{option},
					       );
				    write_conf_pxe();
				    undef $w;
				    return;
				  },
				  no_back => 1,
				 },
		       endadd => {
				  name => N("Congratulations"),
				  data => [ { label => N("The wizard successfully added the PXE boot image.") } ],
				  post => sub { list_label_pxe() },
				  no_back => 1,
				  end => 1,
				  next => 0,
				 },
		      }
	    };
  $w->process($wiz, $in);
  $::isWizard = 0;
}

# remove an entry in PXE menu
sub remove_item {
  my ($widget, $treeview) = @_;
  $::isWizard = 0;
  my $model = $treeview->get_model;
  my $selection = $treeview->get_selection;
  my $iter = $selection->get_selected;
  if ($iter) {
      my $path = $model->get_path($iter);
      my $i = ($path->get_indices)[0];
      ask_okcancel("Remove $listpxe[$i]{label} PXE entry ?") or return;
      remove_in_help($listpxe[$i]{label});
      my $ke = "$X86/$listpxe[$i]{kernel}";
      my $initrdf = "$X86/$listpxe[$i]{initrd}";
      if (basename($listpxe[$i]{kernel} ne "$X86/memdisk")) {
	print "k: $ke\n";
	print "initrd: $initrdf\n";
	system("rm -vf $ke");
	system("rm -vf $initrdf");
      } else {
	print "initrd: $initrdf\n";
	system("rm -vf $initrdf");
      }
      $model->remove($iter);
      splice @listpxe, $i, 1;
      write_conf_pxe;
    }
}

sub test_similar_label {
  my ($newlabel, $oldlabel) = @_;
#  if ($newlabel eq $oldlabel) {
#    err_dialog(N("hmm.."), N("You should provide a new label name.")) and return 0;
#  } els
  if (any { /^label\s$newlabel$/ } cat_($conf)) {
    err_dialog(N("Error !"), N("Found a similar entry in PXE list labeled : $newlabel.\nChoose another label please")) and return 0;
  } else { return 1 };
}

# dialog box to edit a PXE entry
sub edit_box_item {
  my ($widget, $treeview) = @_;
  $::isWizard = 0;
  my $model = $treeview->get_model;
  my $selection = $treeview->get_selection;

  my $iter = $selection->get_selected;
  if ($iter) {
    my $path = $model->get_path($iter);
    my $i = ($path->get_indices)[0];

    my $dialog = new Gtk2::Dialog();
    $dialog->set_modal(1);
    $dialog->set_resizable(FALSE);

#    my $label = Gtk2::Entry->new;
    my $label = Gtk2::Label->new($listpxe[$i]{label});
#   $label->set_text($listpxe[$i]{label});
#    my $oldlabel = $label;

    my $info = Gtk2::Entry->new;
    $info->set_text($listpxe[$i]{info});
    set_help_tip($info, 'info');

    my $kernel = Gtk2::Entry->new;
    $kernel->set_text($listpxe[$i]{kernel});
    set_help_tip($kernel, 'kernel');
    # file selection for kernel
    my $file_dialogk = new Gtk2::FileSelection(N("File Selection"));
    $file_dialogk->set_modal(TRUE);
    $file_dialogk->signal_connect("destroy", sub { $file_dialogk->hide });
    $file_dialogk->ok_button->signal_connect(clicked => sub {
					       my $file = $file_dialogk->get_filename;
					       -f $file or err_dialog(N("Error !"), "Should be a file") and return;
					       $file = "images/" . basename($file);
					       $kernel->set_text($file);
					       $file_dialogk->hide;
					     }, $file_dialogk);
    $file_dialogk->cancel_button->signal_connect(clicked => sub { $file_dialogk->hide });
    # button kernel
    my $buttonkernel = Gtk2::Button->new(N("Select kernel"));
    $buttonkernel->signal_connect(clicked => sub { $file_dialogk->show });

    my $initrd = Gtk2::Entry->new;
    $initrd->set_text($listpxe[$i]{initrd});
    set_help_tip($initrd, 'initrd');
    # file selection for initrd
    my $file_dialog = new Gtk2::FileSelection(N("File Selection"));
    $file_dialog->set_modal(TRUE);
    $file_dialog->signal_connect("destroy", sub { $file_dialog->hide });
    $file_dialog->ok_button->signal_connect(clicked => sub {
					      my $filei = $file_dialog->get_filename;
					      -f $filei or err_dialog(N("Error !"), "Should be a file") and return;
					      $filei = "images/" . basename($filei);
					      $initrd->set_text($filei);
					      $file_dialog->hide;
					    }, $file_dialog);
    $file_dialog->cancel_button->signal_connect(clicked => sub { $file_dialog->hide });
    my $buttoninitrd = Gtk2::Button->new("Select initrd");
    $buttoninitrd->signal_connect(clicked => sub { $file_dialog->show });

    # combo box to pop down automatic installation
    my $automatic = Gtk2::OptionMenu->new;
    $automatic->set_popdown_strings(@list_method);
    $automatic->entry->set_text($listpxe[$i]{automatic});
    set_help_tip($automatic, 'automatic');

    # combo box to pop down list of network interface
    my $interface = new Gtk2::OptionMenu();
    $interface->set_popdown_strings(@list_eth);
    $interface->entry->set_text($listpxe[$i]{interface});
    set_help_tip($interface, 'interface');

    my $network = Gtk2::Entry->new;
    $network->set_text($listpxe[$i]{network});
    set_help_tip($network, 'network');

    my $server = Gtk2::Entry->new;
    $server->set_text($listpxe[$i]{server});
    set_help_tip($server, 'server');

    my $directory = Gtk2::Entry->new;
    $directory->set_text($listpxe[$i]{directory});
    set_help_tip($directory, 'directory');

    my $ramdisk = new Gtk2::OptionMenu();
    $ramdisk->set_popdown_strings(@list_ram);
    $ramdisk->entry->set_text($listpxe[$i]{ramdisk});
    set_help_tip($ramdisk, 'ramsize');

    my $vga = new Gtk2::OptionMenu();
    $vga->set_popdown_strings(@list_vga);
    $vga->entry->set_text($listpxe[$i]{vga});
    set_help_tip($vga, 'vga');

    my $display = Gtk2::Entry->new;
    $display->set_text($listpxe[$i]{display});
    set_help_tip($display, 'display');

    my $option = Gtk2::Entry->new;
    $option->set_text($listpxe[$i]{option});
    set_help_tip($option, 'option');

    my %size_groups = map { $_ => Gtk2::SizeGroup->new('horizontal') } qw(label widget button);
    my $label_and_widgets = sub {
      my ($label, $widget, $button) = @_;
      gtkpack_(Gtk2::HBox->new(0,5),
	       0, gtkadd_widget($size_groups{label}, $label),
	       1, gtkadd_widget($size_groups{widget}, $widget),
	       2, gtkadd_widget($size_groups{button}, $button),
	      );
    };

    # ok, lets create the dialog box :-)
    gtkpack_($dialog->vbox,
	     0, gtkadd(Gtk2::Frame->new(N("Descrition")),
		       gtkpack_(gtkset_border_width(Gtk2::VBox->new, 3),
				0, $label_and_widgets->(N("Label"), $label, ""),
				0, $label_and_widgets->(N("Info"), $info, ""),
			       ),
		      ),
	     0, gtkadd(Gtk2::Frame->new(N("Boot image")),
		       gtkpack_(gtkset_border_width(Gtk2::VBox->new, 3),
				0, Gtk2::Label->new("Path are relative to $X86"),
				0, $label_and_widgets->(N("Kernel"), $kernel, $buttonkernel),
				0, $label_and_widgets->(N("Initrd"), $initrd, $buttoninitrd),
			       ),
		      ),
	     0, gtkadd(Gtk2::Frame->new(N("Automatic Options")),
		       gtkpack_(gtkset_border_width(Gtk2::VBox->new, 3),
				0, $label_and_widgets->(N("Automatic"), $automatic, ""),
                                0, $label_and_widgets->(N("Interface"), $interface, ""),
				0, $label_and_widgets->(N("Network"), $network, ""),
                                0, $label_and_widgets->(N("Server"), $server, ""),
				0, $label_and_widgets->(N("Directory"), $directory, ""),
			       ),
		      ),
	     0, gtkadd(Gtk2::Frame->new(N("Display")),
		       gtkpack_(gtkset_border_width(Gtk2::VBox->new, 3),
                                0, $label_and_widgets->(N("Vga"), $vga, ""),
                                0, $label_and_widgets->(N("Display"), $display, ""),
			       ),
		      ),
	     0, gtkadd(Gtk2::Frame->new(N("Other options")),
		       gtkpack_(gtkset_border_width(Gtk2::VBox->new, 3),
				0, $label_and_widgets->(N("Ramdisk"), $ramdisk, ""),
				0, $label_and_widgets->(N("Option"), $option, ""),
			       ),
		      ),
	     0, create_okcancel({
				 cancel_clicked => sub { $dialog->destroy },
				 ok_clicked => sub {
				   #ask_okcancel("are you sure you want to update all those values ?");
				   $listpxe[$i]{label} = $label->get_text;
				   $listpxe[$i]{info} = $info->get_text;
				   $listpxe[$i]{kernel} = $kernel->get_text;
				   $listpxe[$i]{initrd} = $initrd->get_text;
				   $listpxe[$i]{automatic} = $automatic->entry->get_text;
				   $listpxe[$i]{interface} = $interface->entry->get_text;
				   $listpxe[$i]{network} = $network->get_text;
				   $listpxe[$i]{server} = $server->get_text;
				   $listpxe[$i]{directory} = $directory->get_text;
				   $listpxe[$i]{ramdisk} = $ramdisk->entry->get_text;
				   $listpxe[$i]{vga} = $vga->entry->get_text;
				   $listpxe[$i]{display} = $display->get_text;
				   $listpxe[$i]{option} = $option->get_text;

				   # update value in cells
				   $model->set($iter,
					       COLUMN_LABEL, $listpxe[$i]{label},
					       COLUMN_INFO, $listpxe[$i]{info},
					       COLUMN_KERNEL, $listpxe[$i]{kernel},
					       COLUMN_INITRD, $listpxe[$i]{initrd},
					       COLUMN_AUTOMATIC, $listpxe[$i]{automatic},
					       COLUMN_INTERFACE, $listpxe[$i]{interface},
					       COLUMN_NETWORK, $listpxe[$i]{network},
					       COLUMN_SERVER, $listpxe[$i]{server},
					       COLUMN_DIRECTORY, $listpxe[$i]{directory},
					       COLUMN_RAMDISK, $listpxe[$i]{ramdisk},
					       COLUMN_VGA, $listpxe[$i]{vga},
					       COLUMN_DISPLAY, $listpxe[$i]{display},
					       COLUMN_OPTION, $listpxe[$i]{option},
					      );
				   $dialog->destroy;
				   write_conf_pxe;
				   add_in_help($listpxe[$i]{label}, $listpxe[$i]{info});
				 },
				},
			       ),
	    );
    $dialog->show_all;
  }
}

# add in help.txt
sub add_in_help {
  my ($NAME, $INFO) = @_;
  if (!any { /$NAME/ } cat_($PXEHELP)) {
    append_to_file($PXEHELP, <<EOF);
$NAME : $INFO
EOF

  } else {
    substInFile {
      s/$NAME.*/$NAME : $INFO/;
    } $PXEHELP;
  }
}

sub change_label_in_help {
  my ($NAMEOLD, $NEWNAME) = @_;
  substInFile {
    s/$NAMEOLD\s(.*)/$NEWNAME $1/;
  } $PXEHELP;
}

# remove entry in help.txt
sub remove_in_help {
  my ($NAME) = @_;
  substInFile {
    s/^$NAME\s:.*//x;
    s/^\s*$//;
  } $PXEHELP;
}

# edit cell direct on main interface
# disable due to bug in "cell edit placement" :-(
sub cell_edited {
  my ($cell, $path_string, $new_text, $model) = @_;
  my $path = Gtk2::TreePath->new_from_string($path_string);
  my $column = $cell->get_data("column");
  my $iter = $model->get_iter($path);

  if ($column == COLUMN_LABEL) {
    my $i = ($path->get_indices)[0];
    my $oldlabel = $listpxe[$i]{label};
    my $newlabel = $new_text;
    if (test_similar_label($newlabel, $oldlabel) eq "1") {
      change_label_in_help($oldlabel, $newlabel);
      $listpxe[$i]{label} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{label});
    }
  } elsif ($column == COLUMN_INFO) {
    my $i = ($path->get_indices)[0];
    $listpxe[$i]{info} = $new_text;
    $model->set($iter, $column, $listpxe[$i]{info});
  } elsif ($column == COLUMN_KERNEL) {
    my $i = ($path->get_indices)[0];
    if (-f $new_text) {
      $listpxe[$i]{kernel} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{kernel});
    }
  } elsif ($column == COLUMN_INITRD) {
    my $i = ($path->get_indices)[0];
    if (-f $new_text) {
      $listpxe[$i]{initrd} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{initrd});
    }
  } elsif ($column == COLUMN_AUTOMATIC) {
    my $i = ($path->get_indices)[0];
    if (!member($new_text, @list_method) or ($listpxe[$i]{kernel} =~ /memdisk/)) {
      return;
    } else {
      $listpxe[$i]{automatic} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{automatic});
    }
  } elsif ($column == COLUMN_INTERFACE) {
    my $i = ($path->get_indices)[0];
    if (member($new_text, @list_eth) or ($listpxe[$i]{kernel} != /memdisk/)) {
      $listpxe[$i]{interface} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{interface});
    }
  } elsif ($column == COLUMN_NETWORK) {
    my $i = ($path->get_indices)[0];
    if ($listpxe[$i]{kernel} != /memdisk/) {
      $listpxe[$i]{network} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{network});
    }
  } elsif ($column == COLUMN_SERVER) {
    my $i = ($path->get_indices)[0];
    if ($listpxe[$i]{kernel} != /memdisk/) {
      $listpxe[$i]{server} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{server});
    }
  } elsif ($column == COLUMN_DIRECTORY) {
    my $i = ($path->get_indices)[0];
    if (-d $new_text || ($listpxe[$i]{kernel} != /memdisk/)) {
      $listpxe[$i]{directory} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{directory});
    }
  } elsif ($column == COLUMN_RAMDISK) {
    my $i = ($path->get_indices)[0];
    if (member($new_text, @list_ram) || ($listpxe[$i]{kernel} != /memdisk/)) {
      $listpxe[$i]{ramdisk} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{ramdisk});
    }
  } elsif ($column == COLUMN_VGA) {
    my $i = ($path->get_indices)[0];
    if (member($new_text, @list_vga) || ($listpxe[$i]{kernel} != /memdisk/)) {
      $listpxe[$i]{vga} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{vga});
    }
  } elsif ($column == COLUMN_DISPLAY) {
    my $i = ($path->get_indices)[0];
    if ($listpxe[$i]{kernel} != /memdisk/) {
      $listpxe[$i]{display} = $new_text;
      $model->set($iter, $column, $listpxe[$i]{display});
    }
  } elsif ($column == COLUMN_OPTION) {
    my $i = ($path->get_indices)[0];
    $listpxe[$i]{option} = $new_text;
    $model->set($iter, $column, $listpxe[$i]{option});
  }
  write_conf_pxe;
}

sub show_help { info_dialog("help",
			    gtkpack_(gtkset_border_width(Gtk2::VBox->new, 3),
				     0, $help,
				     0, gtksignal_connect(set_help_tip(Gtk2::Button->new(N("online PXE documentation")), 'editb'), 
							  clicked => sub { open(F, "http://clic.mandrakesoft.com/documentation/pxe/ |") }
							 ),
				    ),
			   )
	      }

# adjust pxe confi with good value
sub pxe_conf {
  if (!-f "$pxeconf.orig") { cp_af($pxeconf, "$pxeconf.orig") }
  substInFile {
    s/default_address.*/default_address=$IPSERVER/;
    s/mtftp_address.*/mtftp_address=$IPSERVER/;
    s/domain.*/domain=$DOMAINNAME/;
  } $pxeconf;
}

# launch wizard to setup a PXE server
sub wizard_pxe_server {
  local $::isEmbedded = 0;
  my $in = 'interactive'->vnew('su');
  undef $::WizardTable;
  undef $::WizardWindow;
  $::isWizard = 1;
  use wizards;
  my $w = wizards->new;
  my $wiz = {
	     name => N("PXE Wizard"),
#	     init => sub {
#	       if (($DOMAINNAME eq "") or ($HOSTNAME eq "localhost")) {
#		 return 0, N("You need to readjust your domainname, not equal to localdomain or none. Please launch drakconnect to adjust it.")
#	       }
#	       1},
	     pages => {
		       welcome => {
				   name => N("PXE wizard") . "\n\n" . N("Set a PXE server.") . "\n" . N("This wizard will help you configuring the PXE server, and PXE boot image management. PXE (Pre-boot eXecution Environment) is a protocol designed by Intel that allows computers to boot through the network. PXE is stored in the ROM of new generation network cards. When the computer boots up, the BIOS loads the PXE ROM in the memory and executes it. A menu is displayed, allowing the computer to boot an operating system loaded through the network."),
				   no_back => 1,
				   next => 'pxeserver',
				  },
		       pxeserver => {
				     name => N("Set PXE server") . "\n\n" . N("We need to use a special dhcpd.conf file with PXE parameter. To set up such a DHCP server, launch the DHCP wizard and check the box 'Enable PXE'. If you don't do that, PXE query will not be answered by this server.") . "\n" . N("Now the wizard will configure all needed default configuration files to allow computers to boot through the network."),
				     no_back => 1,
				     next => 'summaryserver',
				    },
		       summaryserver => {
					 name => N("The wizard will now prepare all default files to set your PXE server"),
					 data => [
						  { label => N("TFTP directory:        $TFTPDIR") },
						  { label => N("Boot image path:       $IMGPATH") },
						  { label => N("PXE config file:       $pxeconf") },
						  { label => N("PXE help file:       $PXEHELP") },
						 ],
					 post => \&do_it_pxe,
					 no_back => 1,
					 next => 'endserver',
					},
		       endserver => {
				     name => N("Congratulations"),
				     data => [ { label => N("The wizard successfully configured your PXE server.") } ],
				     no_back => 1,
				     end => 1,
				     next => 0
				    },
		      },
	    };
  $w->process($wiz, $in);
}

# save old config with date
sub save_config {
  my ($old) = @_;
  my $DATE = chomp_(`date +%d-%m-20%y`);
  if (-f $old) {
    print " - Backup of $old configuration\n";
    cp_af($old, $old . '.' . $DATE);
  }
  return($old . '.' . $DATE);
}

# set default PXE message
sub default_pxe_messages {
  save_config($PXEMESSAGE);
  output($PXEMESSAGE, <<EOF);

                   Welcome to Mandrake Linux PXE Server
                                                       Pxelinux
              .              .-----------------------------------.
             /|\\            /    Press F1 for available images    \\
            /_|_\\           \\    Hosted by  $HOSTNAME
            \\ | /   _       /'-----------------------------------'
             \\|/   (')     /
              '.    U     /                                (O__
                 .   '.  /                 (o_  (o_  (0_   //\\
                 {o_   (o_  (o_  (o_  (o_  //\\  //\\  //\\  //  )
                 (')_  (`)_ (/)_ (/)_ (/)_ V_/_ V_/_ V_/_ V__/_
            ---------------------------------------------------------

 press F1 for help
EOF
}

sub default_pxe_menu {
  save_config($PXEHELP);
  output($PXEHELP, <<EOF);
Available images are:
---------------------
local: local boot
EOF
}

# can adjust block size in tftp server (ita64)
sub tftp_blksize {
  # $o should be with W or not N
  my ($o) = @_;
  if ($o =~ /W/) {
    substInFile { s/server_args.*/server_args = -r blksize -s $TFTPDIR/ } "$XINETDDIR/tftp";
  } else {
    substInFile { s/server_args.*/server_args = -s $TFTPDIR/ } "$XINETDDIR/tftp";
  }
}

# enable tftp server in xinetd conf
sub enable_tftps {
  substInFile { s/disable.*/disable = no/ } "$XINETDDIR/tftp";
}

sub memlinux_prep {
  if (!-f "$X86/memdisk") {
    cp_af($MEMDISK, $X86);
  }
}

sub check_started {
  my ($service) = @_;
  my ($isrunning) = chomp_(`pidof $service`) =~ /(\d+)/m;
  if (!$isrunning) {
    return 1;
  }
}

# main procedure to setup PXE server
sub do_it_pxe {
  return if $::testing;
  my $in = 'interactive'->vnew('su', 'PXE server');
  my $w = $in->wait_message(N("PXE server"), N("Configuring PXE server on your system..."));
  output("$X86/drakwizard_pxe", <<EOF);
do not remove
use to check if drakwizard PXE set server
has been launch.
EOF

  mkdir_p($IMGPATH);
  memlinux_prep();
  default_pxe_menu();
  default_pxe_messages();
  enable_tftps();
  pxe_conf();
  tftp_blksize('N');
  system("service $_ restart") foreach qw(xinetd pxe);
  undef $w;
  check_started($_) foreach qw(xinetd pxe);
}

# add colum to model
sub add_columns {
  my $treeview = shift;
  my $model = $treeview->get_model;
  # horrible hack to avoid: GLib-GObject-CRITICAL **: file gobject.c: line 1019 (g_object_set_property): assertion `G_IS_VALUE (value)' failed at drakpxe2.pl line 640.
  my $t = "0";
  my @j = qw/Label Info Kernel Initrd Method Interface Network Server Directory Ramdisk vga Display Extra Options/;
  each_index {
    my $renderer = Gtk2::CellRendererText->new;
    $renderer->set(editable => 0);
    $renderer->signal_connect(edited => \&cell_edited, $model);
    $renderer->set_data(column => $::i);
    $treeview->insert_column_with_attributes(-1, $j[$t], $renderer, 'text' => $::i);
    $t++;
  } (qw/x x x x x x x x x x x x x/);
#(("Label"), ("Info"), ("Kernel"), ("Initrd"), ("Method"), ("Interface"), ("Network"), ("Server"), ("Directory"), ("Ramdisk"), ("vga"), ("Display"), ("Extra"), ("Options"));
}

###############
# Main Program
###############

# check if first launch
if (! -f "$X86/drakwizard_pxe") {
  info_dialog("Please confure a PXE server", "Its seems this is the first time you run this tool.
A wizard will apppear to configure your pxe server.");
  eval { wizard_pxe_server() };
  my $err = $@;
  $::WizardWindow->destroy if defined $::WizardWindow;
  undef $::WizardWindow;
  if ($err && $err !~ /wizcancel/) {
    err_dialog(N("Error"), N("The PXE server wizard had unexpectly failled:") . "\n\n" . $err);
  }
}

# disable wizard
$::isWizard = 0;
# create model
my $model = create_model();

my $window = ugtk2->new("Drakpxelinux $version");
$window->{rwindow}->set_size_request(800, 400) unless $::isEmbedded; # seems embedded cause some pb
my $W = $window->{window};
$W->signal_connect(delete_event => sub { ugtk2->exit });

my $treeview = Gtk2::TreeView->new_with_model($model);
$treeview->set_rules_hint(TRUE);
$treeview->get_selection->set_mode('single');
add_columns($treeview);

# labels that list pxe menu entry
my $labels = new Gtk2::OptionMenu();
my @labels = list_label_pxe;
my $defaultlabel = get_default_pxe;
$labels->entry->set_text($defaultlabel);
$labels->entry->signal_connect("changed", sub { list_label_pxe; my $new_default = $labels->entry->get_text;
						set_new_default($new_default);
					      }
			      );

$labels->set_popdown_strings(@labels);
$labels->entry->set_text($defaultlabel);
set_help_tip($labels, 'labels');

my $banner = Gtk2::Banner->new('wiz_default_up', ("DrakPXE default configuration file"));
my $okcancel = create_okcancel({
				cancel_clicked => sub { ugtk2->exit },
				ok_clicked => &write_conf_pxe,
			       },
			      );
;

# main interface
$W->add(gtkpack_(Gtk2::VBox->new(0,0),
		 if_(! $::isEmbedded, 0, $banner ),
		 1, create_scrolled_window($treeview),
		 0, gtkpack_(gtkset_border_width(Gtk2::HBox->new, 3),
			     0, Gtk2::Label->new(N("boot:")),
			     0, $labels,
			     0, Gtk2::VSeparator->new(),
                             0, gtksignal_connect(set_help_tip(new Gtk2::Button(N("Wizard PXE server")), 'wizardsrv'),
						  clicked => sub {
						    eval { wizard_pxe_server() };
						    my $err = $@;
						    $::WizardWindow->destroy if defined $::WizardWindow;
						    undef $::WizardWindow;
						    if ($err && $err !~ /wizcancel/) {
						      err_dialog(N("Error"), N("The PXE server wizard had unexpectly failled:")
								 . "\n\n" . $err);
						    }
						  }),
			     0, Gtk2::VSeparator->new,
			     0, gtksignal_connect(new Gtk2::Button(N("Help")),
                                                  clicked => sub { show_help }
						 ),
			     0, Gtk2::VSeparator->new,
                             0, gtksignal_connect(set_help_tip(Gtk2::Button->new(N("Add PXE entry")), 'addpxe'), clicked => sub {
						    eval { wizard_add_entry($model, $treeview) };
						    my $err = $@;
						    $::WizardWindow->destroy if defined $::WizardWindow;
						    undef $::WizardWindow;
						    if ($err && $err !~ /wizcancel/) {
                                                      err_dialog(N("Error"), N("The add PXE entry wizard had unexpectly failled:")
                                                                 . "\n\n" . $err);
                                                    }
                                                  }),
                             0, gtksignal_connect(set_help_tip(Gtk2::Button->new((N"Remove PXE entry")), 'removepxe'), clicked => sub {
                                                    remove_item($model, $treeview); }
						 ),
			     0, gtksignal_connect(set_help_tip(Gtk2::Button->new(N("Edit PXE entry")), 'editb'), clicked => sub {
                                                    edit_box_item($model, $treeview); }
						 ),
			    ),
		 if_($::isEmbedded, 0, $okcancel),
		),
       );

$W->show_all;
Gtk2->main;