#!/bin/sh
# 
# 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 [destdir]
#
#   `name' is the base name of the font, such as `cmr10'.
#   `destdir', if supplied, is either the absolute directory name to use
#     (if it starts with a /) or relative to the default DESTDIR (if not).

WORKDIR=`pwd` 

# TEMPDIR needs to be unique for each process because of the possibility
# of processes simultaneously running this script.
TEMPDIR=${TMPDIR-/tmp}/mttfm.$$

# 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

(
ttfyes="0"
# These search paths will be changed to include `pwd`. This is necessary
# since wo will cd to $TEMPDIR.
: ${KPSE_DOT=`pwd`}; export KPSE_DOT

# Go to the unique working directory.
test -d $TEMPDIR || mkdir $TEMPDIR 
cd $TEMPDIR || exit 1

progname=`basename $0`

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

NAME=`basename $1 .tfm`
MAG=1
DEST=$2

: ${TEXMF=`kpsetool -v '$TEXMF'`}
: ${MAKETEXDIR=$TEXMF/maketex}
export TEXMF MAKETEXDIR

# Possible local customizations?
test -r $MAKETEXDIR/maketex.site && . $MAKETEXDIR/maketex.site

MODE=${MT_DEF_MODE-ljfour}
BDPI=${MT_DEF_BDPI-600}
DPI=$BDPI


  gsfontsdir=/usr/lib/ghostscript/fonts
  fontmap=/usr/lib/ghostscript/Fontmap
  ttfdir=$TEXMF/fonts/chinese/ttf
  export ttfdir
  export gsfontsdir
  export fontmap

  ttfname=`echo $NAME|awk '{print substr($0,1,length($0)-2)}' -`
  plane=`echo $NAME|awk '{print substr($0,length($0)-1,2)}' -`


  echo "yes" > tmp@

  ttfnamenopbf=`
  gawk -v ttfname=$ttfname '
  BEGIN{}
  {if (index(ttfname,"_pfb") > 0) 
      {ttfnamenopbf=substr(ttfname,1,length(ttfname)-4)
       print ttfnamenopbf}
   else
      {print ttfname}
   }
  END{}' tmp@ `


  if test "$ttfname" != "$ttfnamenopbf"; then 
     pfbfile="y"
  fi

  ttfname=$ttfnamenopbf

  if test "$pfbfile" = "y"; then
     ttfnamepfb=$ttfname"_pfb"
  else
     ttfnamepfb=$ttfname
  fi

  if test -r $ttfdir/$ttfname.ttf; then
     ttfyes="1"; ext="ttf"
  else
     if test -r $ttfdir/$ttfname.ttc; then
        ttfyes="1"; ext="ttc"
     fi
  fi

  if test "$ttfyes" = "1"; then
     nameline=`sed -n "/$NAME/p" $TEXMF/fontname/special.map`
     if test "$nameline" = ""; then
       nul="1"
      sed "s/avmmv/$ttfnamepfb/g" $TEXMF/tex/chinese/special.add >> special.tmp
      chadd
      rm -f special.tmp
     fi
     TFMNAME=$NAME.tfm
     ff=`echo $NAME|sed -e 's/.*\([0-9][0-9]\)$/\1/'`

    if test "$GBENV" = "gbenv"; then
       cmd="ttf2tfm $ttfdir/$ttfname.$ext -q -P 3 -E 3 $ttfname@euc@"
    else
       cmd="ttf2tfm $ttfdir/$ttfname.$ext -q -P 3 -E 4 $ttfname@big5@"
    fi
  else
    cmd="mf \mode:=$MODE; mag:=$MAG; scrollmode; input $NAME"
  fi


set x `MakeTeXnames $NAME $DPI $MODE $DEST`
PKDEST=$2
TFMDEST=$3
PKDESTDIR=`echo $PKDEST | sed 's/^[^\/]*$/./; s/\/[^\/]*$//'`
TFMDESTDIR=`echo $TFMDEST | sed 's/^[^\/]*$/./; s/\/[^\/]*$//'`
PKNAME=`basename $PKDEST`
TFMNAME=$NAME.tfm
if test "$ttfyes" = "0"; then
GFNAME=$NAME.$DPI'gf'
fi

# Allow fonts to be read and written (especially in case we make
# directories) by everyone.  
umask 0

if test -r $TFMDESTDIR/$TFMNAME; then
  echo "$progname: $TFMDESTDIR/$TFMNAME already exists."
  echo $TFMDESTDIR/$TFMNAME >  $TEMPDIR/mtout.$$
  append_db $TFMDESTDIR $TFMNAME
  exit
fi

# Try to create the destdir first. Do not create fonts, if this fails.
MakeTeXmkdir "$TFMDESTDIR"
test -d "$TFMDESTDIR" ||
  { echo "$progname: could not mkdir $TFMDESTDIR."; exit 1; }

echo "$progname: Running $cmd"

if test "$ttfyes" = "1"; then
#  eval "$cmd > /dev/null"
  $cmd > /dev/null
  if test $? -eq 0; then
    :
  else
    echo "ttf2tm failed." >&2
    exit 1
  fi
else
  $cmd >/dev/null || { echo "$progname: \`$cmd' failed." >&2; exit 1; }
fi

# $cmd > /dev/null

if test "$ttfyes" = "1"; then
 if test "$pfbfile" = "y"; then
    mv -f $ttfname$plane.tfm $ttfnamepfb$plane.tfm
    if test "$GBENV" != "gbenv"; then
      ttf2pfb  -f $ttfnamepfb -c -plane $plane -o $ttfnamepfb$plane.fnt \
                            $ttfdir/$ttfname.$ext
    else
      ttf2pfb  -f $ttfnamepfb -pid 3 -eid 3 -c -plane $plane -o $ttfnamepfb$plane.fnt \
         $ttfdir/$ttfname.$ext
    fi
   t1asm -b $ttfnamepfb$plane.fnt $ttfnamepfb$plane.pfb
   echo "$ttfnamepfb$plane.pfb"

#   mv $ttfnamepfb$plane.pfb $gsfontsdir
   chmvpfb
   rm -f $ttfnamepfb$plane.fnt

   if test "$GBENV" != "gbenv"; then
      filestrno=58
   else
      filestrno=30
   fi
   lineinfontmap=`grep "/$ttfnamepfb$filestrno" $fontmap`

   rm -f fontmap@
   if test "$lineinfontmap" = ""; then
    echo "Modify file Fontmap ..."
    i=1
    while [ $i -le $filestrno ]; do
      f=$i
      if [ $i -lt 10 ]; then
         f="0$i"
      fi
      echo "/$ttfnamepfb$f     ($ttfnamepfb$f.pfb)   ;" >> fontmap@
     i=`expr $i + 1`
    done
    chaddpfbname      
   fi
 fi
fi

test -f $TFMNAME ||
  { echo "$progname: '$cmd' failed to make $TFMNAME."; exit 1; }

# Install the TFM file carefully, since others may be working simultaneously.

#echo "TFMNAME=$TFMNAME"
#exit


if test "$ttfyes" != "1"; then
mv $TFMNAME $TFMDESTDIR/tfmtmp.$$ \
  || { echo "$progname: Could not mv $TFMNAME $TFMDESTDIR/tfmtmp.$$."; exit 1; }

cd $TFMDESTDIR || exit 1
mv tfmtmp.$$ $TFMNAME
chmod 644 $TFMNAME
else

   if test "$pfbfile" = "y"; then
    if test ! -s $WORKDIR/pfbfile@; then
      echo "yes" > $WORKDIR/pfbfile@
    fi
   fi

   mv $ttfnamepfb.tfm "$TFMDESTDIR" 2>/dev/null \
     || cp $ttfname*.tfm "$TFMDESTDIR" || exit 1
   cd "$TFMDESTDIR" || exit 1
   chmod 644 $ttfname*.tfm
fi



# If this line (or an equivalent) is not present, dvipsk/xdvik/dviljk
# will think MakeTeXTFM failed.
echo $TFMDESTDIR/$TFMNAME >  $TEMPDIR/mtout.$$
append_db $TFMDESTDIR $TFMNAME

MakeTeXmkdir "$PKDESTDIR"
test -d "$PKDESTDIR" || exit 1
if test ! -f $PKDESTDIR/$PKNAME; then
  cd $TEMPDIR
 if test "$ttfyes" = "0"; then
  test -r $GFNAME ||
    { echo "$progname: Metafont failed to make $GFNAME."; exit 1; }
  gftopk ./$GFNAME $PKNAME || exit 1
#fi
  mv $PKNAME $PKDESTDIR/pktmp.$$
  cd $PKDESTDIR
  if test -f $PKNAME; then
    rm -f pktmp.$$
  else
    cd $PKDESTDIR
    mv pktmp.$$ $PKNAME
    chmod 644 $PKNAME
    append_db $PKDESTDIR $PKNAME
  fi
 fi
fi
) 1>&2 </dev/null
