#!/bin/sh
#
#  Do not run this script as a setuid program!  This will
#  result in a major security hole!
#
#   This script file makes a new TeX TFM file, because one wasn't
#   found.  Parameters are:
#
#   name dpi bdpi magnification [mode [subdir]]
#
#   `name' is the name of the font, such as `cmr10'.  `dpi' is
#   the resolution the font is needed at.  `bdpi' is the base
#   resolution, useful for figuring out the mode to make the font
#   in.  `magnification' is a string to pass to MF as the
#   magnification.  `mode', if supplied, is the mode to use.
#
#   Note that this file must execute Metafont, and then place the 
#   result in the correct location for it to be found subsequently.
#   If this doesn't work, it will be evident because MF will be
#   invoked over and over again.
#
#   Of course, it needs to be set up for your site.
#
PATH=/usr/bin:/bin:
LOCALDIR=/usr/lib/texmf
DESTDIR=$LOCALDIR/fonts/metafont/tfm
#
# TEMPDIR needs to be unique for each process because of the possibility
# of simultaneous processes running this script.
#
if test "$TMPDIR" = ""
then
   TEMPDIR=/tmp/mtpk.$$
else
   TEMPDIR=$TMPDIR/mtpk.$$
fi
NAME=$1
DPI=$2
BDPI=$3
MAG=$4
MODE=$5
#
#   Prevent display under the X Window System.  Except it doesn't always
#   work; some sh'ells don't seem to understand unset.  There are also some
#   versions of METAFONT that don't work if the DISPLAY isn't set and
#   the term type is set to xterm.
#
# unset DISPLAY
umask 0

GFNAME=$NAME.$DPI'gf'
TFMNAME=$NAME.'tfm'

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

if test ! -d $DESTDIR
then
   mkdir $DESTDIR
   chmod 755 $DESTDIR
fi

# added by gwb, to allow searching in current dir before cd'ing
if test "$MFINPUTS" != ""
then
   MFINPUTS=$MFINPUTS:`pwd`; export MFINPUTS
fi
mkdir $TEMPDIR
cd $TEMPDIR

if test -r $DESTDIR/$TFMNAME
then
   echo "$DESTDIR/$TFMNAME already exists!"
   exit 0
fi

unset DISPLAY
echo "mf \"\\mode:=$MODE; scrollmode; input $NAME\" < /dev/null"
mf "\mode:=$MODE; mag:=$MAG; scrollmode; input $NAME" < /dev/null
if test ! -r $GFNAME
then
   echo "Metafont failed for some reason on $GFNAME"
   exit 1
fi

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

mv $TFMNAME $DESTDIR/tfmtmp.$$
rm -f $GFNAME $NAME.log
cd $DESTDIR
mv tfmtmp.$$ $TFMNAME
chmod 644 $TFMNAME
exit 0
