#!/bin/sh
# NTeX MakeTeXPK -- make a new PK font, because one wasn't found.
# 
# 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.
#   `destdir', if supplied is either the absolute directory name to use
#     (if it starts with a /) or relative to the default DESTDIR (if not).

echo "NTeX's MakeTeXPK" >&2

DESTDIR=/usr/lib/texmf/fonts/tmp/pk

# 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="/usr/bin/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=/tmp/mtpk.$$

LOCALDIR=/usr/lib/texmf/fonts

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

# 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"; then
  case "$BDPI" in
    85) MODE=sun;;
   118) MODE=lview;;
   300) MODE=CanonCX;;
   600) MODE=ljfour;;
  1270) MODE=LinotypeOneZeroZero;;
     *) echo "ERROR: MakeTeXPK doesn't have a guess for $BDPI dpi devices." >&2
        echo "       Put the mode in a config file or update MakeTeXPK." >&2
        exit 1
  esac
fi

echo "Trying to create font $NAME ("$BDPI"dpi) at "$DPI"dpi ($MAG) as $MODE " >&2

if test ! -d $DESTDIR; then
   mkdir $DESTDIR
   if test $? != 0; then
     echo "ERROR: Could not create $DESTDIR" >&2
     exit 1
   fi
   chmod 1777 $DESTIR >&/dev/null
fi

if test -n "$6"; then
  case "$6" in
    /*) DESTDIR="$6";;
     *) DESTDIR="$DESTDIR/$6";;
  esac
else
  DESTDIR=$DESTDIR/$MODE
fi


umask 0

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

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

# check if a pk file, which may be compressed, exists 

if test -n $FOUND
then
   case $FOUND in
     *pk)  echo "WARING: The font already exists in $FOUND" >&2
           echo $FOUND
	   exit 0;;
    *.gz|*.z|*.Z)   cmd="gzip -d $FOUND"
		    echo -n "Decompressing the font $FOUND..." >&2
		    if $cmd; then
                    	echo `echo $FOUND | sed 's/\.gz$//'`
			echo >&2
			chmod 0644 $FOUND >&/dev/null
                        exit0
		    else
                        echo >&2
			echo "ERROR: decompressing failed for some reason" >&2
			exit 1
		    fi;;
    *gf)  cmd="gftopk $FOUND $PKNAME"
          echo -n "Making a pk font from $FOUND..."  >&2
	  if cmd; then
            echo >&2
            test -d $DESTDIR \
             || mkdir $DESTDIR \
             || (echo "ERROR: MakeTeXPK could not create directory ${DESTDIR}." >&2; exit 1)
            chmod 1777 $DESTDIR >&/dev/null
            mv $PKNAME $DESTDIR/pktmp.$$ \
             || (echo "ERROR: MakeTeXPK could not move $PKNAME to $DESTDIR/pktmp.$$." >&2; exit 1)

            cd $DESTDIR 
            mv pktmp.$$ $PKNAME &>/dev/null
	    chmod 0644 $DESTDIR/$PKNAME >&/dev/null
            echo $DESTDIR/$PKNAME
          else
           echo >&2
           echo "ERROR: gftopk failed!" >&2
           exit 1
          fi;;
   esac
fi

# Do we have a GF file in the current directory?
if test -r $GFNAME; then
  cmd="gftopk ./$GFNAME $PKNAME"
  echo -n "$GFNAME found in current directory. Making a pk font..." >&2
  if $cmd; then
    # Don't move the font; if the person knows enough to make fonts, they
    # know enough to have . in the font paths.
    echo $PKNAME
    echo >&2
    chmod 0644 ./$PKNAME >&/dev/null
    echo "$NAME created." >&2
    exit 0
  else
    echo >&2
    echo "gftopk failed." >&2
    exit 1
  fi

## Do we have an F3 font?
#elif test -r $f3dir/$NAME.f3b; then
#  # You will need Sun's f3tobm program, plus a program I wrote.
#  # See ftp.cs.umb.edu:private/f3totex.
#  cd $f3dir || exit 1
#  command="f3topk -r $DPI $NAME"
#  echo "Running $cmd" >&2
#  if $cmd; then
#    # Best if f3topk echoes its output filename, I think.
#    exit 0
#  else
#    echo "f3topk failed." >&2
#    exit 1
#  fi

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

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

  # Which version of Metafont shall we use?
  case $NAME in
    cm*) mf=cmmf;;
      *) mf=mf;;
  esac

  # Run Metafont. 
  echo -n "Running $mf..." >&2

  $mf "\mode:=$MODE; mag:=$MAG; scrollmode; input $NAME" </dev/null &>/dev/null

  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 &>2
        echo "ERROR: MakeTeXPK failed on $GFNAME." >&2
        exit 1
      fi
    fi
  fi

  echo -n gftopk... >&2
  # Metafont succeeded.  Make the PK file.
  gftopk ./$GFNAME $PKNAME &>/dev/null
fi

echo pk >&2

# If we get here, we've succeeded and we're supposed to move the final
# font to $DESTDIR.
test -d $DESTDIR \
  || mkdir $DESTDIR \
  || (echo "ERROR: MakeTeXPK could not create directory ${DESTDIR}." >&2; exit 1)

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

cd $DESTDIR

mv pktmp.$$ $PKNAME
chmod 0644 $PKNAME >&/dev/null

# If this line (or an equivalent) is not present, dvipsk/xdvik/dviljk
# will think MakeTeXPK failed.  Any other output to stdout will also.
echo $DESTDIR/$PKNAME
echo "$NAME created." >&2





