#!/usr/local/bin/perl
### ==================================================================== 
###  @Perl-file{ 
###	author          = "Alan Hoenig", 
###	version         = "0.7", 
###	date            = "January 1998", 
###	filename        = "mathkit", 
###	address         = "Department of Mathematics
###			   John Jay College
###			   445 West 59th Street
###			   New York, NY 10019
###			   USA", 
###	telephone       = "(516) 385-0736", 
###	email           = "ajhjj@cunyvm.cuny.edu", 
###	codetable       = "ISO/ASCII", 
###	supported       = "yes", 
###	abstract        = "This script constructs the scripts for installing
###			   math fonts combining outline letters and numerals 
###			   matching Metafont bitmaps for special symbols.",
###	package         = "MathKit", 
###	dependencies    = "perl," 
###  } 
### ==================================================================== 

## USAGE: perl mathkit <template> <font-fam> <encoding>
## Examples of use (assumes presence of tm.mkr, tm.mkb):
##   perl ../mathkit tm mnt OT1    
## or simply (if the script is made executable)
##   ../mathkit tm mnt OT1; 

## Some system parameters...

$version = "0.7";
$noslant = 0;
$slanted = 1;

$true =    1;
$false =   0;

## Routines to make conversion between DOS and Unix simple..
##   $mv is the form of the `move' command.
##   $sep is the path separator for the system.
##   $q is the quote character to surround command line parameters and
##     prevent expansion by the operating system.
##   $comment is the string that begins a comment in a system file.

sub UNIX{$mv="mv"; $sep="/"; $q="\'"; $comment="##";}
sub DOS{$mv="domove"; $sep="\\"; $q=""; $comment="REM";}

require("..\\mathkit.par");	# get parameters (system dependent)

if (not $sans_font) {
    $sans_font = "cmss";
    $sans_supplier = "public";
    $sans_typeface = "cm";
    $sans_installer = "base";
}

$rm_tfm = $tfm;			# where are roman tfm's?
$cm_tfm = $tfm;			# where are CM tfm's?
$sf_tfm = $tfm;			# where are sans serif tfm's?
$plain_inputs = $inputs;	# where do plain styles go?
$rm_inputs = $inputs;

$pkext = "pk"; $gfext = "gf";	# DOS defaults
if ($sep ne "\\") {
    $pkext = "${dpi}pk";
    $gfext = "${dpi}gf";
}

if ($texmf) {			# for TDS systems only...
    $pk = "$texmf${sep}fonts${sep}pk${sep}$mode${sep}mathkit";
    $pk = "$pk${sep}z$ARGV[0]$ARGV[1]${sep}dpi$dpi";
    $vf = "$texmf${sep}fonts${sep}vf${sep}mathkit${sep}z$ARGV[0]$ARGV[1]";
    $tfm = "$texmf${sep}fonts${sep}tfm${sep}mathkit${sep}z$ARGV[0]$ARGV[1]";
    $mfinputs = "$texmf${sep}fonts${sep}source";
    $rm_inputs = "$texmf${sep}tex${latex}${sep}$installer";
    $inputs = "$texmf${sep}tex${sep}latex${sep}mathkit";
    $plain_inputs = "$texmf${sep}tex${sep}plain${sep}mathkit";
    $afm = "$texmf${sep}fonts${sep}afm${sep}$supplier$sep$typeface";
    $rm_tfm = "$texmf${sep}fonts${sep}tfm${sep}$supplier$sep$typeface";
    $cm_tfm = "$texmf${sep}fonts${sep}tfm${sep}public${sep}cm";
    $sf_tfm = 
	"$texmf${sep}fonts${sep}tfm${sep}$sans_supplier$sep$sans_typeface";
}

open (MKDIR, ">mkdirs.bat");
&banner("MKDIR", "mkdirs.bat");
$mkdir = "mkdir";		# system dependent `mkdir' command
sub check_dir{			# does a directory exist?
    local($thisdir) = $_[0];
    if (not -d $thisdir) {
	push(@missingdirs, $thisdir);
    }
}

sub check_dirs{			# does a list of directories exist?
    foreach $dir (@_) {
	&check_dir($dir);
    }
    foreach $miss (@missingdirs) {
	$misss = "$miss$sep";
	$Sep = "$sep";
	$Sep = "\\\\" if $sep eq "\\"; # special case for DOS
	while ($misss =~ m/$Sep/g) {
	    if (($know_about{$`} == $false) and (not -d $`)) {
		print MKDIR "$mkdir $`\n" unless $` eq "";
		$know_about{$`} = $true;
	    }
	}
    }
    close MKDIR; undef %know_about;
}
&check_dirs(
    $tfm,
    $rm_tfm,
    $cm_tfm,
    $sf_tfm,
    $afm,
    $pk,
    $vf,
    $psfonts,
    $mfinputs,
    $inputs,
    $plain_inputs,
    $rm_inputs,
	    );
    
## Some bookkeeping with respect to date and time...

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$thisday=(Sun,Mon,Tues,Wednes,Thurs,Fri,Satur)[$wday];
$thisday="${thisday}day";
$month=(January,February,March,April,May,June,July,August,
  September,October,November,December)[$mon];
$fullyear="19$year";
if ($year<50) {$fullyear="20$year";}
sub timestamp{
  if ($min<10) {$min="0$min";}
  $currtime="$hour:$min";
  $currdate="$thisday, $month $mday, $fullyear";
}
&timestamp;

## Some utility routines...

sub display{			# prints its argument on screen
    foreach $thingy (@_) {
	print $thingy;
    }
}

sub log{			# prints arguments in log file
    foreach $thingy (@_) {
	print LOG $thingy;
    }
}

sub record{			# logs and prints arguments
    foreach $thingy (@_) {
	print $thingy;
	print LOG $thingy;
    }
}

$FH_ = "STDOUT";			# default value
sub out{			# prints to a given file.
    foreach $str (@_){
	print $FH_ $str;
    }
}
sub copy{
  die "`copy' can't find file $_[0]" unless -e $_[0];
  open(COPYIN, $_[0]); # the source
  open(COPYOUT, ">$_[1]"); # the target
  while (<COPYIN>) {print COPYOUT $_;}
  close COPYOUT;
}

sub safecopy{# copy source to target ONLY if target doesn't exist either in
    ## current dir or in parent dir
    if ((not -e $_[1]) and (not -e "..$sep$_[1]")) { # target doesn't exist
	&copy(@_);
    } else {
	&record("Copy operation skipped; file $_[1] already exists.\n");
    }
}

## We need a routine to print a banner on files and scripts
## that MathKit makes.  Arguments are: filehandle, comment char,
## file name.  Usage: &printbanner("TEX", "%", "testmath.tex")

sub printbanner{
    local($fh_,$comment_,$filename)=($_[0],$_[1],$_[2]);
    print $fh_ "$comment_ This is file $filename,";
    print $fh_ " created by MathKit$version + Perl$].\n";
    print $fh_ "$comment_ It was created at $currtime on $currdate.\n";
    print $fh_ "$comment_ \n";
    print $fh_ "$comment_ YOU MAY DELETE THIS FILE AFTER USE.\n";
    print $fh_ "$comment_ \n";
}

sub banner{# prints banner in auxiliary files.
    &printbanner($_[0], $comment, $_[1]);
}

## Get command line params...

  $start_up =<<"EndStart";
MathKit needs three arguments to work.  Firstly, I need to know the name
of the template I should use.  At the moment, there are three templates
to choose from:

    1. bv (Baskerville-like);
    2. tm (Times Roman-like); and
    3. pl (Palatino-like).

I also need to know the name of the Roman font to use.  This should be the
three-character abbreviation under which this family was installed---some-
thing like 'mbv' (Baskerville), 'pad' (Adobe Garamond), 'ppl' (Palatino),
or 'ptm' (Times) is what I am looking for.  Finally, I need an encoding
scheme, either 'OT1' or 'ot1'.  As a result, the command line to get me 
going should look something like this:

    ../mathkit bv mbv OT1

Please consider reading the file 'mathkit.tex' (a LaTeX file) for further
important information.  (These comments are stored in the file 'todo.1st'.)

EndStart

if ($#ARGV != 2) {
    open (TODO, ">todo.1st");  
    print TODO $start_up;
    print $start_up;
    close TODO;
    die "Fix me and start again.";
}
$tseries=$ARGV[0]; # the template series
$tser=substr($tseries,0,1); # 1st char becomes $tseries abbreviation
$fontfam=$ARGV[1]; # font family (using Berry name)
open (LOG, ">z$tseries$fontfam.klg"); # open log file
$encoding=$ARGV[2];# which encoding? (T1 or OT1 only)

## Print banner, first off...

&record("This is MathKit$version + Perl$] at $currtime on $currdate.\n\n");
&record("We are using MathKit template $tseries.\n");
&record("We are using font family $fontfam.\n");
&record("The encoding is $encoding.\n");


$enc = 8;            # set the encoding `digit'
$enc = 7 if $encoding =~ /o/i;

## Checking and retrieving font template information...

if (-e "..${sep}${tseries}.mkr") {# get template for Roman fonts
  $template[0]="..${sep}${tseries}.mkr";
} else {# template doesn't exist!
  &record("Where's the template file $tseries?  Dying...\n");
  die "At least, I need to see $tseries.mkr in the parent directory.";
}

## We need only one bold template (if there is at least one).  We try
## bold first, but prefer the semibold...

if (-e "..${sep}${tseries}.mkb") {# get bold template
  $template[1]="..${sep}${tseries}.mkb";
##   $bld = "b";
##   $nfssbld = "b";
} 
if (-e "..${sep}${tseries}.mks") {# get semibold template
  $template[1]="..${sep}${tseries}.mks";
##   $bld = "s";
##   $nfssbld = "sb";
} 

## We need to determine the proper boldweight to use...
## The default:
$bld = "b";
$nfssbld = "b";
## Now to test for the semibold (we prefer to use the semibold if the
## Roman semibold fonts are present.)
if (-e "$afm${sep}${fontfam}s8a.afm") {
    $bld = "s";
    $nfssbld = "sb";
    &record("Text fonts: We are using semibold weights in this run.\n");
} else {
    &record("Text fonts: We are using bold weights in this run.\n");
}

## Now to check font info...

$fullafm = "$afm${sep}${fontfam}r8a.afm";
$expafm = "$afm${sep}${fontfam}r8x.afm";
if (not -e $fullafm) {
    &record("I'm missing font info in $fullafm. Help...\n");
    die "I need font info to continue...";
}
$exp = 1 if -e $expafm;		# Do real experts exist?

## End of preliminary processing...

## Subroutines follow... 

sub afm_metric_info{
    local($target_char, $afm_file) = @_;
    open (TMP, "$afm$sep$afm_file.afm") or
	die "Can't find $afm$sep$afm_file.afm.  Dying...";
    local($h, $d) = (-999,999);	# default values
    while (<TMP>) {
	chomp $_;
	last if /EndCharMetrics/;
	next unless /^C\s+\d+/;
	/C\s+\d+\s+\; WX\s+\d+ \; N ([A-Za-z]+) \; B -?\d+ (-?\d+) \d+ (\d+) \;/;
	if ($1 eq $target_char) {
	    $h = sprintf "%5.1f", 36*$3/100;
	    $d = sprintf "%5.1f", 36*$2/100;
	    $h =~ s/\.0$//;	# strip off final `.0'
	    $d =~ s/\.0$//;
	    $h = "$h/36pt\#";	# add Metafont grammar
	    $d = "$d/36pt\#";
	}
    }
    close TMP;
    ($h, $d);
}

## The routine get_fontfam_parameters reads the afm file to get 
## values for seven properties of the font that enter into a
## Metafont font description.  (The only 2 we can't extract are
## the bar height and math axis.)  We only use this information, however,
## if the parameter $should_try_to_match is set to a non-null, non-zero
## value in the file `mathkit.par'.  
##    The parameter for this routine is a the Berry weight---usually
## `r', or `b', or `s'---of the scalable fonts we are matching.

sub get_fontfam_parameters{
    local($tmp);
    local($afmfile) = "${fontfam}$_[0]8a";
    ($body_height, $tmp) = &afm_metric_info("parenleft", $afmfile);
    ($asc_height, $tmp) = &afm_metric_info("h", $afmfile);
    ($cap_height, $tmp) = &afm_metric_info("B", $afmfile);
    ($fig_height, $tmp) = &afm_metric_info("5", $afmfile);
    ($x_height, $tmp) = &afm_metric_info("x", $afmfile);
    ($tmp, $comma_depth) = &afm_metric_info(",", $afmfile);
    ($tmp, $desc_depth) = &afm_metric_info("g", $afmfile);
}

sub adjust_font_parameters{
    local(@tmp) = ("body_height", "asc_height", "cap_height", "fig_height",
	       "x_height", "comma_depth", "desc_depth");
    local($temp);
    foreach $strng (@tmp) {
	$temp = "$str\#";
	$param{$temp} = $$temp;
    }
}


sub get_template_info{
    open (IN, "$_[0]"); 
    undef %param; # just in case
    while ($_ = <IN>) {
	if ($_ =~ /^([a-zA-Z_#]+):=([0-9a-z\/\.#]+);/) {
		      $param{$1} = "$2"; # 
	    
	}
    }
    close IN;
}

## Some comments about the special fonts (cal, tt, frak, bb, sans serif):
## Calligraphic is easiest---we pluck them from the cmsy font, and they
## already have been properly scaled.  Typewriter tt fonts come from
## cmtt10 which we assume to exist in a scalable version. 
## Fraktur comes from the scalable version of eufm10 (BaKoMa font).
## Blackboard bold will be generated only if the mf source exists;
## bb comes from Alan Jeffrey's bbold10 font.
## 
## Sans serif fonts are a very special case, as they are almost always
## part of a family of sans serif fonts.  MathKit examines the $ss_font 
## variable in the .par file.  If it doesn't exist, no sans serif is taken.  

$cm_x_height = "155/36pt\#"; 
$cm_xht_num = "155";
$cmss_x_height = "160/36pt\#"; 
$cmss_xht_num = "160";
$frak_x_height = "169.6/36pt\#";
$frak_xht_num = "169.6";
	$newbbold =<<"endnewbbold";
\% Font ${tseries}bb10 is modified Blackboard bold 10pt.
mode_setup;
input bbbase;
% Hacks to make sure cmr doesn't generate the font, and doesn't load cmbase.
cmbase := 1;
def generate suffix t = enddef;
% Input the cmr parameters.
input ${tseries}r10;
font_identifier := "${tseries}-BBOLD";
font_coding_scheme := "Blackboard bold";
% Then generate blackboard bold.
input bbold;
bye;
endnewbbold

sub get_font_fam{
    local($fontfam, $fontenc) = ("cmss", "7t");	# default
    local($font) = $_[0];
    if (not $font =~ /^cmss/) {
	$fontfam = substr $font, 0, 3;
	$fontenc = substr $font, 4, 99;
    }
    ($fontfam, $fontenc);
}
    
## First, define some fudge factors for better visual compatibility
## during the scaling process...
$fudge_frak  = 1.03;
$fudge_tt    = 1.0;
$fudge_bb    = 1.0;
$fudge_sans  = 1.03;

sub do_special_fonts{
    ($xht_num, $tmp) = split /\//, $x_height;
    # Nothing special for calligraphic...
    # Now for tt...
    $ttplace = $psfonts;		# traditional
    $ttplace = "$texmf$sep$fonts${sep}type1${sep}public${sep}cm"
	if $texmf;		# TDS
    $ttfont = "$ttplace${sep}cmtt10";
    if ((-e "$ttfont.pfb") or (-e "$ttfont.pfa")) {
	$tt = $true;
	$tt_sf = $xht_num / $cm_xht_num;
	$tt_sf *= $fudge_tt;
	$tt_sf = sprintf "%4.2f", $tt_sf;
	$ttsize = 10*$tt_sf;
	$ttsize = sprintf "%4.1f", $ttsize;
	$replace{"<ttsize>"} = $ttsize;
	&record("Source file $ttfont found:\n  ",
		"scaling cmtt10 by a factor of $tt_sf.\n");	
    } else {
	&record("No tt font source found; no tt font will be provided.\n"); 
    }
    # Now for fraktur...
    $frakplace = $psfonts;		# traditional
    $frakplace = "$texmf$sep$fonts${sep}type1${sep}ams${sep}euler"
	if $texmf;		# TDS
    $frakfont = "$frakplace${sep}eufm10";
    if ((-e "$frakfont.pfb") or (-e "$frakfont.pfa")) {
	$fr = $true;
	$frak_sf = $xht_num / $frak_xht_num;
	$frak_sf *= $fudge_frak;  
	$frak_sf = sprintf "%4.2f", $frak_sf;
	$frsize = 10*$frak_sf;
	$frsize = sprintf "%4.1f", $frsize;
	$replace{"<frsize>"} = $frsize;
	$replace{"<frsf>"} = $frak_sf;
	&record("Source file $frakfont found:\n  ",
		"scaling eufm10 by a factor of $frak_sf.\n");	
    } else {
	&record("No fraktur font found; and none will be provided.\n"); 
    }
    # Now for blackboard bold...
    $bbplace = $mfinputs;		# traditional
    $bbplace = "$texmf$sep$fonts${sep}source${sep}public${sep}bbold"
	if $texmf;		# TDS
    $bbsource = "$bbplace${sep}bbold10.mf";
    if (-e $bbsource) {
	$bb = $true;
	$bb_sf = $tt_sf;
	$bb_sf *= $fudge_bb;
	$bb_sf = sprintf "%4.2f", $bb_sf;
	&record("Source file $bbsource found:\n  ",
		"scaling bbold10 by a factor of $bb_sf.\n");	
	open (OUT, ">${tseries}bb10.mf");
	&printbanner("OUT", "%", "${tseries}bb10.mf");
	print OUT $newbbold;
	close OUT;
	print MF "$mf ${q}\\mode=$mode; scrollmode; input ${tseries}bb10$q\n";
	print GFTOPK "gftopk ${tseries}bb10.$gfext ${tseries}bb10.$pkext\n";
    } else {
	&record("No bbold font found; and none will be provided.\n"); 
    }
    # Now for sans serif fonts...
    if ($sans_font) {
	$sa = $true;
	($sansfam, $sansencoding) = &get_font_fam($sans_font);
	if ($sansfam eq "cmss") {
	    $sans_sf = $xht_num / $cmss_xht_num;
	    $sans_sf = sprintf "%4.2f", $sans_sf;
	    &record("CMSS sans serif scaled by $sans_sf will be used.\n");
	} else {		# compute sf for non-cm sans serif
	    $replace{"<ssr>"} = $sans_font;
	    $replace{"<ssri>"} = "${sansfam}ri$sansencoding";
	    $sansbold = "b";
	    $sansbold = "s" if -e "$sf_tfm$sep${sansfam}s$sansencoding.tfm";
	    $replace{"<ssb>"} = "$sansfam${sansbold}$sansencoding";
	    $replace{"<ssbi>"} = "$sansfam${sansbold}i$sansencoding";
	    ($sans_x_height, $tmp) = &afm_metric_info("x", "${sansfam}r8a"); 
	    ($sans_xht_num, $tmp) = split /\//, $sans_x_height;
	    $sans_sf = $xht_num / $sans_xht_num;
	    $sans_sf *= $fudge_sans;
	    $sans_sf = sprintf "%4.2f", $sans_sf;
	    $sasize = 10*$sans_sf;
	    $sasize = sprintf "%4.1f", $sasize;
	    $replace{"<sasize>"} = $sasize;
	    &record("Sans serif family $sansfam (contining font $sans_font) ",
		"scaled by $sans_sf will be used.\n");
	}
    } else {
	&record("No sans serif font found; and none will be provided.\n"); 
    }
}

## Making Metafont source files...

## When we make Metafont source files, the easiest case occurs 
## when there is a comparable cm file we can copy.  For example, 
## in a times-like (tm) setting, we create tmmi7.mf by copying cmmi7.mf
## and simply making suitable changes to the Metafont parameters.
## In this case the Perl subroutine `makemffiles' calls upon the 
## routine `normalcopy' to perform this creation.

sub normalcopy{
  local($sf, $source) = @_;
  open (IN, "$source") || die "Where is $source?";
  while ($_=<IN>) {# read and digest template values
    last if /font_size/; # discard 1st few lines
  }
  print OUT "SF_=$sf;  % scale factor\n";
  while ($_=<IN>) {# read rest of .mf file
    if ( /^([a-zA-Z_#]+):=([0-9\.\/a-z#]+);/ ) {
	 $curparam=$1; $oldvalue=$2; # save these!
	 if (defined $param{$curparam}) {
	   $newvalue=$param{$curparam};
	   $newvalue="SF_*$newvalue" if /pt#/;
	   s/$oldvalue/$newvalue/;
	 }
    }
    if ($slantind==$noslant) {
	 if (/^slant:=([\.0-9]+);/) {# zero slant in roman files
	   s/$1/0/;
	 }
    }
    if (/generate/) { # have we reached the `generate' statement?
	s/generate\s+/generate mk/;
    }
    print OUT $_;
  }
}

## In case the source file (eg cmsy12.mf or cmmib7.mf) does NOT exist,
## some handwaving is necessary.  We'll read from the corresponding roman
## file (eg cmr12 or cmbx7) which HAD better exist up through the
## `generate' command, but then finish off with the corresponding 
## roman file.  Thus, to create tmbsy7.mf, we will read cmbx7 up through
## the `generate' command towards the end of that file, and then pick
## up cmsy7.mf, using only the material following the `math_fitting'
## parameter (this is the last parameter in the standard .mf files).
## Final concern: the sy fonts need the parameter `rth#' defined as
## either .4pt# or .6pt# (roman or bold), so we have to take 
## care to create this variable if need be.

sub fudgecopy{
  local($sf, $source) = @_;
  ## redefine the source...
  if ($doingbold == $true) {
    $source="$mfinputs${sep}cmbx$size.mf";
  } else {
    $source="$mfinputs${sep}cmr$size.mf";
  }
  $nextsize=$size; 
  $nextsize=10 if $size>10; # unfortunate default
  $nextmid=$midfix;
  $nextmid =~ s/b//;
  $nextsource="$mfinputs${sep}cm$nextmid$nextsize.mf";
  open (IN, "$source") || die "Where is $source?";
  while ($_=<IN>) {# read and digest template values
    last if /font_size/; # discard 1st few lines
  }
  print OUT "SF_=$sf;  % scale factor\n";
  while ($_=<IN>) {# read rest of .mf file until `generate'
    if ( /^([a-zA-Z_#]+):=([0-9\.\/a-z#]+);/ ) {
	 last if /^generate/;
	 $curparam=$1; $oldvalue=$2; # save these!
	 if (defined $param{$curparam}) {
	   $newvalue=$param{$curparam};
	   $newvalue="SF_*$newvalue" if /pt#/;
	   s/$oldvalue/$newvalue/;
	 }
    }
    if ($slantind==$noslant) {
	 if (/^slant:=([\.0-9]+);/) {# zero slant in roman files
	   s/$1/0/;
	 }
    }
    print OUT $_ unless /^generate/;
  }
  close IN;
  open (IN, $nextsource) ||
    die "Where is $nextsource?";
  while (<IN>) {last if /^math_fitting/;} # discard front of file
  $myrth=".4pt#";
  if ($doingbold==$true) {$myrth=".6pt#";}
  print OUT "\nrth#:=$myrth; % assume rules come from extension font\n";
  while (<IN>) {
    next if /^rth#/; # we generated this line already
    s/rule_thickness#/rth#/g; # ensure references are to rth#
    s/generate\s+/generate mk/;
    print OUT $_;
  }
}

## The actual mf files are created by the following routine.

sub mf_file_header{
    local($target, $midfix, $size) = @_;
    print OUT "%% $tseries-like font, file $target $size point\n";
    print OUT "%% Created by MathKit$version at $currtime on $currdate.\n";
    print OUT "if unknown cmbase: input cmbase fi\n\n";
    $fontid="$tseries$midfix";
    $fontid =~ tr/a-z/A-Z/; # make everything uppercase
    print OUT "font_identifier:=\"$fontid\"; font_size ${size}pt#;\n\n";
}

## &makemffiles(src prefix, target midfix, slant ind, list of sizes);
sub makemffiles{# create and process Roman fonts
  local($prefix,$midfix,$slantind)=($_[0],$_[1],$_[2]);
  local(*sizes)=$_[3];
  foreach $size (@sizes) {
    $source="$mfinputs${sep}${prefix}${midfix}${size}.mf";
    $targetfirst="${tseries}${midfix}${size}";
    $target="$targetfirst.mf";
    $tfmsource="$cm_tfm$sep${prefix}${midfix}${size}.tfm";
    ## All gf and pk files will have been created, and we need pl files.
    print PL "echo $targetfirst\n"; # system dependent
    print PL "tftopl $targetfirst.tfm $targetfirst.pl\n";
    print MF "$mf ${q}&cm \\mode=$mode; scrollmode; input $targetfirst$q\n";
    print GFTOPK "gftopk $targetfirst.$gfext $targetfirst.$pkext\n";
    open (OUT, ">$target") || die "Cannot open $target.  Help!";
    $sf=$size/10;
    &mf_file_header($target, $midfix, $size);
    if (-e $source) {
	 &normalcopy($sf, $source); # source exists--o joy, o rapture!
    } else {
	 &fudgecopy($sf, $source); # no source---finagling follows
    }
    close IN; close OUT;
  }
}

sub do_roman{
  $doingbold=$false;
  &makemffiles("cm","r",$noslant,*romans);
}

sub do_bold{
  $doingbold=$true;
  &makemffiles("cm","bx",$noslant,*bolds);
}

sub do_mathital{
  $doingbold=$false;
  &makemffiles("cm","mi",$slanted,*romans);
}

sub do_mathitalb{
  $doingbold=$true;
  &makemffiles("cm","mib",$slanted,*bolds);
}

sub do_mathsy{
  $doingbold=$false;
  &makemffiles("cm","sy",$slanted,*romans);
}

sub do_mathbsy{
  $doingbold=$true;
  &makemffiles("cm","bsy",$slanted,*bolds);
}

sub do_tenex{
  $doingbold=$false;
  &makemffiles("cm","ex",$noslant,*ex);
}

sub do_tenbex{
  $doingbold=$true;
  &makemffiles("cm","bex",$noslant,*ex);
}

sub get_encoding_sequence{
    local($sequence) = "${enc}t";
    if ($exp) {			# expertised fonts?
	$sequence = "9e";	# Cork (8t) + experts
	$sequence = "9t" if $enc == 7; # 
    }
    $replace{"<enct>"} = $sequence;
    $sequence;
}
 
sub startpl{
  local($e) = &get_encoding_sequence($enc);
  print PL "tftopl $rm_tfm$sep${fontfam}r$e.tfm ${fontfam}r$e.pl\n";
  print PL "tftopl $rm_tfm$sep${fontfam}ri$e.tfm ${fontfam}ri$e.pl\n";
  if ($#template==1) {
	print PL "tftopl $rm_tfm$sep$fontfam$bld$e.tfm $fontfam$bld$e.pl\n";
	print PL 
	    "tftopl $rm_tfm$sep$fontfam${bld}i$e.tfm $fontfam${bld}i$e.pl\n";
  }
}

## This routine reads an fd file and discards comments, the `\endinput' 
## statement, and the `\ProvidesPackage' statement.  It adds font scaling
## info specified as an argument to the routine into all 
## `\DeclareFontShape' statements.  One catch: scaling info may have
## been placed in the fd file by fontinst, so we want to catch this and
## replace it by our current scaling factor.

sub get_fd_info_and_scale_it{
    local($fdfile, $mysize, $inputs_) = @_;
    local($pathandfdfile) = "$inputs_${sep}$fdfile.fd";
    open(FD, $pathandfdfile) or die "Can't find $pathandfdfile.  Help";
    while (<FD>) {		# skips comments and ProvidesPackage
	last if /\\DeclareFontFamily/;
    }
    &out("\n%% Extracting font info from file $fdfile.fd...\n");
    FDLINE: for (;;) {
	if ( $_ =~ /\[\d*\.\d+\]/ ) { # scaling factor found!
	    $_ =~ s/\[\d*\.\d+\]/\[$mysize\]/g;
	} else {		# no scaling factor found
	    # we search for right angle, followed by 0 or more space,
	    # followed by fontname (which begins with alpha)
	    s/(>\s*)([^<\s])/\1 \[$mysize\] \2/g;
	}
	&out("$_") unless /^%/;	# ignore comments
	last FDLINE if eof(FD);
	$_ = <FD>;
	last FDLINE if /\\endinput/;
    }
    &out("%% End of extraction from file $fdfile.fd.\n\n");
    close FD;
}

sub makestylefile{
  $source="z$_[0].mk";
  $pathsource="..$sep$source";
  $target="z$tseries$fontfam.$_[0]";
  close IN; close OUT; # just in case
  open (IN, $pathsource) 
    || die "We are missing file $pathsource.  HELP!";
  open (OUT, ">$target")
    || die "Cannot open file $target.  Help!";
  $FH_ = "OUT";
  print OUT "%% This is file $target, containing ";
  print OUT "TeX macros to typeset mock-MF fonts.\n";
  print OUT "%% Created by MathKit$version at $currtime on $currdate.\n";
  while ($_=<IN>) {
    next if /^%%/; # strip out comment lines
    while ( /(<[a-z]+>)/ ) {  # parameters for replacement?
	 $a_ = $1; $b_ = $replace{$a_};
	 $_ =~ s/$a_/$b_/g;
    }
    if ( /^\[([a-z]+)\]/ ) {# do we strip off special font indicators?
	 $ind = $1;
	 $_ =~ s/\[b\]// if defined $template[1];
	 $_ =~ s/\[$ind\]// if $$ind;
    }
    print OUT $_ unless /^\[/; # some special lines are not needed
  }
  if ($_[0] eq "tex") {		# hook for the plain .tex file
  }
  if ($_[0] eq "sty") {		# hook for the LaTeX (NFSS) .sty file
      if ($template[1]) {	# is there a bold series?
	  print OUT "\\DeclareMathAlphabet{\\mathbf}{$encoding}";
	  print OUT "{$fontfamx}{$nfss_bold}{n}\n";
      }
      if ($tt) {		# for tt
	  $tt_inputs = $inputs;
	  $tt_inputs = "$texmf${sep}tex${sep}latex${sep}$tt_installer"
	      if $texmf;
	  &get_fd_info_and_scale_it("${encoding}cmtt", $tt_sf, $tt_inputs);
	  print OUT "\\DeclareMathAlphabet{\\mathtt}{$encoding}";
	  print OUT "{\\ttdefault}{m}{n}\n";
      }
      if ($sa) {		# for sans serif
	  $sans_inputs = $inputs;
	  $sans_inputs = "$texmf${sep}tex${sep}latex${sep}$sans_installer"
	      if $texmf;
	  &get_fd_info_and_scale_it("${encoding}${sansfam}", $sans_sf, 
				    $sans_inputs);
	  print OUT "\\newcommand{\\sansdefault}{$sansfam}\n";
	  print OUT "\\DeclareMathAlphabet{\\mathsf}{$encoding}";
	  print OUT "{\\sansdefault}{m}{n}\n";
      }
  }
  print OUT "\\endinput\n"; 
  close IN; close OUT;
}

sub create_hax_files{
    $hackprefix = "$tseries$fontfam";
    &safecopy("..${sep}rhax.mtx", "${hackprefix}rrx.mtx");
    &safecopy("..${sep}ihax.mtx", "${hackprefix}rix.mtx");
    &safecopy("..${sep}iskew.mtx", "${hackprefix}riw.mtx");
    $replace{"<hax>"} = $hackprefix;
    if (defined $template[1]) {	# A bold?  If so, we need bold versions...
        &safecopy("..${sep}rhax.mtx", "${hackprefix}brx.mtx");
        &safecopy("..${sep}ihax.mtx", "${hackprefix}bix.mtx");
        &safecopy("..${sep}iskew.mtx", "${hackprefix}biw.mtx");
    }
}

sub makemakevp{ # makes the file makevp.tex, the fontinst install file.
  &create_hax_files;		# files for fixing metric info
  $source="makevp.mk";
  $pathsource="..$sep$source";
  $target="makevp.tex";
  close IN; close OUT; # just in case
  open (IN, $pathsource) 
    || die "We are missing file $pathsource.  HELP!";
  open (OUT, ">$target")
    || die "Cannot open file $target.  Help!";
  print OUT "%&plain\n";
  print OUT "%% This is file $target, containing ";
  print OUT "fontinst install commands to create virtual fonts.\n";
  print OUT "%% Created by MathKit$version at $currtime on $currdate.\n";
  while ($_=<IN>) {
    next if /^%%/; # strip off comment lines
    while ( /(<[a-z]+>)/ ) {# parameters for replacement?
	 $a_ = $1;
	 $b_ = $replace{$a_};
	 $_ =~ s/$a_/$b_/;
    }
    if ( /^\[b\]/ ) {# do we strip off bold indicator?
	 $_ =~ s/\[b\]// if defined $template[1];
    }
    print OUT $_ unless /^\[b\]/; # bold lines are not needed
  } 
  close IN; close OUT;
  open (IN, "makevp.tex");
  open (OUT, ">makevf.bat");
  &banner("OUT", "makevf.bat");
  while ($_ = <IN>) {
      next unless $_ =~ /\\installfont\{/;
      $_ =~ /\s+\\installfont\{([a-z0-9]+)\}\{/;
      print OUT "vptovf $1.vpl $1.vf $1.tfm\n";
  }
  close IN; close OUT;
}

## This routine prints $_ to output filehandle TEX provided there
## are no bracketed qualifiers at the line beginning.  It tests 
## for 2-char qualifiers; "b" is handled separately.

sub printTEXlines {m
    if ($_ =~ /^\[([a-z]+)\]/) {
	local($ind_)=$1;
	$_=~s/^\[$ind_\]// if $$ind_;
    }
    print TEX $_ unless /^\[/;m
}

sub makeLaTeXtest{
    open(TEX, ">testmath.tex");
    &printbanner("TEX", "%", "testmath.tex");
    local($infile) = "..${sep}testmath.mk";
    open(TEMPLATE, $infile) or
	die "Where is the template $infile?";
    while ($_ = <TEMPLATE>) {
	next if /^%/;		# strip comments away
        while ( /(<[a-z]+>)/ ) {# parameters for replacement?
             $a_ = $1;
             $b_ = $replace{$a_};
             $_ =~ s/$a_/$b_/;
        }
        if ( /^\[b\]/ ) { # do we strip off bold indicator?
	    $_ =~ s/\[b\]// if defined $template[1];
	}
	&printTEXlines;
    }
    close TEX; close TEMPLATE;
}
  
sub makeplaintest{
    open(TEX, ">testmatp.tex");
    &printbanner("TEX", "%", "testmatp.tex");
    local($infile) = "..${sep}testmatp.mk";
    open(TEMPLATE, $infile) or
	die "Where is the template $infile?";
    while ($_ = <TEMPLATE>) {
	next if /^%/;		# strip comments away
        while ( /(<[a-z]+>)/ ) {# parameters for replacement?
             $a_ = $1;
             $b_ = $replace{$a_};
             $_ =~ s/$a_/$b_/;
        }
        if ( /^\[b\]/ ) { # do we strip off bold indicator?
	    $_ =~ s/\[b\]// if defined $template[1];
	}
	&printTEXlines;
    }
    close TEX; close TEMPLATE;
}
  
    $finish_up =<<"EndFinish";

Now to finish up.  Perform the following tasks:
0.  Execute 'mkdirs.bat' to create any missing directories.
1.  Execute the file 'makegf.bat'.  (This may take some time...)
2.  Create the packed pixel .pk files.  On some (Unix) systems, you do this  
    via commands like:
localhost>>foreach X (*.300gf)
localhost>>gftopk \$X \$X:r.300pk
localhost>>end
    but many systems do not permit this.  Therefore, you will need to examine
    the accompanying file 'makepk.bat' and EDIT it accordingly to conform
    to your system standards and syntax.  Thereafter, execute this file;
    make sure you EDIT it first!
3.  Execute the file 'makepl.bat'.
4.  Execute the command 'tex makevp'.  (This may take some time...)
5.  Execute the file 'makevf.bat'.
6.  Run the files 'testmath.tex' (LaTeX) or 'testmatp.tex' (plain TeX) 
    through TeX to test the files.  [Consult the documentation 'mathkit.tex'
    for advice on adjusting these new math fonts.]
7.  When you are satisfied with the results, execute 'putfonts.bat' to store
    the fonts and files where they belong.
(This advice appears in the Ascii file 'todo.nxt' and in the log file
'z$tseries$fontfam.klg' which contains other information about this run.)
EndFinish

## Now let's record the remaining tasks...
sub finish{
   ## first, make bat files executable...
    @batch_files = 
      ("putfonts.bat", "mkdirs.bat", "makegf.bat", 
       "makepk.bat", "makepl.bat", "makevf.bat");
    chmod 0755, @batch_files;
    ## Putting files where they belong...
    print PUT "$mv *.mf $mfinputs\n";
    print PUT "$mv *.vf $vf\n";
    print PUT "$mv *.tfm $tfm\n";
    ## $pkext = "pk";		# DOS default
    ## if ($sep ne "\\") {
    ## 	   print "I need the extension for the pixel files you created.\n";
    ## 	   print "This extension is something like '300pk' or '600pk'.\n";
    ## 	   print "Please enter it now: ";
    ## 	   $pkext = <STDIN>; chomp $pkext;
    ## }
    ## $pkext =~ s/^\.//;		# just in case user added a `dot'
    &record("I am using the extension `.$pkext' for pixel files.\n");
    print PUT "$mv *.$pkext $pk\n";
    print PUT "$mv *.fd $inputs\n";
    print PUT "$mv z$tseries$fontfam.sty $inputs\n";
    print PUT "$mv z$tseries$fontfam.tex $plain_inputs\n";
    ## User instructions...
    &record($finish_up);
    open (TODO, ">todo.nxt");
    &banner("TODO", "todo.nxt");
    print TODO $finish_up;
    close TODO;
}

## For each point size <n> we create roman, mi, sy

@romans=(12,10,9,7,5); # point sizes for Roman fonts
@bolds=(12,10,7,5);  # point sizes for bold fonts
@ex=(10);                  # point sizes for extension fonts

## The following replacements will be used to personalize the templates.

$replace{"<currdate>"} = $currdate;
$replace{"<fam>"} = $fontfam;
$replace{"<famx>"} = $fontfam;	# expert family?
$replace{"<famx>"} = "${fontfam}x" 
    if -e "$rm_inputs$sep$encoding${fontfam}x.fd";
$fontfamx = $replace{"<famx>"};
$replace{"<mock>"} = $tseries;
$replace{"<m>"} = $tser;
$replace{"<enc>"} = $enc;
$replace{"<enx>"} = $replace{"<enc>"};
$replace{"<enx>"} = "9"
    if -e "$rm_inputs$sep$encoding${fontfam}x.fd";
$replace{"<encoding>"} = $encoding;
$replace{"<b>"} = $bld;
$replace{"<nfssb>"} = $nfssbld;
$replace{"<bseries>"} = "b"; ## change to "bx" if appropriate
## but...
$replace{"<bseries>"} = "sb" if $bld eq "s";
$nfss_bold = $replace{"<bseries>"};

## Some defaults for the special fonts...

$replace{"<ssr>"} = "cmss10";		# sans serif stuff
$replace{"<ssb>"} = "cmssbx10";
$replace{"<ssri>"} = "cmssi10";
$replace{"<ssbi>"} = "cmssbx10";
$replace{"<sasize>"} = "10";
$replace{"<ttsize>"} = "10";		# tt
$replace{"<cal>"} = "${tseries}sy10";		# calligraphic
$replace{"<calb>"} = "${tseries}bsy10";		# calligraphic
$replace{"<casize>"} = "10";
$replace{"<frsize>"} = "10";		# fraktur
$replace{"<frsf>"} = "1";

sub do_it_all{
  open (PL, ">makepl.bat"); # script to create pl files fontinst needs
  &banner("PL", "makepl.bat");
  open (LTX, ">z${tseries}${fontfam}.sty"); # contains NFSS (LaTeX) macros
  open (PLN, ">z${tseries}${fontfam}.tex"); # contains plain TeX macros
  open (PUT, ">putfonts.bat");	# places completed fonts where they belong
  &banner("PUT", "putfonts.bat");
  open (MF, ">makegf.bat"); # script to execute Metafont
  &banner("MF", "makegf.bat");
  open (GFTOPK, ">makepk.bat"); # script to pack all pixels
  &banner("GFTOPK", "makepk.bat");
  &get_fontfam_parameters("r");
  &get_template_info($template[0]);
  &adjust_font_parameters if $I_should_try_to_match;
  &startpl;
  ## Do all roman fonts...
  &do_roman;
 &do_mathital;
 &do_mathsy;
 &do_tenex;
 &do_special_fonts;		# tt, frak, b'board bold, cal, sans serif
 if (defined $template[1]) { # do bold fonts...
   &get_template_info($template[1]);
   &do_bold;
   &do_mathitalb;
   &do_mathbsy;
   &do_tenbex;
 }
  &makestylefile("tex"); # make plain TeX macro file
  &makestylefile("sty"); # make LaTeX NFSS package file
  &makemakevp;           # make the fontinst install file
  close PL; close LTX; close PLN; close MF; close GFTOPK;
  &makeLaTeXtest;
  &makeplaintest;
  &finish;
}

## Let 'er rip...

&do_it_all;

