#!/bin/sh
# original MakeTeXPK -- make a new PK font, because one wasn't found.
# 
# (If you change or delete the word `original' on the previous line,
# installation won't write this MakeTeXPK over yours.)
# 
# Parameters:
#
#   name dpi bdpi magnification [mode]
#
#   `name' is the base name of the font, such as `cmr10'.
#   `dpi' is the resolution the font is needed at.
#   `bdpi' is the base resolution, used to intuit the mode to use.
#   `magnification' is a string to pass to MF as the value of `mag'.
#   `mode', if supplied, is the mode to use.
#
# This script must echo the name of the generated PK file (and nothing
# else) to standard output. Yes, this is different from the original dvips.

echo "Running MakeTeXPK $*" 1>&2

# Define to `gsftopk' or `ps2pk' or whatever to make PK files for
# PostScript fonts. If this is defined, MAPFILE must also be defined to
# be your psfonts.map file or some equivalent. The Makefile substitutes
# for this, too. You can get gsftopk from
# math.berkeley.edu:pub/Software/TeX/gsftopk.tar.Z.
: ${gsftopk=""}
: ${MAPFILE=/usr/lib/texmf/dvips/psfonts.map}

# TEMPDIR needs to be unique for each process because of the possibility
# of simultaneous processes running this script.
TEMPDIR=${TMPDIR-/tmp}/mtpk.$$

NAME=$1
DPI=$2
BDPI=$3
MAG=$4
MODE=$5

# Where to put the new file
: ${DESTDIR=/usr/local/lib/texmf/fonts/pk/$MODE}

umask 0

GFNAME=$NAME.$DPI'gf'
PKNAME=$NAME.$DPI'pk'

# Have we been spuriously called? No harm done, if so.
if test -r $DESTDIR/$PKNAME; then
  echo "$DESTDIR/$PKNAME already exists!" 1>&2
  echo $DESTDIR/$PKNAME
  exit 0
fi

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

# Do we have a GF file in the current directory?
if test -r $GFNAME; then
  echo "gftopk ./$GFNAME $PKNAME" 1>&2
  gftopk ./$GFNAME $PKNAME
  # Don't move the font; if the person knows enough to make fonts, they
  # know enough to have . in the font paths.
  echo $PKNAME
  exit 0
fi

# Is this a PostScript font?
if test -n "$gsftopk" && egrep \^$NAME'([ 	]|$)' $MAPFILE >/dev/null; then
  echo "Running $gsftopk $NAME $DPI." 1>&2
  if $gsftopk $NAME $DPI 1>&2; then
    echo $NAME
    exit 0
  else
    echo "$gsftopk failed."
    exit 1
  fi
fi
  
# We're down to trying Metafont.  Since we want to run it in a
# temporary directory, add the current directory to MFINPUTS.
MFINPUTS=`pwd`:${MFINPUTS}:
export MFINPUTS

test -d $TEMPDIR || mkdir $TEMPDIR 
cd $TEMPDIR || exit 1

# Which version of Metafont shall we use?
# (Just plain Metafont is safest)
mf=mf

# If an explicit mode is not supplied, try to guess. You can get a
# complete list of extant modes from ftp.cs.umb.edu:pub/tex/modes.mf.
if test -z "$MODE"; then
  case "$BDPI" in
    85) MODE=sun;;
   118) MODE=lview;;
   300) MODE=CanonCX;;
   600) MODE=ljfour;;
  1270) MODE=LinotypeOneZeroZero;;
     *) echo "MakeTeXPK doesn't have a guess for $BDPI dpi devices." 1>&2
        echo "Put the mode in a config file, or update MakeTeXPK." 1>&2
        exit 1
  esac
fi

# Run Metafont. 
echo "Running $mf \mode:=$MODE; mag:=$MAG; scrollmode; input $NAME" 1>&2
$mf "\mode:=$MODE; mag:=$MAG; scrollmode; input $NAME" </dev/null 1>&2

if test ! -r $GFNAME; then
  # Maybe it succeeded at DPI +- 1?  Annoying to have to check for this,
  # but means we can use floating-point in the sources, so fine.
  test_dpi=`expr $DPI - 1`
  test_name=$NAME.${test_dpi}
  if test -r ${test_name}gf; then
    GFNAME=${test_name}gf
    PKNAME=${test_name}pk
  else
    test_dpi=`expr $DPI + 1`
    test_name=$NAME.${test_dpi}
    if test -r ${test_name}gf; then
      GFNAME=${test_name}gf
      PKNAME=${test_name}pk
    else
      echo "MakeTeXPK failed to make $GFNAME." 1>&2
      exit 1
    fi
  fi
fi

# Metafont succeeded.  Make the PK file.
gftopk ./$GFNAME $PKNAME

test -d $DESTDIR \
  || mkdir $DESTDIR \
  || (echo "${DESTDIR}: MakeTeXPK could not create directory." 1>&2; exit 1)

# Install the PK file carefully, since others may be working simultaneously.
mv $PKNAME $DESTDIR/pktmp.$$ \
  || (echo "$0: Could not mv $PKNAME $DESTDIR/pktmp.$$." 1>&2; exit 1)

cd $DESTDIR || exit 1
mv pktmp.$$ $PKNAME
chmod 644 $PKNAME

# If this line (or an equivalent) is not present, dvipsk/xdvik will think
# MakeTeXPK failed.  Any other output to stdout will likewise lose.
echo $DESTDIR/$PKNAME
