#!/bin/sh
# 
# 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.
# 
# Parameters:
#   name dpi bdpi magnification [mode [destdir]]
#
#   `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. Unless it's `default', in
#     which case we guess. (This is so people can specify a destdir
#     without a mode.)
#   `destdir', if supplied, is either the absolute directory name to use
#     (if it starts with a /) or relative to the default DESTDIR (if not).

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

# Clean up on normal or abnormal exit.
trap 'cd /; test -f $TEMPDIR/mtout.$$ && cat $TEMPDIR/mtout.$$; rm -rf $TEMPDIR; trap '' 0; exit 0' 0 1 2 15

(
# These search paths will be changed to include `pwd`. This is necessary
# since wo will cd to $TEMPDIR.
: ${KPSE_DOT=`pwd`}; export KPSE_DOT

# Go to the unique working directory.
test -d $TEMPDIR || mkdir $TEMPDIR 
cd $TEMPDIR || exit 1

progname=`basename $0`
: ${PSMAPFILE=`kpsetool -w dvips_config psfonts.map`}

if test $# -lt 4; then
  echo "Usage: $progname name dpi bdpi mag [mode [destdir]]."
  exit 1
fi

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

# Check that $BDPI and $MODE are consistent; if not, ignore the mode and
# hope we can correctly guess it from bdpi.  (People like to specify the
# resolution on the command line, not the mode so much.)
if test -n "$MODE"; then
  mf_bdpi=`mf \
'\mode:='$MODE';mode_setup;message"BDPI= "&decimal round pixels_per_inch;end.'\
           </dev/null \
           | awk '/DPI=/ {print $2}'`
  if test $mf_bdpi != $BDPI; then
    echo "$progname: Mismatched mode $MODE and resolution $BDPI; ignoring mode." >&2
    unset MODE
  fi
fi

: ${TEXMF=`kpsetool -v '$TEXMF'`}
: ${MAKETEXDIR=$TEXMF/maketex}
export TEXMF MAKETEXDIR

# grep for the font in $PSMAPFILE, if some ps-to-pk is claimed to be supported.
# We have to figure out the name of the base font -- $NAME is probably
# something like pplr, but it's rpplr or pplr0 or pplr8r that's in psfonts.map.
pattern="^r?$NAME"'(0|8r)?([ 	]|$)' 
psline=`egrep "$pattern" $PSMAPFILE`
if test -n "$psline"; then
  cmd="gsftopk $NAME $DPI"
  MODE=gsftopk
else
  # If an explicit mode is not supplied, try to guess. You can get a
  # list of extant modes from ftp.cs.umb.edu:pub/tex/modes.mf.
  if test -z "$MODE" || test "$MODE" = default; then
    case "$BDPI" in
      85) MODE=sun;;
     100) MODE=nextscrn;;
     300) MODE=cx;;
     400) MODE=nexthi;;
     600) MODE=ljfour;;
    1270) MODE=linolo;;
       *) echo "$progname: Can't guess mode for $BDPI dpi devices."
          echo "$progname: Use a config file, or update me."
          exit 1
    esac
  fi

  # Run Metafont. Always use plain Metafont, since reading cmbase.mf
  # does not noticeably slow things down.
  cmd="mf \mode:=$MODE; mag:=$MAG; scrollmode; input $NAME"
fi

donames='set x `MakeTeXnames $NAME $DPI $MODE $DEST`
PKDEST=$2
PKDESTDIR=`echo $PKDEST | sed "s/^[^\/]*$/./; s/\/[^\/]*$//"`
PKNAME=`basename $PKDEST`
GFNAME=$NAME.${DPI}gf'

eval "$donames"

# Allow fonts to be read and written (especially in case we make
# directories) by everyone.  
umask 0

# Possible local customizations?
test -r $MAKETEXDIR/maketex.site && . $MAKETEXDIR/maketex.site

if test -r $PKDESTDIR/$PKNAME; then
  echo "$progname: $PKDESTDIR/$PKNAME already exists."
  echo $PKDESTDIR/$PKNAME > $TEMPDIR/mtout.$$
  append_db $PKDESTDIR $PKNAME
  exit
fi

mdir='MakeTeXmkdir "$PKDESTDIR"
test -d "$PKDESTDIR" ||
  { echo "$progname: could not mkdir $PKDESTDIR."; exit 1; }'
eval "$mdir"

echo "$progname: Running $cmd"
$cmd </dev/null
ret=$?
if test -z "$psline"; then  
  test -r $GFNAME ||
    { test -r $NAME.`expr $DPI + 1`gf && DPI=`expr $DPI + 1` &&
      eval "$donames" && eval "$mdir"; } ||
    { test -r $NAME.`expr $DPI - 1`gf && DPI=`expr $DPI - 1` &&
      eval "$donames" && eval "$mdir"; } ||
    { echo "$progname: Metafont failed to make $GFNAME."; exit 1; }
  gftopk ./$GFNAME $PKNAME || exit 1
fi
test ! -f $PKNAME && test -f $NAME.${DPI}pk && mv $NAME.${DPI}pk $PKNAME
test -s $PKNAME ||
  { echo "$progname: '$cmd' failed to make $PKNAME."; exit 1; }

# Install the PK file carefully, since others may be working simultaneously.
mv $PKNAME $PKDESTDIR/pktmp.$$ \
  || { echo "$progname: Could not mv $PKNAME $PKDESTDIR/pktmp.$$."; exit 1; }

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

# If this line (or an equivalent) is not present, dvipsk/xdvik/dviljk
# will think MakeTeXPK failed.  Any other output to stdout will also lose.
append_db $PKDESTDIR $PKNAME
echo $PKDESTDIR/$PKNAME > $TEMPDIR/mtout.$$
) 1>&2 </dev/null
