#!@PERL@

# Compare a set of AFM files from a given directory against a set
# available via a PATH. In PATH '//' are accepted (waterproof???)
# Examples:
#	compareafm fontinst/afm lw35nfss/texmf/adobe//afm
#       compareafm one two
# rcpt@urc.tue.nl / 6 Dec 1994

$AFMPATH = '@AFMPATH@';
$PATHSEP = ':';
@notavailable = ();
@afmfiles = ();

die "Usage: compareafm directory [path]\n" unless @ARGV > 0 && @ARGV < 3;

$AFMPATH = $ARGV[1] if @ARGV == 2;

die "Can't open $ARGV[0]\n" unless -d $ARGV[0] && -r $ARGV[0];
$afmdir = $ARGV[0];
opendir(AFMDIR, $afmdir);
@afmfiles = readdir(AFMDIR);
closedir(AFMDIR);

%months = (); $month = 0;
foreach $m ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
   'Sep', 'Oct', 'Nov', 'Dec') {
    $months{$m} = sprintf("%02d", ++$month);
}

foreach $font (@afmfiles) {
   next if ($font eq '.' || $font eq '..');
   next unless $font =~ m#\.afm$#;

   $font =~ s/\.afm$//;
   chop($name = `grep FontName "$afmdir/$font.afm"`);
   $name =~ s/FontName //;
   $absfontname = &findafm($name, $AFMPATH);
   if ($absfontname ne "") {
      &match($absfontname, "$afmdir/$font.afm");
   }
   else {
      push(@notavailable, $name);
   }
}
if (@notavailable > 0) {
   print "\n\nThe following AFM files are not available:\n";
   foreach $font (@notavailable) {
      print "\t$font\n";
   }
}

sub match {
   local($font1, $font2) = @_;
   local($different) = 0;
   local($C, $WX, $NAME);
   local($date1, $date2, $version1, $version2);

   $date1 = &date($font1); $date2 = &date($font2);
   $version1 = &version($font1); $version2 = &version($font2);

   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;
         $line{$NAME} = $_;
      }
      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) {
            if ($different == 0) {
               print "$font1 ($date1, $version1)\n";
               print "$font2 ($date2, $version2)\n";
               $different = 1;
            }
            print $line{$NAME};
            print $_;
         } 
      }
      last if /^EndCharMetrics/;
   }
   close(FONT);
}

# Convert Creation date into standard time
sub date {
   local($font) = @_;
   local($year, $month, $day, $dayofweek, $time);
   local($date) = "??";

   chop($_ = `grep "^Comment Creation Date" $font`);
   if (/^Comment Creation Date:/) {
      ($dayofweek, $month, $day, $time) =
         /^Comment Creation Date: ?(\S+)\s(\S+)\s(\S+)\s(\S+)/;
      ($year) = /\s+(\d+)$/;
      $date = "$year.$months{$month}.$day";
   }
   $date;
}

# get version
sub version {
   local($font) = @_;
   local($version) = "??";

   chop($_ = `grep "^Version " $font`);
   if (/^Version/) {
      ($version) = /^Version ?(\S+)/;
   }
   $version;
}

#
# 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 "";
}
