#!/bin/sh
# MakeTeXPK -- make a new PK font, because one wasn't found.
# customised for the debian distribution of TeX by
# Nils Rennebarth <nils@hertha.exp-math.uni-essen.de>
# 
# (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="ps2pk"}
: ${MAPFILE=/usr/lib/texmf/dvips/psfonts.map}
: ${MAPFILE2=/usr/lib/texmf/dvips/psfonts.add}

# 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=/var/spool/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

# Since we want to run it in a temporary directory, and may run metafont,
# add the current directory to MFINPUTS.
MFINPUTS=`pwd`:${MFINPUTS}:
export MFINPUTS

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

# Is this a PostScript font?
# grep for the font in $MAPFILE.
# 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)?([    ]|$)'
test -n "$gsftopk" && egrep "$pattern" $MAPFILE >psline
if test ! -s psline; then
test -n "$gsftopk" && egrep "$pattern" $MAPFILE2 >psline
fi
if test -s psline; then
  # This is a PostScript font.
  MODE=$gsftopk
  case $gsftopk in
       ps2pk*) # This is a terrible hack alltogher. Sigh
               special_part=`cat psline | sed -e 's/^[^"]*"//' -e 's/"[^"]*$//'`
               # .167 SlantFont
               slant=`echo $special_part \
                      | awk '{ if ($2 == "SlantFont") print "-S" $1 }'`
               extend=`echo $special_part \
                       | awk '{ if ($2 == "ExtendFont") print "-E" $1 }'`
               line=`cat psline`
               encod=`expr "$line" : '.*<\(.*.enc\).*'`
               test -n "$encod" && encod=-e$encod
               SRC=`expr "$line" : '.*<\(.*.pf[ab]\).*'`
               SRC=`kpsewhich -format 16 $SRC`;
               AFM=`echo $SRC \
                    | sed -e 's,/type1/,/afm/,' -e 's,\.pf[ab]$,.afm,'`
               cmd="$gsftopk -X$DPI $encod $slant $extend -a$AFM $SRC $NAME.${DPI}pk"
               DESTDIR=/var/spool/texmf/fonts/pk/ps2pk
               ;;
            *) cmd="$gsftopk $NAME $DPI"
               ;;
  esac
  $cmd

else

  # We are down to trying Metafont.
  # 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

fi

test -d $DESTDIR \
  || mkdir $DESTDIR \
  || chmod o+t $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
