#!/bin/sh
#
#   This script file makes a new TeX TFM font, because one wasn't
#   found.  Usage:
#
#   MakeTFM name [mode]
#
#   `name' is the name of the font, such as `cmr10'.
#   `mode', if supplied, is the mode to use.
#
#   Note that this file must execute Metafont, and place the result 
#   in the correct location.
#
#   Of course, it needs to be set up for your site.
#
# TEMPDIR needs to be unique for each process because of the possibility
# of simultaneous processes running this script.
TEXDIR=/usr/lib/texmf
LOCALDIR=$TEXDIR/fonts
DESTDIR=$LOCALDIR/tmp/tfm
TEMPDIR=/tmp/mttfm.$$
NAME=`echo $1 | sed 's/\.tfm$//'`
MODE=$2

echo >&2
echo "NTeX's MakeTeXTFM" >&2

umask 0

if test "$MODE" = ""
then MODE=nullmode
fi

echo "Trying to create a TeX metric file for $NAME ($MODE)." >&2

TFMNAME=$NAME.'tfm'

# Clean up on normal or abnormal exit
trap "cd /; rm -rf $TEMPDIR $DESTDIR/tfmtmp.$$" 0 1 2 15

if test ! -d $TEMPDIR; then
   mkdir $TEMPDIR
   if test $? != 0; then
     echo "ERROR: Could not create $TEMPDIR" >&2
     exit 1
   fi
else
   echo "WARNING: $TEMPDIR already existed" >&2
fi
cd $TEMPDIR

if test -r $DESTDIR/$TFMNAME
then
   echo "WARNING: $DESTDIR/$TFMNAME already exists!" >&2
   echo >&2
   echo $DESTDIR/$TFMNAME
   exit 0
fi

if test ! -d $DESTDIR; then
   mkdir $DESTDIR
   if test $? != 0; then
     echo "ERROR: Could not create $DESTDIR" >&2
     exit 1
   fi
fi

echo -n "Running MetaFont..." >&2
mf "\mode:=$MODE; mag:=magstep(0); scrollmode; input $NAME" </dev/null >&/dev/null
if test ! -r $TFMNAME
then
   echo >&2
   echo "ERROR: Metafont failed for some reason on $NAME" >&2
   exit 1
fi

# Install the TFM file carefully, since others may be doing the same
# as us simultaneously.

chmod 644 $TFMNAME 1>&2
mv $TFMNAME $DESTDIR/tfmtmp.$$ 1>&2
cd $DESTDIR 1>&2
mv tfmtmp.$$ $TFMNAME 1>&2
echo "$TFMNAME created." >&2
echo $DESTDIR/$TFMNAME

