#!/bin/sh
#
# This script make a new METAFONT cm*.mf or dc*.mf or fc*.mf file
# because one wasn't found.  Usage
#
# MakeTeXMF name
#
# `name' is the name of the font file, such as `cmr10'
#
# This file need the sauter font system and the dc fonts
#
# 940726, FL: customized for NTeX

MFDIR=/usr/lib/texmf/mf
DESTDIR=/usr/lib/texmf/fonts/tmp/src
SAUTERDIR=/usr/lib/texmf/fonts/cm/sauter
DCDIR=/usr/lib/texmf/fonts/dc
FCDIR=/usr/lib/texmf/fonts/language/fc

NAME=`echo $1 | sed 's/\.mf$//'`

echo >&2
echo "NTeX's MakeTeXMF" >&2

umask 0

MFNAME=$NAME.mf

# Which type of font is this?
case $NAME in
  cm*) font=cm;;
  dc*) font=dc;;
  fc*) font=fc;;
  *) font=other;;
esac

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

if test -r $DESTDIR/$MFNAME
then
   echo "WARNING: $DESTDIR/$MFNAME already exists" >&2
   echo $DESTDIR/$MFNAME
   exit 0
fi

if test $font = cm; then
  # The font is CM.  Try Sauter's scripts.
  echo "Trying interpolated/extrapolated CM source for $NAME." >&2
  if test -d $SAUTERDIR; then
   	cd $SAUTERDIR
   	rootfont=`echo $NAME | sed 's/[0-9]*$//'`
   	pointsize=`echo $NAME | sed 's/cm[a-z]*//'`
	if test -e $SAUTERDIR"/base/src/b-"$rootfont".mf"; then
          if test -e $SAUTERDIR"/make-mf"; then
   	    $SAUTERDIR/make-mf $rootfont $pointsize >&2
   	    # Install the MF file carefully, since others may be doing the same
   	    # as us simultaneously.
   	    chmod 644 src/$MFNAME &>/dev/null
   	    mv src/$MFNAME $DESTDIR/mftmp.$$ &>/dev/null
   	    cd $DESTDIR
   	    mv mftmp.$$ $MFNAME &>/dev/null
            echo "$MFNAME created." >&2
   	    echo $DESTDIR/$MFNAME
          else
            echo "ERROR: Could not find make-mf" >&2
          fi
	else
	  echo "ERROR: Sauter base for the font does not exist" >&2
	fi
  else
   	echo "ERROR: Could not find the sauter font directory" >&2
  fi
elif test $font = dc; then
  # The font is DC. 
  echo "Trying interpolated/extrapolated DC source for $NAME." >&2
  if test -d $DCDIR; then
  	cd $DESTDIR
  	rootfont=`echo $NAME | sed 's/[0-9]*$//'`
  	pointsize=`echo $NAME | sed 's/dc[a-z]*//'` 
	if test -e $DCDIR/make-dc-mf; then
		if test -e $DCDIR"/base/src/"$rootfont".mf"; then
                  if test -e $DCDIR"/make-dc-mf"; then
  		    $DCDIR/make-dc-mf $rootfont $pointsize >/dev/null
  		    chmod 644 $MFNAME >/dev/null >/dev/null
                    echo "$MFNAME created." >&2
  		    echo $DESTDIR/$MFNAME
                  else
                    echo "ERROR: Could not find make-dc-mf" >&2
                  fi
		else
		  echo "ERROR: DC basefile for the font does not exist" >&2
		fi
	else
		echo "ERROR: Could not find make-dc-mf" >&2
	fi
   else
	echo "ERROR: Could not find the DC font directory" >&2
   fi
elif test $font = fc; then
  # The font is FC. 
  echo "Trying interpolated/extrapolated FC source for $NAME." >&2
  if test -d $FCDIR; then
  	cd $DESTDIR
  	rootfont=`echo $NAME | sed 's/[0-9]*$//'`
  	pointsize=`echo $NAME | sed 's/fc[a-z]*//'` 
	if test -e $FCDIR/make-fc; then
		if test -e $FCDIR"/base/src/b-"$rootfont".mf"; then
  		  $FCDIR/make-fc $rootfont $pointsize $pointsize >/dev/null
  		  chmod 644 $MFNAME >/dev/null
  		  echo $DESTDIR/$MFNAME
                  echo "$MFNAME created." >&2
		else
		  echo "ERROR: FC basefile for the font does not exist" >&2
		fi
	else
		echo "ERROR: Could not find make-fc" >&2
	fi
   else
	echo "ERROR: Could not find the FC font directory" >&2
   fi
else
  echo "ERROR: MakeTeXMF is not implemented for this font" >&2
fi
