%
% NUWEB $VER: CallMF.w 2.0 (22.3.95)
%
% Included in this NUWEB program are the ARexx scripts `MakeBatch.rexx'
% and `MakeTeXFont.rexx'.  To extract these ARexx scripts, use the command
%
%    > ami_nuweb -t callmf.w
%
% To create the LaTeX documentation of this file, use the commands
%
%    > ami_nuweb -c callmf.w
%    > latex callmf.tex
%    > ami_nuweb -c callmf.w
%    > latex callmf.tex
%    > latex callmf.tex
%
% You can find the Amiga port of the NUWEB `literate programming'
% language in the AmiNet at directory
%
%    AmiNet:/dev/misc/ami_nuweb0.90.lha
%
\documentclass{article}
\usepackage{latexsym}
\usepackage{mflogo}

\language=\USenglish

\title{CallMF 2.0 \\
  Automatic Font Generation with \MF}

\author{Andreas Scherer \\
  Roland-Stra{\ss}e 16 \\
  52070 Aachen, Germany \\
  \texttt{\small scherer@@genesis.informatik.rwth-aachen.de}}

\newcommand{\TUG}{\TeX\ Users Group (TUG)\renewcommand{\TUG}{TUG}}
\newcommand{\TDS}{\TeX\ Directory Structure (TDS)\renewcommand{\TDS}{TDS}}
\newcommand{\TWG}{\TUG\ Working Group (TWG)\renewcommand{\TWG}{TWG}}

\begin{document}

\maketitle

\begin{abstract}
\noindent
This is version 2.0 of the CallMF package, the ARexx scripts for
automatic font generation with `Pas\TeX' and `\texttt{DVIPS}'.
It works with the new `\TeX\ Directory Structure' and is
incompatible with version~1.0.
\end{abstract}

\vfill
{\small\noindent\baselineskip=11pt
\copyright\ 1995 by Andreas Scherer.

\noindent
Permission is granted to make and distribute verbatim copies of the
electronic form of this documentation provided that the electronic
copyright notice and this permission notice are preserved on all copies.

\noindent
Permission is granted to copy and distribute modified versions of the
electronic form of this documentation under the conditions for verbatim
copying, provided that the entire resulting derived work is distributed
under the terms of a permission notice identical to this one.\par}

\newpage
\tableofcontents

\newpage
\section{Introduction}

This is the all-new version of the `CallMF' package for automatic font
generation with the Pas\TeX\ drivers \texttt{ShowDVI} and \texttt{DVIprint}
as well as \texttt{DVIPS}.

\section{History}

CallMF~2.0 is directly descendend of CallMF~1.0, provided with either
Pas\TeX\ or \MF~2.71.  There have been many hands working on CallMF since
1991.  CallMF~1.0 was written and developed by Martin Bok\"amper, Georg
He{\ss}mann, J\"org H\"ohle, and Ulrich Wisser.  For my own distribution
of \MF~2.71 in 1993 and 1994, I made several minor changes to the scripts.

Although the names \texttt{MakeBatch.rexx} and \texttt{MakeTeXFont.rexx}
are the same in both versions of CallMF, they should absolutely \emph{not}
be mixed in any way.  CallMF~2 works completely different than version~1.

Ulrich Wisser's \texttt{MakeTeXFontSet.rexx} script is not included in
version~2 of CallMF.  As the author warns about its use, I don't support~it.
Maybe somebody will come up with a better idea for creating sets of related
fonts automatically.

\section{\TeX\ Directory Structure}

CallMF~2 is fully depending on the new \TDS\ developed by \TUG\ in
1995, which is described in the file

\begin{verbatim}
   ftp.th-darmstadt.de:/pub/tex/TDS-compliant/draft/tdsguide.dvi
\end{verbatim}

The main idea is the following directory scheme, which should be
present on all systems soon (and Amiga will be there again first!):

\begin{verbatim}
   </texmf>/fonts/<type>/<supplier>/<typeface>/<file>
\end{verbatim}

\begin{itemize}
\item{\texttt{</texmf>}} is a system-dependent part of the global directory
structure.  For the Amiga this should be `\texttt{TeXMF:}'.
\item{\texttt{<type>}} is one of `\texttt{gf}', `\texttt{log}',
`\texttt{pk}', `\texttt{source}' or `\texttt{tfm}'.
\item{\texttt{<supplier>}} is for example `\texttt{ams}' or
`\texttt{public}' for freely available fonts.
\item{\texttt{<typeface>}} is for example `\texttt{cm}', `\texttt{euler}'
or `\texttt{latex}'.
\item{\texttt{<file>}} is any name of a file residing in this scheme.
\end{itemize}

If the \texttt{<type>} is one of `\texttt{gf}', `\texttt{pk}' or
`\texttt{log}', two extra levels of subdirectories are necessary
to uniquely distinguish the files:

\begin{verbatim}
   </texmf>/fonts/pk/<supplier>/<typeface>/<mode>/dpi<dpi>/<file>
\end{verbatim}

\begin{itemize}
\item{\texttt{<mode>}} may be any entry from the canonical modes
collection `\texttt{modes.mf}' (current version~2.2).
\item{\texttt{dpi<dpi>}} is an ISO-compliant form of the resolution
dependent subdirectory structure proposed by the \TWG\ for a new~\TDS.
\end{itemize}

Some examples for the new scheme (in top-down alphabetic order):

\begin{verbatim}
   TeXMF:fonts/gf/ams/euler/amiga/dpi100/eufm10.100gf
   TeXMF:fonts/gf/public/cm/amiga/dpi100/cmr10.100gf
   TeXMF:fonts/log/ams/euler/amiga/dpi100/eufm10.100log
   TeXMF:fonts/log/public/cm/amiga/dpi100/cmr10.100log
   TeXMF:fonts/pk/ams/euler/amiga/dpi100/eufm10.100pk
   TeXMF:fonts/pk/public/cm/amiga/dpi100/cmr10.100pk
   TeXMF:fonts/source/ams/euler/eufm10.mf
   TeXMF:fonts/source/public/cm/cmr10.mf
   TeXMF:fonts/tfm/ams/euler/eufm10.tfm
   TeXMF:fonts/tfm/public/cm/cmr10.tfm
\end{verbatim}

\section{Installation}

\subsection{System requirements}

CallMF is a set of ARexx scripts, so you need William Hawes' ARexx
system installed.  It is included in version~2 of the AmigaOS, but
even for OS~1.3 you can purchase ARexx separately.

\subsection{Automatic installation}

The ARexx scripts and the configuration file \texttt{FontVols} for font
caching are automatically installed by the file `\texttt{callmf.ins}'
coming with this distribution.  All you have to do is `\texttt{execute}'
the shell~script.  Do \emph{not} install CallMF~2.0 simply as an update
in your existing system, if you do not intend to switch to~\TDS.

\subsection{Setup for use with Pas\TeX}

To make use of CallMF in connection with Georg He{\ss}mann's Pas\TeX\
drivers \texttt{ShowDVI} and \texttt{DVIprint} and/or the Amiga port of
Tomas Rokicki's \texttt{DVIPS}~5.58 by Giuseppe Ghib\`o, set the
environment variable \texttt{CALLMF} to~one~of

\begin{verbatim}
   TeXMF:web2c61/rexx/MakeBatch.rexx
   TeXMF:web2c61/rexx/MakeTeXFont.rexx
\end{verbatim}

The first entry is the preferred choice, it collects all references to
missing fonts in a batch file, calling \texttt{MakeTeXFont.rexx} later.
The second generates missing fonts `on the fly'.  Due to the excessive
time consumption of \MF\ it should only be used on really fast machines.

\section{Description of the files}

\subsection{\texttt{MakeBatch.rexx}}

This is the revised version of the `CallMF' batch script.  To make use of
it in connection with the Pas\TeX\ drivers and/or \texttt{DVIPS}, set the
environment variable \texttt{CALLMF} to

\begin{verbatim}
   TeXMF:web2c61/rexx/MakeBatch.rexx
\end{verbatim}

These three dvi driver programs evaluate the environment variable
\texttt{CALLMF} and call the ARexx command defined there, whenever
a font file is missing.

\texttt{MakeBatch.rexx} has the following arguments provided by the dvi
drivers \texttt{ShowDVI}, \texttt{DVIprint}, and \texttt{DVIPS} directly:

\begin{itemize}
\item{\texttt{FONTNAME}} is the name of the font driver,
e.g., `\texttt{cmr10}'.
\item{\texttt{DPI}} is the resolution of the final output,
e.g., `\texttt{120}'.
\item{\texttt{HBASE}} is the horizontal base resolution,
e.g., `\texttt{100}'.
\item{\texttt{VBASE}} is the vertical base resolution,
e.g., `\texttt{100}'.
\item{\texttt{DRIVER}} is either `\texttt{show}',
`\texttt{print}', or `\texttt{dvips}', depending on the
dvi driver in use.
\item{\texttt{PKFILENAME}} is the resolution dependent file name prepended
with the `\texttt{<dpi>}' part of~\TDS, e.g., `\texttt{dpi120/cmr10.120pk}'.
\item{\texttt{PKDIR}} is the complete driver dependent directory path as
specified in the font configuration files, e.g.,
`\texttt{TeXMF:fonts/source/public/cm/amiga/}'.  (\texttt{ShowDVI} and
\texttt{DVIprint} use only the first entry in the font configuration file,
which should be set up to the `\texttt{<mode>}' only.)
\end{itemize}

Here is the overall layout of the \texttt{MakeBatch.rexx} script.
For ARexx it is essential to find a C~like comment in the first
line of~a~script.
@o MakeBatch.rexx
@{/* This ARexx program was automatically created by NUWEB.
   Do not edit by hand! */
@<Files and scripts for \texttt{MakeBatch.rexx}@>
Address COMMAND
@<Set up the batch file@>
@<Get the calling parameters@>
@<Fix any \texttt{show} entries to a single \texttt{<mode>}@>
@<Insert font caching for \texttt{DVIPS}@>
@<Create a new line for the batch file@>
@<Enter the new line in the batch file, but not twice@>
Exit 0
@}

\subsection{\texttt{MakeTeXFont.rexx}}

This is the kernel script of CallMF~2.0.  Here the necessary steps for
generation of \TeX\ fonts with \MF\ are processed automatically.  Also
format conversion and installation are done.

It has the following arguments provided by \texttt{ShowDVI},
\texttt{DVIprint}, and \texttt{DVIPS} directly, just the same
as for \texttt{MakeBatch.rexx}:

\begin{itemize}
\item{\texttt{FONTNAME}} is the name of the font driver loaded by~\MF,
e.g., `\texttt{cmr10}'.
\item{\texttt{DPI}} is the resolution of the final output,
e.g., `\texttt{120}'.  This together with the next parameter
determines the `\texttt{mag}' value.
\item{\texttt{HBASE}} is horizontal base resolution of the
\texttt{<driver>} used, e.g., `\texttt{100}' for `\texttt{amiga}'.
\item{\texttt{VBASE}} is vertical base resolution,
e.g., `\texttt{100}'.  This value is not used in the script.
\item{\texttt{DRIVER}} is either `\texttt{show}', `\texttt{print}'
or `\texttt{dvips}' and is ignored in this script.  (It has a meaning
in \texttt{MakeBatch.rexx}, though.)
\item{\texttt{PKFILEARG}} is the resolution dependent file name including
the lowest level of the directory path , e.g., `\texttt{dpi120/cmr10.120pk}'.
Although this is in fact part of the \TDS, this value is currently not used.
\item{\texttt{PKDIRARG}} is the driver dependent directory path in \TDS\
notation, e.g., `\texttt{TeXMF:fonts/source/public/cm/amiga/}'.  This value
is only used to determine the \texttt{<mode>}, so it could virtually
contain anything as long there is a valid \texttt{<mode>} name embraced to
the left and the right by single~slashes.
\end{itemize}

Here is the overall layout of \texttt{MakeTeXFont.rexx}.  For ARexx it is
essential to find a C~like comment in the first line of~a~script.
@o MakeTeXFont.rexx
@{/* This ARexx program was automatically created by NUWEB.
   Do not edit by hand! */
@<User definable stuff in \texttt{MakeTeXFont.rexx}@>
@<The main body of \texttt{MakeTeXFont.rexx}@>
@<Local procedures for \texttt{MakeTeXFont.rexx}@>
@}

\section{Implementation of the scripts}

\subsection{\texttt{MakeBatch.rexx}}

\subsubsection{User definable part}

This is the complete set of files necessary for CallMF~2.0 to work.
You may change these entries to accommodate your personal system setup.
@d Files and scripts for \texttt{MakeBatch.rexx}
@{BATCHNAME = "TeXMF:fonts/MakeTeXFont.sh"
FONTFILENAME = "TeXMF:pastex/config/fontvols"
MAKETEXFONT = "TeXMF:web2c61/rexx/MakeTeXFont"
@}

\subsubsection{Fixed part}

First, \texttt{MakeBatch.rexx} makes your it is able to write (or rather
append) entries to the batch file.
@d Set up the batch file
@{If ~Exists(BATCHNAME) Then Do
  If ~Open(BATCHFILE,BATCHNAME,'W') Then Do
    Say "MakeBatch: Could not create "BATCHNAME" !"
    Exit 10
  End; Else Do
    Call Writeln(BATCHFILE,";# Batchfile for font generation")
    If Close(BATCHFILE) Then Do
      'Protect 'BATCHNAME' Add s'
      Say "MakeBatch: Batchfile "BATCHNAME" created."
    End; Else Do
      Say "MakeBatch: Error during creation of "BATCHNAME" !"
      Exit 10
    End
  End
End
@}

Here is the complete list of parameters to the \texttt{MakeBatch.rexx}
script as described earlier.
@d Get the calling parameters
@{Parse Arg PARAMETERS
Parse Value PARAMETERS With FONTNAME DUMMY
Parse Value DUMMY With DPI HBASE VBASE DUMMY
Parse Value DUMMY With DRIVER DUMMY
Parse Value DUMMY With PKFILENAME PKDIR .
@}

Only \texttt{ShowDVI} and \texttt{DVIprint} of version ${}\geq1.08$
supply arguments \texttt{<drivertype>} (\texttt{show}/\texttt{print})
and \texttt{<pkfilename>} (for example `\texttt{dpi120/cmr10.120pk}',
depending on the font configuration files \texttt{ShowDVI.fnt} and
\texttt{DVIprint.fnt}).

Only \texttt{ShowDVI} and \texttt{DVIPrint} of version ${}\geq1.10$
supply the argument \texttt{<pkdir>} (for example
`\texttt{TeXMF:fonts/source/public/cm/amiga/}').  For the purposes of
CallMF~2, only the \texttt{<mode>} part of \texttt{<pkdir>} would suffice,
e.g., `\texttt{/amiga/}'.

\texttt{DVIPS}~5.47 supplies \texttt{<drivertype>}, \texttt{<pkfilename>},
and \texttt{<pkdir>}, but the latter is \emph{not} correct!  It is always
set to `\texttt{TeX:pk/}' instead of the directory given in the file
\texttt{config.ps}.

\texttt{DVIPS}~5.58 supplies \texttt{<drivertype>}, \texttt{<pkfilename>},
and \texttt{<pkdir>} correctly as given in \texttt{config.ps}.  Surely
there will also be a version fully supporting~\TDS.

To save a considerable amount of disk space, all ``\texttt{show}'' entries
are redirected to the standard ``\texttt{amiga}'' mode.  For example, the
Pas\TeX\ mode ``\texttt{OneTwoZero}'' will use the 120~dpi fonts generated
for ``\texttt{mag=1.2}'' of mode ``\texttt{amiga}''.

@d Fix any \texttt{show} entries to a single \texttt{<mode>}
@{If DRIVER="show" Then Do
  HBASE = "100"
  VBASE = "100"
  PKDIR = "/amiga/"
  PARAMETERS = FONTNAME DPI HBASE VBASE DRIVER PKFILENAME PKDIR
End
@}

The following code is the unmodified version written by Ulrich Wisser for
CallMF~1.0.  I don't use \texttt{DVIPS} and have never tested this.

@d Insert font caching for \texttt{DVIPS}
@{If DRIVER="dvips" Then Do
  If Open('FONTFILE', FONTFILENAME, 'READ') Then Do
    Do Until EOF('FONTFILE')
      LINE = ReadLn('FONTFILE')
      If Pos(';', LINE) > 0 Then
        LINE = Left(LINE, Pos(';', LINE)-1)
      Parse Var LINE DISK'='ddd'd.'LINE
      If DDD=HBASE Then Do
        Do Until LINE = ''
          Parse Var LINE DIR'.'LINE
          If DIR=DPI Then Do
            If Exists(DISK':'PKFILENAME) Then Do
              FULLNAME=PKDIR||PKFILENAME
              DIRNAME=Left(FULLNAME, LastPos('/', FULLNAME)-1)
              If ~Exists(DIRNAME) Then Do
                Say 'makedir' DIRNAME
                MakeDir DIRNAME
              End
              Say 'copy 'DISK':'PKFILENAME' 'FULLNAME
              Copy DISK':'PKFILENAME' 'FULLNAME' QUIET NOREQ'
              If ~Close('FONTFILE') Then
                Say "Can't close file "FONTFILENAME"!"
              Exit
            End /* If Exists */
          End /* If DIR=DPI */
        End /* Do Until LINE */
      End /* If DDD */
    End /* Do Until EOF */
    If ~Close('FONTFILE') Then
      Say "Can't close file "FONTFILENAME"!"
  End /* If Open */
  Else Say "Couldn't find file "FONTFILENAME
End /* If DRIVER */
@}

Uncomment the first lines to see what \texttt{MakeBatch.rexx} intends
to~do.  Else it is very simple, just hand over all \texttt{PARAMETERS}
to the kernel script that will do the actual font generation.  I~thought
about modifying some of the unused entries, but it doesn't seem worthwhile
(and it is a good test for \texttt{MakeTeXFont.rexx}).
@d Create a new line for the batch file
@{/*
 * Say "MakeBatch: Font named = "FONTNAME" with dpi = "DPI
 * Say "and base x = "HBASE", y = "VBASE
 * If "" ~= DRIVER Then
 *   Say " for driver = "DRIVER", pkname = "PKFILENAME
 */
MESSAGE = "rx" MAKETEXFONT PARAMETERS
@}

Would it be better to `\texttt{Search}' for \texttt{FONTNAME}, \texttt{DPI},
\texttt{HBASE}, and \texttt{VBASE} only?
@d Enter the new line in the batch file, but not twice
@{'Search >NIL: 'BATCHNAME' "'MESSAGE'"'

If RC ~=0 Then
  @<The line does not yet exist, append it to the batch file@>
Else 
  Say "MakeBatch: Entry already exists!"
@}

@d The line does not yet exist, append it to the batch file
@{Do
  @<Open the batch file for appending@>
  DUMMY = Writeln(BATCHFILE,MESSAGE)
  @<Close the batch file@>
End
@}

@d Open the batch file for appending
@{If ~Open(BATCHFILE,BATCHNAME,'Append') Then Do
  Say "MakeBatch: Can't open "BATCHNAME" to add new entry"
  Exit 10
End
@}

@d Close the batch file
@{If Close(BATCHFILE) Then
  Say "MakeBatch: New entry added for "FONTNAME DPI" dpi"
Else Do
  Say "MakeBatch: Error while adding new entry!"
  Exit 10
End
@}

\subsection{\texttt{MakeTeXFont.rexx}}

\subsubsection{User definable part}

Although considerable effort has been expended to make CallMF~2.0 fully
compliant to the new \TDS\ in the latest version I could find, there are
still some system dependent parts for the individual user to change.
\label{UserStuff}
@d User definable stuff in \texttt{MakeTeXFont.rexx}
@{@<Files and directories@>
@<The programs used@>
@<The default base@>
@<Final decision@>
@}

As soon as your TeX system is fully compliant to the \TDS\ developed by
\TUG, all you should have to change is this very first entry and nothing
else (if at all).  Here `\texttt{TeXMF:}' stands for the freely available
`\texttt{</texmf>}' part of~\TDS.  I~choose the Amiga equivalent in form
of an `\texttt{assign}ed' device.  The names of the subdirectories are
fairly standardized (except for the CallMF specific `\texttt{log}').
CallMF only has to do with bitmapped fonts, so there is no need to add
entries for `\texttt{/vf}' or~the~like.
@d Files and directories
@{FONTSDIR = "TeXMF:fonts"

SOURCEDIR = FONTSDIR || "/source"
GFDIR     = FONTSDIR || "/gf"
PKDIR     = FONTSDIR || "/pk"
TFMDIR    = FONTSDIR || "/tfm"
LOGDIR    = FONTSDIR || "/log"

MASTERLOG = LOGDIR || "/MakeTeXFont.log" /* Main logfile */
@| FONTSDIR GFDIR LOGDIR PKDIR SOURCEDIR TFMDIR MakeTeXFont.log @}

This name might also need adaption.  It's temporary right now, until
I know for certain where `\texttt{/rexx}' is to be located in~\TDS.
@d Files and directories
@{REXXDIR = "TeXMF:web2c61/rexx"
@| REXXDIR @}

\MF\ and companion.  \texttt{PATH} should be set appropriately.
\texttt{VirMF} can be `resident' anyway, so we do not use absolute
paths here.
@d The programs used
@{VIRMF  = "VirMF"
GFTOPK = "GFtoPK"
@| VIRMF GFTOPK @}

There are not many other bases around, so you'd better
leave this entry `as~is'.
@d The default base
@{BASE = "plain"
@| BASE plain.mf @}

Should gf files be stored for later reference?  Set this to `\texttt{1}'
if you don't want the gf files to be deleted after they are converted to
pk files.  (You can always recreate the gf file to a given pk file with
\texttt{PKtoGF} later.)
@d Final decision
@{KEEPGFFILES = 0
@| KEEPGFFILES @}

\subsubsection{Fixed part}

There is no user definable stuff below this point.  Really.  We do some
timing on the fly.  This covers almost the complete run including but not
limited to the conversion steps and the actual file installation.
@d The main body of \texttt{MakeTeXFont.rexx}
@{@<Special introduction@>
DUMMY = Time("Reset")
@<Do the actual work@>
ELAPSED = Time("ELAPSED")
LOGSTRING = "created in "ELAPSED%3600
LOGSTRING = LOGSTRING || ":"Right(ELAPSED%60//60,2,'0')
LOGSTRING = LOGSTRING || ":"Right(ELAPSED%1//60,2,'0')
Call Log(LOGSTRING)
Exit 0 /* This is the end */
@| DUMMY ELAPSED LOGSTRING @}

Here we set up the script for a good start.  User breaks will be handled in
a special subroutine.  Several commands addressed to the operating system.
@d Special introduction
@{Signal On BREAK_C
Signal On BREAK_D
Address COMMAND
@| BREAK_C BREAK_D@}

@d Do the actual work
@{@<Get the arguments this script is called with@>
@<Create some internal defaults we need@>
@<Run {\MF} to generate the font@>
@<Run \texttt{GFtoPK} to convert the pixel format@>
@<Install the files at the appropriate places@>
@}

The arguments of the CallMF~2 scripts are exactly those of version~1 for
compatibility with the Pas\TeX\ drivers, but the parameters are used
differently, i.e, more intensely.  Do \emph{not} mingle the two versions.
@d Get the arguments this script is called with
@{Parse Arg FONTNAME DPI HBASE VBASE DRIVER PKFILEARG PKDIRARG .

LOGSTRING = "Making font" FONTNAME "at" DPI "dpi and base"
LOGSTRING = LOGSTRING || " ("HBASE","VBASE")"
Say LOGSTRING
@| FONTNAME DPI HBASE VBASE DRIVER PKFILEARG PKDIRARG @}

These files will show up in the current directory later and
will be copied to the appropriate places further below.
These are the `extended' file names used by~\MF.
@d Create some internal defaults we need
@{CURGFFILENAME  = FONTNAME || "." || DPI || "gf"
CURPKFILENAME  = FONTNAME || "." || DPI || "pk"
CURLOGFILENAME = FONTNAME || ".log"
CURTFMFILENAME = FONTNAME || ".tfm"

LOGFILENAME = FONTNAME || "." || DPI || "log"
@| CURGFFILENAME CURPKFILENAME CURLOGFILENAME CURTFMFILENAME LOGFILENAME @}

Check the top level directories for the respective files.
@d Create some internal defaults we need
@{Call SafeMakeDir(LOGDIR)
Call SafeMakeFile(MASTERLOG)
Call SafeMakeDir(PKDIR)
Call SafeMakeDir(TFMDIR)

If 0 ~= KEEPGFFILES Then
  Call SafeMakeDir(GFDIR)
@}

With the new directory structure, \texttt{ShowDVI} and \texttt{DVIprint}
already provide the \texttt{<mode>} we want to call \MF~with.  It comes
as the rightmost subdirectory name in the \texttt{PKDIRARG} argument.
The font configuration files can be set up so that only the \texttt{<mode>}
part embraced to the left and right by single slashes is supplied.
@d Run {\MF} to generate the font
@{@<Determine the mode we're in@>
@<Determine the magnification@>
@<Set up the command line options@>
@<Call {\MF}@>
@<Check for possible errors@>
@}

@d Determine the mode we're in
@{MODE = Left(PKDIRARG,LastPos("/",PKDIRARG)-1)
MODE = SubStr(MODE,LastPos("/",MODE)+1)
@| MODE @}

The \texttt{<mag>} parameter for \MF\ is almost trivial.  With the above
setting we can omit the tedious calculations done in version~1.  And there
is no need for the dubious `\texttt{config/modes}' file any longer.
@d Determine the magnification
@{MAG = DPI / HBASE
@| MAG @}

The missing information will come directly from \MF\ itself, because it
should best know where the font files are located.  \TDS\ helps greatly
reduce the effort to determine the other locations.  Here we take for
granted that the `missing font' actually was never created before and
so we call \MF~first (it won't be found, if \TDS\ is not correctly set~up).
@d Set up the command line options
@{MFOPTIONS = "\mode="MODE"; mag="MAG"; scrollmode; input" FONTNAME
@| MFOPTIONS @}

First, create the gf file and the tfm file and the log file by calling
the \MF\ program.  If anything below fails, you can proceed by hand; all
necessary files will be present in the current directory.  To allow
`restart', \MF~is not called when the expected output file is already
present.  So you should delete the gf file if parts are missing.
@d Call {\MF}
@{Say "Calling MetaFont with base: &"BASE "and startup commands:"
Say "`"MFOPTIONS"'"

If ~Exists(CURGFFILENAME) Then
  VIRMF '&'BASE '"'MFOPTIONS'"'
Else RC = 0 /* Simulate a perfect result */
@| RC @}

\MF\ returns codes 0, 5, 10, and 20 respectively, so we can take
appropriate actions.  There is a slight improvement over version~1,
the log file is consulted to determine the name of the font driver
file, which is almost never in the current directory.  However, the
log file is not sufficiently to determine the correct error position.
@d Check for possible errors
@{ERRORCODE = RC

If 5 >= ERRORCODE Then Do
  If 0 < ERRORCODE Then
    Call Error
  Else
    Say FONTNAME "translated without errors."
End; Else Do
  Call Error
  Exit 0
End
@| ERRORCODE EDITSCRIPT MFREXXEDIT SOURCEFILE MFEdit.rexx @}

Second, create the pk file by calling the \texttt{GFtoPK} program.
This is done in the local directory, not yet in the final path.
@d Run \texttt{GFtoPK} to convert the pixel format
@{GFTOPK CURGFFILENAME
@<Check the output@>
@| GFTOPK @}

Don't delete the gf file on failure.  The extra information
attached to the pk file is a convenience, this and even more
information is actually contained in the pk file itself (if
the \MF\ base has `\texttt{modes.mf}' loaded).
@d Check the output
@{If Exists(CURPKFILENAME) Then Do
  If 0 = KEEPGFFILES Then
    Delete CURGFFILENAME
  FileNote CURPKFILENAME '"mode='MODE', mag='MAG'"'
End; Else Do
  Call Log("Where is my "CURPKFILENAME"?")
  Exit 5
End
@}

Third, check all directories needed for installation and try to move the
created files appropriately.  For this we consult the log file directly.
@d Install the files at the appropriate places
@{@<Determine the source path@>
@<Determine the supplier and the typeface@>
@<Install the log file@>
@<Install the tfm file@>
@<Install the pk file@>
@<Install the gf file@>
@}

@d Determine the source path
@{If ~Open('LOGFILE',CURLOGFILENAME,'READ') Then Do
  Call Log("Can't open "CURLOGFILENAME"!")
  Exit 10
End

LINE = ReadLn('LOGFILE') /* Dummy read the first intro line */
LINE = ReadLn('LOGFILE') /* Dummy read the second calling line */
LINE = ReadLn('LOGFILE') /* Read the third line for real */

SOURCEFILE = Word(LINE,1) /* The `driver' file is what we want */

If ~Close('LOGFILE') Then Do
  Call Log("Can't close "CURLOGFILENAME"!")
  Exit 10
End
@| LOGFILE LINE SOURCEFILE @}

Get the \texttt{<supplier>} and \texttt{<typeface>} parts of the
directory names.  Both keep the slash at the left, but not on the
right side.  This code is a bit tricky, so don't fool around with~it.
@d Determine the supplier and the typeface
@{SUPPLIER = SubStr(SOURCEFILE,Length(SOURCEDIR)+2)
/* `+1' for the open brace and `+1' for the leftmost slash */
SUPPLIER = Left(SUPPLIER,LastPos("/",SUPPLIER)-1)

TYPEFACE = SubStr(SUPPLIER,LastPos("/",SUPPLIER))
SUPPLIER = Left(SUPPLIER,LastPos("/",SUPPLIER)-1)
@| SUPPLIER TYPEFACE @}

@d Install the log file
@{@<Check and create the log directory@>
@<Copy the log file@>
@}

Check the directory structure for the renamed log file.  The following
steps directly document the \TDS\ on my system and thus should be fairly
easy to adopt for a (slightly) different scheme.
@d Check and create the log directory
@{LOGDIR = LOGDIR || SUPPLIER
Call SafeMakeDir(LOGDIR)

LOGDIR = LOGDIR || TYPEFACE
Call SafeMakeDir(LOGDIR)

LOGDIR = LOGDIR || "/" || MODE
Call SafeMakeDir(LOGDIR)

LOGDIR = LOGDIR || "/dpi" || DPI
Call SafeMakeDir(LOGDIR)

LOGFILENAME = LOGDIR || "/" || LOGFILENAME
@}

We use `Copy/Delete' instead of `Rename' to overwrite the existing log
file (e.g., from a failed call of \texttt{MakeTeXfont}).  This works
between devices,~too.
@d Copy the log file
@{Copy CURLOGFILENAME LOGFILENAME clone
Delete CURLOGFILENAME
@}

@d Install the tfm file
@{@<Check and create the tfm directory@>
@<Copy the tfm file@>
@}

Check the directory structure for the renamed tfm file.
@d Check and create the tfm directory
@{TFMDIR = TFMDIR || SUPPLIER
Call SafeMakeDir(TFMDIR)

TFMDIR = TFMDIR || TYPEFACE
Call SafeMakeDir(TFMDIR)

TFMFILENAME = TFMDIR || "/" || CURTFMFILENAME
@}

Check the tfm file; if it doesn't exist, move it there
and delete it here.  Once a tfm file is correctly installed,
it is valid for all pk files together (even if there were milder
forms of warnings in the \MF~run).
@d Copy the tfm file
@{If ~Exists(TFMFILENAME) Then Do
  Copy CURTFMFILENAME TFMFILENAME clone
  Protect TFMFILENAME r
End

Delete CURTFMFILENAME
@}

@d Install the pk file
@{@<Check and create the pk directory@>
@<Copy the pk file@>
@}

Check the directory structure for the renamed pk file.
@d Check and create the pk directory
@{PKDIR = PKDIR || SUPPLIER
Call SafeMakeDir(PKDIR)

PKDIR = PKDIR || TYPEFACE
Call SafeMakeDir(PKDIR)

PKDIR = PKDIR || "/" || MODE
Call SafeMakeDir(PKDIR)

PKDIR = PKDIR || "/dpi" || DPI
Call SafeMakedir(PKDIR)

PKFILENAME = PKDIR || "/" || CURPKFILENAME
@}

Look if the font already is installed.  (Shouldn't happen!)
@d Copy the pk file
@{If Exists(PKFILENAME) Then Do
  LOGSTRING = PKFILENAME
  LOGSTRING = LOGSTRING "already exists;" '0A'X
  LOGSTRING = LOGSTRING "check your driver's configuration!"
  Say LOGSTRING
  Call Log(LOGSTRING)
  Delete CURPKFILENAME
  Exit 5
End

Copy CURPKFILENAME PKFILENAME clone
Delete CURPKFILENAME
Protect PKFILENAME r
@}

The `generic font' files are only kept, when you modify this script in
the section about `user-definable stuff'.
@d Install the gf file
@{If 0 ~= KEEPGFFILES Then Do /* Optionally store GF files */
  @<Check and create the gf directory@>
  @<Copy the gf file@>
End
@}

Check the directory structure for the renamed gf file.
@d Check and create the gf directory
@{GFDIR = GFDIR || SUPPLIER
Call SafeMakeDir(GFDIR)

GFDIR = GFDIR || TYPEFACE
Call SafeMakeDir(GFDIR)

GFDIR = GFDIR || "/" || MODE
Call SafeMakeDir(GFDIR)

GFDIR = GFDIR || "/dpi" || DPI
Call SafeMakedir(GFDIR)

GFFILENAME = GFDIR || "/" || CURGFFILENAME
@}

Look if the font already is installed.  (Shouldn't happen!)
@d Copy the gf file
@{If Exists(GFFILENAME) Then Do
  LOGSTRING = GFFILENAME
  LOGSTRING = LOGSTRING "already exists;" '0A'X
  LOGSTRING = LOGSTRING "check your driver's configuration!"
  Say LOGSTRING
  Call Log(LOGSTRING)
  Delete CURGFFILENAME
  Exit 5
End

Copy CURGFFILENAME GFFILENAME clone
Delete CURGFFILENAME
Protect GFFILENAME r
@}

\subsubsection{Local subroutines}

And finally we define the local routines used in the main body.
@d Local procedures for \texttt{MakeTeXFont.rexx}
@{BREAK_C:
BREAK_D:
  Call Log "Execution aborted!"

  If Exists(CURGFFILENAME) Then
    Delete CURGFFILENAME
  If Exists(CURLOGFILENAME) Then
    Delete CURLOGFILENAME

  Exit 10
@| BREAK_C BREAK_D @}

Log-function
@d Local procedures for \texttt{MakeTeXFont.rexx}
@{Log: Procedure Expose MASTERLOG FONTNAME MODE HBASE VBASE DPI MAG
Parse Arg MSG

MESSAGE = Date() Time() || ":"
MESSAGE = MESSAGE FONTNAME "for" MODE
MESSAGE = MESSAGE "("HBASE","VBASE") with" DPI "dpi"
MESSAGE = MESSAGE "(mag="MAG")" '0A'X
MESSAGE = MESSAGE || MSG
Say MESSAGE

If ~Open(LOGFILE,MASTERLOG,'Append') Then Do
  Say "Can't open logfile!"
  Return
End

DUMMY = WriteLn(LOGFILE,MESSAGE)
If ~Close(LOGFILE) Then
  Say "Can't close logfile!"

Return
@| Log @}

@d Local procedures for \texttt{MakeTeXFont.rexx}
@{Error:
  Say "Translation problem, MetaFont returned "ERRORCODE"."
  Call Log("Translation problem, MetaFont returned "ERRORCODE".")
Return
@| Error @}

When will ARexx supply `GetEnv/SetEnv'?
@d Local procedures for \texttt{MakeTeXFont.rexx}
@{MyGetEnv: Procedure
Parse Arg NAME

If Open(TEMPFILE,"ENV:"||NAME,'READ') Then Do
  GIVES = Readln(TEMPFILE)
  Call Close TEMPFILE
End; Else
  GIVES = ""

Return GIVES
@| MyGetEnv GIVES TEMPFILE @}

Create a subdirectory.
@d Local procedures for \texttt{MakeTeXFont.rexx}
@{SafeMakeDir: Procedure
Parse Arg DIRNAME

If ~Exists(DIRNAME) Then Do
  MakeDir DIRNAME
  If ~Exists(DIRNAME) Then Do
    Call Log("Could not create directory" DIRNAME)
    Exit 10
  End
  Say "Directory ("DIRNAME") did not exist.  It was created."
End

Return
@| SafeMakeDir DIRNAME @}

Create a file.
@d Local procedures for \texttt{MakeTeXFont.rexx}
@{SafeMakeFile: Procedure
Parse Arg FILENAME

If ~Exists(FILENAME) Then Do
  "echo >" || FILENAME
  If ~Exists(FILENAME) Then Do
    Call Log("Could not create file="FILENAME)
    Exit 10
  End
  Say "File ("FILENAME") did not exist.  It was created."
End

Return
@| SafeMakeFile FILENAME @}

\section{Files}

@f

\section{Macros}

@m

\section{Identifiers}

@u

\end{document}
