#!/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.$$

# 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

# 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

# start of redirection stdout -> stderr, stdin <- /dev/null
(

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

: ${MAKETEXDIR=`kpsetool -v '$MAKETEXDIR'`}
export 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
  set x `echo "$psline" | sed 's@[<"]@@g'`
  shift; shift; shift;
  unset encoding psname slant extend
  while test ! -z "$1"; do
    case "$1" in
      *.enc)       encoding="-e $1";;
      *.pf[ab])    psname="$1";;
      *SlantFont)  slant="-S $lastopt";;
      *ExtendFont) extend="-E $lastopt";;
    esac
    lastopt="$1"
    shift
  done
  cmd="gsftopk $NAME $DPI"
  if [ -z "$psname" ]; then
    cmd="gsftopk $NAME $DPI"
    MODE=gsftopk
  else
    cmd="ps2pk -v -X$DPI -R$BDPI $slant $extend $encoding $psname $NAME.${DPI}pk"
    MODE=ps2pk
  fi
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;;
     300) MODE=cx;;
     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

set x `MakeTeXnames $NAME $DPI $MODE $DEST`
PKDEST=$2
PKDESTDIR=`dirname $PKDEST`
PKNAME=`basename $PKDEST`
GFNAME=$NAME.$DPI'gf'

# 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

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

echo "$progname: Running $cmd"
$cmd </dev/null
ret=$?
if test -z "$psline"; then  
  test -r $GFNAME ||
    { 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
# end of redirection stdout, stdin
