#!/usr/local/bin/bash
#
#   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 bdftosnf,
#   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.
TEMPDIR=/tmp/bdf-snf.$$
NAME=$1
DPI=$2
BDPI=$3
MAG=$4
MODE=$5
DESTDIR=$6
umask 0

#declare -i cmfound=0
#MFDIRS=`echo $MFINPUTS | gawk -F: '{ for (i=1; i<=NF; i++) print $i }'`
#for f in $MFDIRS
#do
#  if [ -r $f/$NAME.mf ]; then
#    declare -i cmfound=1
#  fi
#done

if [ -r /usr/local/lib/tex/fonts/ps-outlines/$NAME.pfa ] 
then
  echo Building X-font from PostScript outline
  PStoXfont $1 $2 $3 $4 $5 $6
  exit 0
else
  echo Building X-font from MetaFont outline
fi

#  Something like the following is useful at some sites.
GFNAME=$NAME.$DPI'gf'
BDFNAME=$NAME.$DPI.'bdf'
SNFNAME=$NAME.$DPI.pcf

COMPRESS=1

# check if we're not running with MIT server after all
if xdpyinfo|grep -s 'vendor string: *.*MIT.*'; then
#       DESTDIR=/usr/lib/X11/fonts/xtex
#       SNFNAME=$NAME.$DPI.'snf'
        COMPRESS=0
        MITSERVER=1
fi

if test "$COMPRESS" = "1"
then
  SNFZNAME=${SNFNAME}'.Z'
else
  SNFZNAME=${SNFNAME}
fi

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

mkdir $TEMPDIR
cd $TEMPDIR

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

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

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


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

echo "1st mftobdf -dpi" $DPI $NAME
mftobdf -dpi $DPI $NAME
if test ! -r $BDFNAME
then 
  pwd
  echo mf "\mode:=$MODE; mag:=$MAG/1000; scrollmode; input $NAME </dev/null"
  mf "\mode:=$MODE; mag:=$MAG/1000; scrollmode; input $NAME" </dev/null
  if test ! -r $GFNAME
  then
#
# My local metafont gives bogus names occasionally. Don't know why.
#
     echo "Unable to find $GFNAME in directory "`pwd`
     OLDDPI=$DPI
     NEWDPI=`expr $DPI - 1`
     if [ -r $NAME.$NEWDPI'gf' ] ; then
       DPI=$NEWDPI
     fi
     NEWDPI=`expr $DPI + 1`
     if [ -r $NAME.$NEWDPI'gf' ] ; then
       DPI=$NEWDPI
     fi
     if [ -r $NAME.$DPI'gf' ] ; then
       GFNAME=$NAME.$DPI'gf'
       BDFNAME=$NAME.$DPI.'bdf'
       SNFNAME=$NAME.$DPI.pcf
       echo "Metafont built $GFNAME instead of $NAME.${OLDDPI}gf, \
but that's ok..."
       gftopk $GFNAME
     else
       echo "Metafont failed for some reason on $GFNAME, \
but continuing anyway"
     fi
  else
    gftopk $GFNAME
  fi

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

echo "$FONTCOMPILER"
if [ $MITSERVER ]; then
        bdftopcf $BDFNAME > $SNFNAME
else
        /usr/bin/dxfc $BDFNAME > $SNFNAME
fi
if test ! -r $SNFNAME
then
   echo "Font compiler failed for some reason on $SNFNAME"
   exit 1
fi

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

#cp $BDFNAME $DESTDIR/bdftmp.$$
cp $SNFNAME $DESTDIR/snftmp.$$
cd $DESTDIR
#mv bdftmp.$$ $BDFNAME
mv snftmp.$$ $SNFNAME

if test "$COMPRESS" = "1"
then
  compress -f $SNFNAME
fi

if [ $MITSERVER ]; then
        mkfontdir $DESTDIR
else
        /usr/bin/dxmkfontdir $DESTDIR
fi
xset +fp $DESTDIR
xset fp rehash

exit 0