#!/bin/sh 
#
#   This script file makes a new X/TeX screen font, because one wasn't
#   found.  Parameters are:
#
#   name dpi bdpi magnification destdir
#
#   `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.  'destdir' is the directory in which to cache the new
#   font.
#
#   Note that this file must execute Metafont, mftobdf, and then bdftopcf,
#   and place the result in the correct location for X
#   to find it subsequently.  
#
#   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.
#
# (Gary Flake, flake@scr.siemens.com): Cleaned this up so that it would
# run on a linux box.  Here is a list of font utilities that are needed:
#
#   /usr/TeX/bin/mftobdf  (not included w/ standard T disks)
#   /usr/TeX/bin/cmmf     (symbolic link to virmf)
#   /usr/X11/bin/bdftopcf (included w/ standard T disks)

TEMPDIR=/tmp/bdf-pcf.$$
NAME=$1
DPI=$2
BDPI=$3
MAG=$4
MODE=$5
DESTDIR=$6
umask 0

#  Something like the following is useful at some sites.
GFNAME=$NAME.$DPI'gf'
BDFNAME=$NAME.$DPI.'bdf'
PCFNAME=$NAME.$DPI.'pcf'
SNFNAME=$NAME.$DPI.'snf'
DESTDIR=/usr/TeX/lib/texmf/fonts/xtex
COMPRESS=1


if [ "$COMPRESS" = "1" ]; then
  BDFZNAME=${BDFNAME}'.Z'
else
  BDFZNAME=${BDFNAME}
fi

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

mkdir $TEMPDIR
cd $TEMPDIR

if [ -r $DESTDIR/$BDFZNAME ]; then
  echo "$DESTDIR/$BDFZNAME already exists!"
  cd $DESTDIR
  echo mkfontdir $DESTDIR
  mkfontdir $DESTDIR
  xset fp rehash
  exit 0
fi

##
# First try mftobdf, maybe it exists...
##

echo "1st mftobdf -dpi" $DPI $NAME
mftobdf -dpi $DPI $NAME
if [ ! -r $BDFNAME ]; then 
  echo cmmf "\mode:=$MODE; mag:=$MAG/1000; scrollmode; input $NAME </dev/null"
  cmmf "\mode:=$MODE; mag:=$MAG/1000; scrollmode; input $NAME" </dev/null
  if [ -r $GFNAME ]; then
    #
    # My local metafont gives bogus names occasionally. Don't know why.
    #
    echo "Unable to find $GFNAME in directory $cwd"
    echo "Metafont failed for some reason on $GFNAME, but continuing anyway"
  fi

  echo "mftobdf -dpi" $DPI $NAME
  mftobdf -dpi $DPI $NAME
  if [ ! -r $BDFNAME ]; then 
    echo "Mftobdf failed for some reason on $BDFNAME"
    exit 1
  fi
fi

# from bsdtopcf

echo bdftopcf $BDFNAME ">" $PCFNAME
bdftopcf $BDFNAME > $PCFNAME

if [ ! -r $PCFNAME ]; then
  echo "Font compiler failed for some reason on $PCFNAME"
  exit 1
fi

# Install the BDF and PCF files carefully, since others may be doing the same
# as us simultaneously.

cp $PCFNAME $DESTDIR/pcftmp.$$
cd $DESTDIR
mv pcftmp.$$ $PCFNAME

if [ "$COMPRESS" = "1" ]; then
  echo compress -f $PCFNAME
  compress -f $PCFNAME
fi

echo mkfontdir $DESTDIR
mkfontdir $DESTDIR
xset fp rehash

exit 0


