#! /usr/bin/perl

# Make AFMs for Ghostscript fonts
#
# Requires afmmaker.ps, which you can find at www.ftpsearch.com
# Expects the ghostscript fonts to live in directories defined
# in the environment variables GS_LIB or GS_FONTPATH
# 
# To use, just make a temporary directory, cd into it, and
# run this script.  Might want to check the configuration section
# below before you take off.  :-)
#

# ************************************************************************************
#     CONFIGURATION VARIABLES
#
# ************************************************************************************

# where is your  Ghostscript font map?  Following is default on RedHat
$fontmap = "/usr/share/ghostscript/5.10/Fontmap"; 

# where is the afmmaker.ps file?  
$afmmaker = "/usr/share/ghostscript/afmmaker.ps";

# what line in afmmaker.ps to search for?  If you got afmmaker.ps from
# source.robertk.com, then you don't need to change this.
$fontline = "/fontName /Verdana-BoldItalic def";

# the command to run ghostscipt (may need a full path if not in your shell path)
$gscommand = "ghostscript";

# whether or not to overwrite any existing AFMs.
# 0 means don't overwrite, 1 means overwrite
$overwriteAFMs = 0;

# and finally, whether or not to remove the generated AFMs from the
# current directory when the script finishes.
# 0 means don't remove, 1 means remove
$removeAFMs = 0;

# ************************************************************************************
#     END OF CONFIGURATION VARIABLES
#
#   It's probably best you don't change anything below this...
# ************************************************************************************

open (FMAP, $fontmap) || die "Can't open fontmap at $fontmap\n";
while($line = <FMAP>)
{
   chomp($line);
   if(substr($line,0,1) ne "%")  # skip comments
   {
      ($fontname, $filename) = split(/ /, $line, 2);
      $fontname  = substr($fontname, 1);
      $filename = substr($filename, index($filename, "("));
      $filename = substr($filename, 1, index($filename, ")")-1);
      if(length($filename) > 0)
      {
         open(AFMM, $afmmaker) || die "Can't open afmmaker.ps at $afmmaker\n";
         open(TEMPF, ">tempafmmaker.ps") || die "Can't create temp file!\n";
         while ($afmline = <AFMM>) 
         {
            chomp($afmline);
            $offset = index($afmline, $fontline);
            if ($offset >= 0) 
            {
               substr($afmline, $offset, length($fontline)) = "/fontName /$fontname def";
            }
            print TEMPF "$afmline\n";
         }
         close(TEMPF);
         if (index($filename, ".") > 0) 
         {
            $filename = substr($filename, 0, index($filename, "."));
            $filename = "$filename.afm";
         }
         print "Creating AFM for $fontname in $filename\n";
         $command = "$gscommand -q - -dNODISPLAY -dNOPAUSE <tempafmmaker.ps >$filename";
         `$command`;
      }
   }
}
# remove the temp file
`rm -f tempafmmaker.ps`;
close(FMAP);


# now copy the AFMs to their respective directories
# optionally overwrite any that are already there

$gslib = $ENV{'GS_LIB'};
$gsfp = $ENV{'GS_FONTPATH'};

$fontpath = "$gslib";
if (length($fontpath) > 0)
{
   $fontpath = "$fontpath:$gsfp";
}
else
{
   $fontpath = $gsfp;
}

@fontdirs = split(/:/, $fontpath);
$elems = $#fontdirs + 1;

print "Searching $elems font paths...\n";
for ($pathno=0;  $pathno <= $#fontdirs; $pathno++) 
{
   print "Searching $fontdirs[$pathno]...\n";
   @fnames = `ls $fontdirs[$pathno]`;
   for ($namei=0; $namei < $#fnames; $namei++) 
   {
      ($base, $extn) = split(/\./, $fnames[$namei]);
      $fullpathfont = "$fontdirs[$pathno]/$fnames[$namei]";
      $fnames[$namei] = "$base.afm";
      $fullpathafm = "$fontdirs[$pathno]/$fnames[$namei]";
      # for each file in directory, check AFMs.
         if ($overwriteAFMs == 0) 
         {
            if(-e $fullpathafm)
            {
               print "AFM $fullpathafm already exists. Skipping.\n";
            }
            else
            {
               print "Copying $fnames[$namei]\n";
               `cp $fnames[$namei] $fullpathafm\n`;
            }
         }
         else
         {
            print "Copying $fnames[$namei]\n";
            `cp $fnames[$namei] $fullpathafm`;
         }
     }
}

# clean up
if ($removeAFMs == 1) 
{
   print "Removing generated AFMs from current directory.\n"
   `rm -f *.afm`;
}

print "All done!\n"

