summaryrefslogtreecommitdiff
path: root/Master/tlpkg/dev/multi-updmap.pl
blob: 2d93452b621b3d2a9992ea7f597970c0c900cde7 (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
#!/usr/bin/env perl
# updmap: utility to maintain map files for outline fonts.
# 
#
# Copyright 2011 Norbert Preining
# This file is licensed under the GNU General Public License version 2
# or any later version.
#
# History:
# Original shell script (C) 2002 Thomas Esser
# first perl variant (C) Fabrice Popineau
# the original version where licensed under the following agreement:
# Anyone may freely use, modify, and/or distribute this file, without
# limitation.
#

my ($TEXMFROOT, $TEXMFMAIN, $TEXMFVAR);

BEGIN {
  $^W=1;
  $TEXMFROOT = `kpsewhich -var-value=TEXMFROOT`;
  if ($?) {
    print STDERR "updmap: Cannot find TEXMFROOT, aborting!\n";
    exit 1;
  }
  chomp($TEXMFROOT);
  chomp($TEXMFMAIN =`kpsewhich --var-value=TEXMFMAIN`);
  chomp($TEXMFVAR = `kpsewhich -var-value=TEXMFVAR`);
  chomp($TEXMFCONFIG = `kpsewhich -var-value=TEXMFCONFIG`);
  unshift (@INC, "$TEXMFROOT/tlpkg");
}

my $version = '$Id$';

use strict;
use TeXLive::TLUtils qw(mkdirhier mktexupd win32 log);
use Getopt::Long qw(:config no_autoabbrev ignore_case_always);
use Pod::Usage;

my $prg = TeXLive::TLUtils::basename($0);

# used for testing only
use Data::Dumper;

my %opts = ( quiet => 0, nohash => 0, nomkmap => 0 );
my $alldata;

my @cmdline_options = (
  "cnffile=s@", 
  "copy", 
  "disable=s@",
  "dvipdfmoutputdir=s",
  "dvipsoutputdir=s",
  "enable=s{1,2}",
  "edit",
  "force",
  "listavailablemaps",
  "listmaps|l",
  "nohash",
  "nomkmap",
  "dry-run|n",
  "outputdir=s",
  "pdftexoutputdir=s",
  "quiet|silent|q",
  # the following is a correct specification of an option according
  # to the manual, but it does not work!
  # we will treat that option by itself in processOptions
  #"setoption=s@{1,2}",
  "showoptions=s@",
  "syncwithtrees",
  "version",
  "help|h"
  );

my %settings = (
  dvipsPreferOutline    => [ qw/true false/ ],
  LW35                  => [ qw/URW URWkb ADOBE ADOBEkb/ ],
  dvipsDownloadBase35   => [ qw/true false/ ],
  pdftexDownloadBase14  => [ qw/true false/ ],
  dvipdfmDownloadBase14 => [ qw/true false/ ],
);

&main();

###############

sub main {
  processOptions();

  help() if $opts{'help'};

  if ($opts{'showoptions'}) {
    showOptions(@{$opts{'showoptions'}});
    exit 0;
  }

  # determine which config files shold be used
  # replaces the former "setupCfgFile"
  if ($opts{'cnffile'}) {
    for my $f (@{$opts{'cnffile'}}) {
      if (! -f $f) {
        die "$prg: Config file \"$f\" not found.";
      }
    }
  } else {
    @{$opts{'cnffile'}}  = `kpsewhich -all updmap.cfg`;
    chomp(@{$opts{'cnffile'}});
  }

  if ($opts{'listmaps'}) {
    listMaps();
    exit 0;
  }

  if ($opts{'syncwithtrees'}) {
    warning("NOT IMPLEMENTED syncwithtrees!\n");
    exit 1;
  }

  # we do changes always in the used config file with the highest
  # priority
  my ($top_config_file) = @{$opts{'cnffile'}};
  my $bakFile = $top_config_file;
  $bakFile =~ s/\.cfg$/.bak/;
  my $changed = 0;

  my $cmd;
  read_updmap_files(@{$opts{'cnffile'}});
  if ($opts{'edit'}) {
    # it's not a good idea to edit updmap.cfg manually these days,
    # but for compatibility we'll silently keep the option.
    $cmd = 'edit';
    my $editor = $ENV{'VISUAL'} || $ENV{'EDITOR'};
    $editor ||= (&win32 ? "notepad" : "vi");
    &copyFile($top_config_file, $bakFile);
    system($editor, $top_config_file);
    $changed = files_are_different($bakFile, $top_config_file);
  } elsif ($opts{'setoption'}) {
    $cmd = 'setOption';
    $changed = setOptions (@{$opts{'setoption'}});
  } elsif ($opts{'enable'}) {
    $cmd = 'enableMap';
    warning("NOT IMPLEMENTED enable!\n");
    exit 1;
  } elsif ($opts{'disable'}) {
    $cmd = 'disableMap';
    warning("NOT IMPLEMENTED disable!\n");
    exit 1;
  }

  if ($cmd && !$opts{'force'} && !$changed) {
    print "$top_config_file unchanged.  Map files not recreated.\n" 
      if !$opts{'quiet'};
  } else {
    if (!$opts{'nomkmap'}) {
      setupOutputDir("dvips");
      setupOutputDir("pdftex");
      mkMaps();
    }
    unlink ($bakFile) if (-r $bakFile);
  }

  my $updLSR = &mktexupd();
  $updLSR->{mustexist}(0);
  if (!$opts{'nohash'}) {
    print "$prg: Updating ls-R files.\n" if !$opts{'quiet'};
    $updLSR->{exec}();
  }
}


sub mkMaps {
  warning("NOT IMPLEMENTED mkMaps ... dumping data structure for now!\n");
  $Data::Dumper::Indent = 1;
  read_updmap_files(@{$opts{'cnffile'}});
  read_map_files();
  merge_data();
  print Data::Dumper->Dump([$alldata], [qw(mapdata)]);
}


sub processOptions {
  # first process the stupid setoption= s@{1,2} which is not accepted
  my $oldconfig = Getopt::Long::Configure(qw(pass_through));
  my @setoptions;
  GetOptions("setoption=s{1,2}" => \@setoptions) or
    die "Try \"$prg --help\" for more information.\n";
  Getopt::Long::Configure($oldconfig);

  # now revamp the @setoptions that they have the proper style
  my @realset;
  while (@setoptions) {
    my $t = shift @setoptions;
    if ($t =~ m/=/) {
      push @realset, $t;
    } else {
      my $tt = shift @setoptions;
      die "Try \"$prg --help\" for more information.\n"
        if !defined($tt);
      push @realset, "$t=$tt";
    }
  }
  if (@realset) {
    @{$opts{'setoption'}} = @realset;
  }

  # now continue with normal option handling

  GetOptions(\%opts, @cmdline_options) or 
    die "Try \"$prg --help\" for more information.\n";
}

# determines the output dir for driver from cmd line, or if not given
# from TEXMFVAR
sub setupOutputDir {
  my $driver = shift;
  if (!$opts{$driver . "outputdir"}) {
    if ($opts{'outputdir'}) {
      $opts{$driver . "outputdir"} = $opts{'outputdir'};
    } else {
      $opts{$driver . "outputdir"} = "$TEXMFVAR/fonts/map/$driver/updmap";
    }
  }
  my $od = $opts{$driver . "outputdir"};
  &mkdirhier($od);
  if (! -w $od) {
    die "$prg: Directory \"$od\" isn't writable: $!";
  }
  print "$driver output dir: \"$od\"\n" if !$opts{'quiet'};
  return $od;
}

###############################################################################
# setOption (@options)
#   parse @options for "key=value" (one element of @options)
#   we can only have "key=value" since that is the way it was prepared
#   in process_options
#   (These were the values provided to --setoption.)
#   
sub setOptions {
  my (@options) = @_;
  for (my $i = 0; $i < @options; $i++) {
    my $o = $options[$i];

    my ($key,$val) = split (/=/, $o, 2);
    
    die "$prg: unexpected empty key or val for options (@options), goodbye.\n"
      if !$key || !$val;
    &setOption ($key, $val);
  }
  return save_updmap($alldata->{'top_config'});
}

# returns 1 if actually saved due to changes
sub save_updmap {
  my $fn = shift;
  my %upd = %{$alldata->{'updmap'}{$fn}};
  if ($upd{'changed'}) {
    open (FN, ">$fn") || die "$prg: can't write to $fn: $!";
    # collect the lines with data
    my %line_to_setting;
    my %line_to_map;
    my @add_setting;
    my @add_map;
    if (defined($upd{'setting'})) {
      for my $k (keys %{$upd{'setting'}}) {
        if ($upd{'setting'}{$k}{'line'} == -1) {
          push @add_setting, $k;
        } else {
          $line_to_setting{$upd{'setting'}{$k}{'line'}} = $k;
        }
      }
    }
    if (defined($upd{'maps'})) {
      for my $k (keys %{$upd{'maps'}}) {
        if ($upd{'maps'}{$k}{'line'} == -1) {
          push @add_map, $k;
        } else {
          $line_to_map{$upd{'maps'}{$k}{'line'}} = $k;
        }
      }
    }
    my @lines = @{$upd{'lines'}};
    for my $i (0..$#lines) {
      if (defined($line_to_setting{$i})) {
        my $k = $line_to_setting{$i};
        my $v = $upd{'setting'}{$k}{'val'};
        print FN "$k $v\n";
      } elsif (defined($line_to_map{$i})) {
        my $m = $line_to_map{$i};
        my $t = $upd{'maps'}{$m}{'type'};
        my $p = ($upd{'maps'}{$m}{'status'} eq "disabled" ? "#! " : "");
        print FN "$p$t $m\n";
      } else {
        print "$lines[$i]\n";
      }
    }
    # add the new settings and maps
    for my $k (@add_setting) {
      my $v = $upd{'setting'}{$k}{'val'};
      print FN "$k $v\n";
    }
    for my $m (@add_map) {
      my $t = $upd{'maps'}{$m}{'type'};
      my $p = ($upd{'maps'}{$m}{'status'} eq "disabled" ? "#! " : "");
      print FN "$p$t $m\n";
    }
    close(FN) || warn("Cannot close file handle for $fn: $!");
    delete $alldata->{'updmap'}{$fn}{'changed'};
    return 1;
  }
  return 0;
}

###############################################################################
# setOption (conf_file, option, value)
#   sets option to value in the config file (replacing the existing setting
#   or by adding a new line to the config file).
#
sub setOption {
  my ($opt, $val) = @_;

  if ($opt eq "LW35") {
    if ($val !~ m/^(URWkb|URW|ADOBE|ADOBEkb)$/) {
      die "$0: Invalid value $val for option $opt.\n";
    }
  } elsif ($opt =~ 
m/^(dvipsPreferOutline|dvipsDownloadBase35|(pdftex|dvipdfm)DownloadBase14)$/) {
      if ($val !~ m/^(true|false)$/) {
        die "$0: Invalid value $val for option $opt; should be \"true\" or \"false\".\n";
      }
  } else {
    die "$0: Unsupported option $opt.\n";
  }

  # silently accept this old option name, just in case.
  return if $opt eq "dvipdfmDownloadBase14";
  
  #print "Setting option $opt to $val...\n" if !$quiet;
  my $tc = $alldata->{'top_config'};
  if (!defined($alldata->{'updmap'}{$tc})) {
    die "$prg: top config file $tc has not been read.";
  }
  if (defined($alldata->{'updmap'}{$tc}{'setting'}{$opt}{'val'})) {
    if ($alldata->{'updmap'}{$tc}{'setting'}{$opt}{'val'} eq $val) {
      return;
    }
    $alldata->{'updmap'}{$tc}{'setting'}{$opt}{'val'} = $val;
    $alldata->{'updmap'}{$tc}{'changed'} = 1;
  } else {
    $alldata->{'updmap'}{$tc}{'setting'}{$opt}{'val'} = $val;
    $alldata->{'updmap'}{$tc}{'setting'}{$opt}{'line'} = -1;
    $alldata->{'updmap'}{$tc}{'changed'} = 1;
  }
}


#############################################################################
# showOptions(item)
#   show Options for an item
#
sub showOptions {
  foreach my $item (@_) {
    if ($item eq "LW35") {
      print "URWkb URW ADOBE ADOBEkb\n";
    }
    elsif ($item =~ 
m/(dvipsPreferOutline|(dvipdfm|pdftex)DownloadBase14|dvipsDownloadBase35)/) {
      print "true false\n";
    }
    else {
      print "Unknown item \"$item\"; should be one of LW35, dvipsPreferOutline,\n" 
          . "  dvipsDownloadBase35, or pdftexDownloadBase14\n";
    }
  }
  exit 0
}

#
# listMaps
#
# list all maps in the format
# Maptype Mapname 'enabled'|'disabled' 'in' updmap_cfg_filename
#
sub listMaps {
  read_updmap_files(@{$opts{'cnffile'}});
  for my $m (keys %{$alldata->{'maps'}}) {
    my $origin = $alldata->{'maps'}{$m}{'origin'};
    print $alldata->{'updmap'}{$origin}{'maps'}{$m}{'type'}, " $m ",
      $alldata->{'updmap'}{$origin}{'maps'}{$m}{'status'}, " in $origin\n";
  }
}

###############################################################################
# copyFile()
#   copy file $src to $dst, sets $dst creation and mod time
#
sub copyFile {
  my ($src, $dst) = @_;
  my $dir;
  ($dir=$dst)=~s/(.*)\/.*/$1/;
  mkdirhier $dir;

  $src eq $dst && return "can't copy $src to itself!\n";

  open IN, "<$src" or die "$0: can't open source file $src for copying: $!";
  open OUT, ">$dst";

  binmode(IN);
  binmode(OUT);
  print OUT <IN>;
  close(OUT);
  close(IN);
  my @t = stat($src);
  utime($t[8], $t[9], $dst);
}

###############################################################################
# files_are_different(file_A, file_B[, comment_char])
#   compare two equalized files.
#
sub files_are_different {
  my $file_A=shift;
  my $file_B=shift;
  my $comment=shift;
  my $retval=0;

  my $A=equalize_file("$file_A", $comment);
  my $B=equalize_file("$file_B", $comment);
  $retval=1 unless ($A eq $B);
  return $retval;
}

###############################################################################
# equalize_file(filename[, comment_char])
#   read a file and return its processed content as a string.
#   look into the source code for more details.
#
sub equalize_file {
  my $file=shift;
  my $comment=shift;
  my @temp;

  open IN, "$file";
  my @lines = (<IN>);
  close IN;
  chomp(@lines);

  for (@lines) {
    s/\s*${comment}.*// if (defined $comment); # remove comments
    next if /^\s*$/;                           # remove empty lines
    s/\s+/ /g;     # replace multiple whitespace chars by a single one
    push @temp, $_;
  }
  return join('X', sort(@temp));
}


#################################################################
#
# reading updmap-cfg files and the actual map files
#
# the following hash saves *all* the information and is passed around
# we do not fill everything from the very beginning to make sure that
# we only read what is necessary (speed!)
#
# initialized by read_updmap_files
# $alldata->{'top_config'} = first element of the following list, where
#   all the changes are executed
# $alldata->{'order'} = [ list of updmap in decreasing priority ]
# $alldata->{'updmap'}{$full_path_name_of_updmap}{'lines'} = \@lines
# $alldata->{'updmap'}{$full_path_name_of_updmap}{'setting'}{$key}{'val'} = $val
# $alldata->{'updmap'}{$full_path_name_of_updmap}{'setting'}{$key}{'line'} = $i
# $alldata->{'updmap'}{$full_path_name_of_updmap}{'maps'}{$mapname}{'type'} 
#            = 'Map'|'MixedMap'|'disabled'
# $alldata->{'updmap'}{$full_path_name_of_updmap}{'maps'}{$mapname}{'status'} 
#            = 'enabled'|'disabled'
# $alldata->{'updmap'}{$full_path_name_of_updmap}{'maps'}{$mapname}{'line'} = $i
# $alldata->{'maps'}{$m}{'origin'} = $updmap_path_name
#
# initialized by read_map_files
# $alldata->{'maps'}{$m}{'fonts'}{$font} = $rest
#
# initialized by merge_data
# $alldata->{'merged'}{'setting'}{$key} = $val
# $alldata->{'merged'}{'allMaps'}{'fonts'}{$fontdef} = $rest
# $alldata->{'merged'}{'noMixedMaps'}{'fonts'}{$fontdef} = $rest
#

sub read_updmap_files {
  my (@l) = @_;
  for my $l (@l) {
    my $updmap = read_updmap_file($l);
    $alldata->{'updmap'}{$l}{'lines'} = $updmap->{'lines'};
    if (defined($updmap->{'setting'})) {
      for my $k (keys %{$updmap->{'setting'}}) {
        $alldata->{'updmap'}{$l}{'setting'}{$k}{'val'} = $updmap->{'setting'}{$k}{'val'};
        $alldata->{'updmap'}{$l}{'setting'}{$k}{'line'} = $updmap->{'setting'}{$k}{'line'};
      }
    }
    if (defined($updmap->{'maps'})) {
      for my $k (keys %{$updmap->{'maps'}}) {
        $alldata->{'updmap'}{$l}{'maps'}{$k}{'type'} = $updmap->{'maps'}{$k}{'type'};
        $alldata->{'updmap'}{$l}{'maps'}{$k}{'status'} = $updmap->{'maps'}{$k}{'status'};
        $alldata->{'updmap'}{$l}{'maps'}{$k}{'line'} = $updmap->{'maps'}{$k}{'line'};
      }
    }
  }
  $alldata->{'order'} = \@l;
  # get the first config file where we will do all the changes
  $alldata->{'top_config'} = $l[0];
  # first round determine which maps should be used and which type, as
  # different updmap.cfg files might specify different types of maps
  # (MixedMap or Map).
  # Again, we have to do that in reverse order
  for my $l (reverse @l) {
    if (defined($alldata->{'updmap'}{$l}{'maps'})) {
      for my $m (keys %{$alldata->{'updmap'}{$l}{'maps'}}) {
        $alldata->{'maps'}{$m}{'origin'} = $l;
      }
    }
  }
}

sub read_updmap_file {
  my $fn = shift;
  if (!open(FN,"<$fn")) {
    log("Cannot read $fn: $!");
    return;
  }
  # we count lines from 0 ..!!!!
  my $i = -1;
  my %data;
  my @lines = <FN>;
  chomp(@lines);
  $data{'lines'} = [ @lines ];
  close(FN) || warn("Cannot close $fn: $!");
  for (@lines) {
    $i++;
    chomp;
    next if /^\s*$/;
    next if /^\s*#$/;
    next if /^\s*#[^!]/;
    next if /^#![^ ]/;
    # allow for commands on the line itself
    s/([^#].*)#.*$/$1/;
    my ($a, $b, @rest) = split ' ';
    if ($a eq "#!") {
      if ($b eq "Map" || $b eq "MixedMap") {
        my $c = shift @rest;
        if (!defined($c)) {
          warning("updmap: apparently not a real disable line, ignored: $_\n");
        } else {
          if (defined($data{'maps'}{$c})) {
            warning("updmap: double mentioning of $c in $fn\n");
          }
          $data{'maps'}{$c}{'status'} = 'disabled';
          $data{'maps'}{$c}{'type'} = $b;
          $data{'maps'}{$c}{'line'} = $i;
        }
      }
      next;
    }
    if (@rest) {
      warning("updmap: line $i in $fn contains a syntax error, more than two words!\n");
    }
    if (defined($settings{$a})) {
      my @possible_values = @{$settings{$a}};
      if (scalar(grep($_ eq $b, @possible_values))) {
        $data{'setting'}{$a}{'val'} = $b;
        $data{'setting'}{$a}{'line'} = $i;
      } else {
        warning("updmap: unknown setting for $a: $b, ignored!\n");
      }
    } elsif ($a eq "Map" || $a eq "MixedMap") {
      if (defined($data{'maps'}{$b}) && $data{'maps'}{$b}{'type'} ne $a) {
        warning("updmap: double mentioning of $b with conflicting types in $fn\n");
      } else {
        $data{'maps'}{$b}{'type'} = $a;
        $data{'maps'}{$b}{'status'} = 'enabled';
        $data{'maps'}{$b}{'line'} = $i;
      }
    } else {
      warning("updmap: unrecognized line $i in $fn: $_\n");
    }
  }
  return \%data;
}

sub read_map_files {
  if (!defined($alldata->{'updmap'})) {
    return $alldata;
  }
  for my $f (keys %{$alldata->{'updmap'}}) {
    next if !defined($alldata->{'updmap'}{$f}{'maps'});
    for my $m (keys %{$alldata->{'updmap'}{$f}{'maps'}}) {
      # we do not read a map file multiple times, if $alldata{'maps'}{$m} is
      # defined we expect that it was read and do skip it
      next if defined($alldata->{'maps'}{$m}{'fonts'});
      my $ret = read_map_file($m);
      if (defined($ret)) {
        for my $font (keys %$ret) {
          $alldata->{'maps'}{$m}{'fonts'}{$font} = $ret->{$font};
        }
      }
    }
  }
}

sub read_map_file {
  my $mn = shift;
  my $fn = `kpsewhich $mn`;
  chomp($fn);
  return if (!$fn);
  open(MF,"<$fn") || return;
  my %data;
  while (<MF>) {
    chomp;
    next if /^\s*$/;
    next if /^\s*%/;
    my ($a, $b) = split(' ', $_, 2);
    $data{$a} = $b;
  }
  return \%data;
}


#
# merging the various font definitions
#
sub merge_data {
  my @l = @{$alldata->{'order'}};
  #
  # for security clean out everything that was there
  %{$alldata->{'merged'}} = ();
  #
  # first read in the settings
  # we read it in *reverse* order and simple fill up the combined data
  # thus if there are multiple definitions/settings, the one coming from
  # the first in the original list will win!
  for my $l (reverse @l) {
    # merge settings
    if (defined($alldata->{'updmap'}{$l}{'setting'})) {
      for my $k (keys %{$alldata->{'updmap'}{$l}{'setting'}}) {
        $alldata->{'merged'}{'setting'}{$k} = $alldata->{'updmap'}{$l}{'setting'}{$k}{'val'};
      }
    }
  }
  #
  # now merge the data
  #
  for my $m (keys %{$alldata->{'maps'}}) {
    my $origin = $alldata->{'maps'}{$m}{'origin'};
    next if ($alldata->{'updmap'}{$origin}{'maps'}{$m}{'status'} eq "disabled");
    for my $f (keys %{$alldata->{'maps'}{$m}{'fonts'}}) {
      $alldata->{'merged'}{'allMaps'}{'fonts'}{$f} = 
        $alldata->{'maps'}{$m}{'fonts'}{$f};
      $alldata->{'merged'}{'noMixedMaps'}{'fonts'}{$f} = 
        $alldata->{'maps'}{$m}{'fonts'}{$f}
          if ($alldata->{'updmap'}{$origin}{'maps'}{$m}{'type'} eq "Map");
    }
  }
}

sub warning {
  print STDERR @_;
}

#
# help, version etc etc
#

sub version {
  my $ret = sprintf "%s (TeX Live, multi) version %s\n", $prg, $version;
  return $ret;
}

sub help {
  my $usage = <<"EOF";
Usage: $prg     [OPTION] ... [COMMAND]
   or: $prg-sys [OPTION] ... [COMMAND]

Update the default font map files used by pdftex, dvips, and dvipdfm(x),
as determined by the configuration file updmap.cfg (the one returned by
running "kpsewhich updmap.cfg").

Among other things, these map files are used to determine which fonts
should be used as bitmaps and which as outlines, and to determine which
fonts are included in the output.

By default, the TeX filename database (ls-R) is also updated.

Options:
  --cnffile FILE            read FILE for the updmap configuration
  --dvipsoutputdir DIR      specify output directory (dvips syntax)
  --pdftexoutputdir DIR     specify output directory (pdftex syntax)
  --outputdir DIR           specify output directory (for all files)
  --copy                    cp generic files rather than using symlinks
  --force                   recreate files even if config hasn't changed
  --nomkmap                 do not recreate map files
  --nohash                  do not run texhash
  -n, --dry-run             only show the configuration, no output
  --quiet, --silent         reduce verbosity

Commands:
  --help                    show this message and exit
  --version                 show version information and exit
  --showoptions ITEM        show alternatives for options
  --setoption OPTION VALUE  set option, where OPTION is one of:
                             LW35, dvipsPreferOutline, dvipsDownloadBase35,
                             or pdftexDownloadBase14
  --setoption OPTION=VALUE  as above, just different syntax
  --enable MAPTYPE MAPFILE  add "MAPTYPE MAPFILE" to updmap.cfg,
                              where MAPTYPE is either Map or MixedMap
  --enable Map=MAPFILE      add \"Map MAPFILE\" to updmap.cfg
  --enable MixedMap=MAPFILE add \"MixedMap MAPFILE\" to updmap.cfg
  --disable MAPFILE         disable MAPFILE, whether Map or MixedMap
  --listmaps                list all active and inactive maps
  --listavailablemaps       same as --listmaps, but without
                             unavailable map files
  --syncwithtrees           disable unavailable map files in updmap.cfg

Explanation of the map types: the (only) difference between Map and
MixedMap is that MixedMap entries are not added to psfonts_pk.map.
The purpose is to help users with printers that render Type 1 outline
fonts worse than mode-tuned Type 1 bitmap fonts.  So, MixedMap is used
for fonts that are available as both Type 1 and Metafont.

Explanation of the --setoption possibilities:

  dvipsPreferOutline    true|false  (default true)
    Whether dvips uses bitmaps or outlines, when both are available.
  dvipsDownloadBase35   true|false  (default false)
    Whether dvips includes the standard 35 PostScript fonts in its output.
  pdftexDownloadBase14  true|false   (default true)
    Whether pdftex includes the standard 14 PDF fonts in its output.
  LW35                  URWkb|URW|ADOBEkb|ADOBE  (default URWkb)
    Adapt the font and file names of the standard 35 PostScript fonts.
    URWkb    URW fonts with "berry" filenames    (e.g. uhvbo8ac.pfb)
    URW      URW fonts with "vendor" filenames   (e.g. n019064l.pfb)
    ADOBEkb  Adobe fonts with "berry" filenames  (e.g. phvbo8an.pfb)
    ADOBE    Adobe fonts with "vendor" filenames (e.g. hvnbo___.pfb)

  These options are only read and acted on by updmap; dvips, pdftex, etc.,
  do not know anything about them.  They work by changing the default map
  file which the programs read, so they can be overridden by specifying
  command-line options or configuration files to the programs, as
  explained at the beginning of updmap.cfg.

Explanation of trees and files normally used:

  updmap both reads and writes TEXMFCONFIG/web2c/updmap.cfg, according to
  the actions specified.

  updmap writes the map files for dvips (psfonts.map) and pdftex
  (pdftex.map) to the TEXMFVAR/fonts/map/updmap/{dvips,pdftex}/
  directories.   

  The log file is written to TEXMFVAR/web2c/updmap.log.

  When updmap-sys is run, TEXMFSYSCONFIG and TEXMFSYSVAR are used
  instead.  This is the only difference between updmap-sys and updmap.

  Other locations can be used if overridden on the command line, or these
  trees don't exist, or you are not using the original TeX Live.

  To see the precise locations of the various files that
  will be read and written, give the -n option (or read the man page).

For step-by-step instructions on making new fonts known to TeX, read
http://tug.org/fonts/fontinstall.html.  For even more terse
instructions, read the beginning of updmap.cfg.

Report bugs to: tex-k\@tug.org
TeX Live home page: <http://tug.org/texlive/>
EOF
;
  print &version();
  print $usage;
  exit 0;
}

### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2 expandtab: #