#!@PERL@

# Make the installation of LaTeX PostScript fonts easy.
#
# Example:
#    fontinst zmtt
#
# rcpt@urc.tue.nl / Jan. 1995

$AFMPATH = '@AFMPATH@';
$FONTINSTDIR = '@FONTINSTDIR@';
$ZMATHTIMEDIR = '@ZMATHTIMEDIR@';
$PATHSEP = ':';

die "Usage: fontinst set ...\n" unless @ARGV > 0;

die "Can't open $FONTINSTDIR\n" unless -d $FONTINSTDIR && -r $FONTINSTDIR;

# do we have all ingredients?
die "No $FONTINSTDIR/inputs/fontinst.sty file available\n" 
   unless -r "$FONTINSTDIR/inputs/fontinst.sty";

# Don't hassle output
select(STDOUT); $| = 1;

# Collect all AFM files we will need
foreach $set (@ARGV) {

   open(MAP, "<mapfiles/$set.map");
   while (<MAP>) {
      next if /^#/;
      chop;

      print "Using '$line' from $psfontsmap\n" if $verbose;
      ($texname, $psname) = /^[ *]*(\S+)\s+(\S+)/;

      # Copy AFM file from our private set if we have one otherwise take
      # the one distributed with fontinst
      next if -r "$texname.afm"; # already loaded

      $absfontname = &findafm($psname, $AFMPATH);
      $theirafm = &findafm($psname, "$FONTINSTDIR/afm:$ZMATHTIMEDIR/afm");
      if ($absfontname ne "") { # take our own AFM file!
         print "Using private AFM file for $psname\n";
         system("cp $absfontname $texname.afm");
         if ($theirafm ne "" && &incompatible($absfontname, $theirafm)) {
               chop($cs1 = `./cs $absfontname`); 
               chop($cs2 = `./cs $theirafm`);
               print "Incompatibel AFM files, checksums $cs1 vs. $cs2\n";
         }
      }
      else { # take theirs
         if ($theirafm eq "") {
            die "AFM for $psname not available ",
                "but used in $set.map ($texname)\n";
         }
         print "Using AFM file supplied by fontinst for $psname\n";
         system("cp $FONTINSTDIR/afm/$texname.afm $texname.afm");
      }
   }
   close(MAP);
}

# Now run TeX for each of the fontsets
foreach $set (@ARGV) {
   # Copy required files into current directory
   if (-x "$set.req") {
      system("$set.req");
      die "$set.req: exit code ", ($? >>8) & 255, "\n" if $?;
   }

   if ($set eq "zmtt") {
      die "No $ZMATHTIMEDIR/fontzmtt.tex file available\n" 
         unless -r "$ZMATHTIMEDIR/fontzmtt.tex";
      system ("cp $ZMATHTIMEDIR/font$set.tex $set.tex");
      die "cp: exit code ", ($? >>8) & 255, "\n" if $?;
      $ENV{'TEXINPUTS'} = ".:$ZMATHTIMEDIR/inputs:$FONTINSTDIR/inputs";
   }
   else {
      open(FONTINST, ">$set.tex");
      print(FONTINST <<"FONTINST");
\\input fontinst.sty 
\\latinfamily{$set}{} 
\\bye
FONTINST
      close(FONTINST);
      $ENV{'TEXINPUTS'} = ".:$FONTINSTDIR/inputs";
   }

   # Now run tex
   system ("tex $set");
   die "tex: exit code ", ($? >>8) & 255, "\n" if $?;
}

#
# Next function returns the name of the file containing the wanted
# PostScript font <afmname> or an empty string if the font is not found
# in <afmfontpath>.
#
sub findafm {
   local($afmname, $afmfontpath) = @_;
   local($afmfname, $afmfile, $subdir, @subdirectories);
   local($firstdir, $lastdir);

   # Loop through $afmfontpath directories to see if we have one
   foreach (split(/$PATHSEP/, $afmfontpath)) {
      if (m,//$,) { s,//$,,;
         opendir(DIR, $_);
         @subdirectories=readdir(DIR);
         closedir(DIR);
         foreach $subdir (@subdirectories) {
            next if ($subdir eq '.' || $subdir eq '..');
            next unless -d "$_/$subdir";
            $afmfile = &findafm($afmname, "$_/$subdir");
            return $afmfile unless $afmfile eq '';
         }
         return "";
      }
      elsif (m,//,) {
         ($firstdir, $lastdir) = m,^(.*)//(.*)$,;
         opendir(DIR, $firstdir);
         @subdirectories=readdir(DIR);
         closedir(DIR);
         foreach $subdir (@subdirectories) {
            next if ($subdir eq '.' || $subdir eq '..');
            next unless -d "$firstdir/$subdir";
#print "looking for $afmname in $firstdir/$subdir/$lastdir\n";
            $afmfile = &findafm($afmname, "$firstdir/$subdir/$lastdir");
            return $afmfile unless $afmfile eq '';
         }
         return "";
      }
      next unless -d "$_";
      opendir(DIR, "$_");
      @fontfiles=readdir(DIR);
      closedir(DIR);
      foreach $font (@fontfiles) {
         next if ($font eq '.' || $font eq '..');
         next unless $font =~ m#\.afm$#;
         $afmfile = "$_/$font"; 
         chop($afmfname = `grep FontName $afmfile`);
         $afmfname =~ s/FontName //;
#print "found $afmfname in $afmfile\n" if $afmfname eq $afmname;
         return $afmfile if $afmfname eq $afmname;
      }
   }
   return "";
}

sub incompatible {
   local($font1, $font2) = @_;
   local($C, $WX, $NAME);
   local(%wx, %ch);

   open(FONT, "<$font1");
   while (<FONT>) {
      last if /^StartCharMetrics/;
   }
   %wx = (); %ch = (); %line = ();
   while (<FONT>) {
      if (/WX/) {
         ($C)    = /C +(\d+)/;
         ($WX)   = /WX +(\d+)/;
         ($NAME) = /N +(\w+)/;
         $wx{$NAME} = $WX;
         $ch{$NAME} = $C;
      }
      last if /^EndCharMetrics/;
   }
   close(FONT);

   open(FONT, "<$font2");
   while (<FONT>) {
      last if /^StartCharMetrics/;
   }
   while (<FONT>) {
      if (/WX/) {
         ($C)    = /C +(\d+)/;
         ($WX)   = /WX +(\d+)/;
         ($NAME) = /N +(\w+)/;
         if (!defined $wx{$NAME} || $wx{$NAME} != $WX || $ch{$NAME} != $C) {
            close(FONT);
            return 1;
         } 
      }
      last if /^EndCharMetrics/;
   }
   close(FONT);
   return 0;
}





