#!/bin/sh

TEMPDIR=${TMPDIR-/tmp}/mtmf.$$

# 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

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

# start of redirection stdout -> stderr, stdin <- /dev/null
(
progname=`basename $0`

DIR_PERMS=1777
FILE_PERMS=644

if test $# -lt 1 ; then
  echo "Usage: $progname name."
  exit 1
fi

if test ! -f "`kpsetool -w cnf texmf.cnf`"; then
  echo "$progname: global Kpathsea configurarion file texmf.cnf not found."
  echo
  echo "Please set the environment variable TETEXDIR or TEXMFCNF correctly."
  echo "For details see the teTeX and the Kpathsea manual"
  exit
fi

TEXMF=`kpsetool -v '$TEXMF'`
destdir="$TEXMF/fonts/source/public/dc"
ls_R="$TEXMF/ls-R"

NAME="`basename $1 .mf`"
rootname=`echo "$NAME" | sed 's/[0-9]*$//'`
pointsize=`echo "$NAME" | sed "s/^$rootname//"`

case "$NAME" in
  dc*) destdir="$TEXMF/fonts/source/public/dc";;
  tc*) destdir="$TEXMF/fonts/source/public/ts";;
  *)   exit;;
esac

test -d "$destdir" || exit 1
cd "$destdir" || exit 1

case "$pointsize" in
  "") echo "$progname: no pointsize."; exit 1;;
  11) realsize=10.95444 ;;	# \magstephalf
  14) realsize=14.4 ;;	# \magstep2
  17) realsize=17.28 ;;	# \magstep3
  20) realsize=20.74 ;;	# \magstep4
  25) realsize=24.88 ;;	# \magstep5
  ????) realsize=`echo "$pointsize" | sed 's/\(..\)/\1./'`;;
  *) realsize="$pointsize" ;;
esac

mfname="$NAME.mf"
if test -f "$mfname"; then
  echo "$destdir/$mfname already exists!"
  echo $destdir/$mfname >$TEMPDIR/mtout.$$
  append_db "$destdir" "$mfname"
  exit
fi

cat > "$mfname.tmp$$" <<EOF
if unknown dxbase: input dxbase fi;
gensize:=$realsize;
generate $rootname;
EOF

chmod "$FILE_PERMS" "$mfname.tmp$$"
rm -f "$mfname"
mv "$mfname.tmp$$" "$mfname"
append_db "$destdir" "$mfname"
echo "$destdir/$mfname" >$TEMPDIR/mtout.$$
) 1>&2 </dev/null
# end of redirection stdout, stdin
