#!/bin/sh 
#
#	newfont ver 1.0
#
#	A shell script for creating new fonts in proper directories
#
#	Usage newfont [-o] <[-p] [-s scale] fontname><...><...>
#
#	-o 	: overwrite pk and tfm files if they exist!
#	-p	: use plain mf
#	-s sc	: sc is the scaling factor*1000 e.g. 1000 for standard 
#		  size
#	fontname: just the name of the font to create
#	Copyright (C) 1992 by K J Dryllerakis
#
#	You are allowed to use or modify this shell script as
#	long as you clearly state the original author and
#	you do not charge money for its use or distribution
#
#	Variables to be set for the local system
# ========================================================================
BASEDIR=/usr/local/lptex
BINDIR=$BASEDIR/bin
FONTDIR=$BASEDIR/fonts
TMPDIR=/tmp/newfont.$$
MFPLAIN=mf
CMMF=cmmf
PRINTERDPI=300
TFMPATH=$FONTDIR/tfm
PKPATH=$FONTDIR/pk
# =======================================================================
OVERWR=0
MFBIN=$CMMF
SCALED=1000
CANWRITE=0

umask 002


#	Check typing
if [ $# = 0 ]
then
	usage ; 
fi
#	Check if we can write to the output directories
if [ -w $TFMPATH -a -w $PKPATH ]
then
	CANWRITE=1
fi
if [ "$1" = "-o" -a $# -ne 1 ]; then
	OVERWR=1
	shift
fi
# Parse arguments
while [ $# -ne 0 ]
do
	if [ "$1" = "-p" ]; then
		MFBIN=$MFPLAIN
		shift
	fi
	if [ "$1" = "-s" -a $# -gt 2 ]; then
		SCALED=$2
		shift
		shift
	fi
	if [ $# -eq 0 ] ; then
		echo "$0: No font name specified exciting..."
		exit 1
	fi

	SIZE=`echo 300 $SCALED \* 1000 / p | dc`
	MFMAG=`echo 5 k $SIZE 300 / p | dc`
	FONT=$1
	shift
	PKFILE=$PKPATH/$SIZE/$FONT.pk
	TFMFILE=$TFMPATH/$FONT.tfm
	HERE=`pwd`

	if [ \( ! \( -f $PKFILE \) \) -o $OVERWR -eq 1  ] ; then
		if [  $CANWRITE -ne 1 ] ;  then
			echo "Insuffient write permition. \
				Only running MF for test"
		fi
		if [ ! -d $TMPDIR ] ; then 
			mkdir $TMPDIR
		fi
		MFINPUTS=$HERE:$MFINPUTS; export MFINPUTS
		cd $TMPDIR
		echo $MFBIN \\mag:=$MFMAG\; \\mode:=localfont \; input $FONT
		$BINDIR/$MFBIN \\relax\; \\mag:=$MFMAG\; \\mode:=localfont \; input $FONT
		GFFILE=$TMPDIR/$FONT.${SIZE}gf
		if [ -f $TMPDIR/$FONT.${SIZE}gf ] ; then
			if [ $CANWRITE -eq 1 ]; then
				if [ ! -d $TFMPATH ]; then
					mkdir $TFMPATH
				fi
				if [ \( !  -f $TFMFILE  \) -o $OVERWR -eq 1 ]
				then
					mv $TMPDIR/$FONT.tfm $TFMFILE
				fi
				if [ ! -d $PKPATH ] ; then mkdir $PKDIR ; fi
				if [ ! -d $PKPATH/$SIZE ] ; then 
					mkdir $PKPATH/$SIZE
				fi
				$BINDIR/gftopk $GFFILE $PKFILE
			fi
		else
			echo "metafont failed. I can't create the font!"
		fi
	fi
done

rm -rf $TMPDIR
usage()
{
	echo "usage: $0 <-p [-s scale]  font_name> ..."
	exit 1
}