#!/bin/sh
# original mktextfm -- make a new TFM file, because one wasn't found.
# 
# (If you change or delete the word `original' on the previous line,
# installation won't write this script over yours.)
# 
# te@informatik.uni-hannover.de, kb@mail.tug.org, and infovore@xs4all.nl.
# Public domain.

WORKDIR=`pwd` 
version='$Id: mktextfm,v 1.17 1998/02/16 08:56:13 olaf Exp $'
progname=`echo $0 | sed 's%.*/%%'`
usage="Usage: $progname [--destdir DESTDIR] FONT.

Makes a TFM file for FONT, if possible. Use DESTDIR for the root of where
to install into, either the absolute directory name to use (if it starts
with a /) or relative to the default DESTDIR (if not)."

# Handle non-positional options, except for --version/--help
while test $# -gt 0; do
  case "$1" in
    --destdir) shift; DEST="$1"; shift ;;
    --version|-version) break ;;
    --help|-help) break ;;
    *) break ;;
  esac
done

# Common code for all scripts.
: ${MT_TEXMFMAIN=`kpsewhich --expand-path='$TEXMFMAIN'`}
: ${MT_MKTEX_OPT=`kpsewhich --format='web2c files' mktex.opt`}
test -n "$MT_MKTEX_OPT" || MT_MKTEX_OPT="$MT_TEXMFMAIN/web2c/mktex.opt"
if test ! -f "$MT_MKTEX_OPT"; then
  echo "$progname: Cannot find mktex.opt; check your installation." >&2
  exit 1
fi

. "$MT_MKTEX_OPT"

# start of redirection stdout -> stderr, stdin <- /dev/null
(

ttfyes="0"

NAME=`basename "$1" .tfm`
MAG=1
#DEST="$2"
DPI=$BDPI


# Modify for ttf2tfm

  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@

#  gawk -v ttfname=$ttfname '

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

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

  ttfname=$ttfnamenopbf
  ttffile=`grep "$ttfname" $TEXMF/ttfonts.map|awk '{print $1 " " $2} '`
#  ttffile=`echo "$ttffile"|gawk -v ttfname=$ttfname '
  ttffile=`echo "$ttffile"|gawk '
  {atloc=index($1,"@"); beforeat=substr($1,1,atloc-1)
   if (beforeat == ttfname){print $2;exit}}' ttfname=$ttfname`

  export ttffile

  if test "$pfbfile" = "y"; then
     psfontsmap=$TEXMF/dvips/config/psfonts.map
     pdftexmap=$TEXMF/dvips/config/pdftex.map
     export psfontsmap
     export pdftexmap

     ttfnamepfb=$ttfname"_pfb"
  else
     ttfnamepfb=$ttfname
  fi

 if test "$ttffile" != ""; then
  if test -s $ttffile; then
    ttfyes="1"
  else
#    if test "$pfbfile" = "y"; then
      nottfedit=`which ttf_edit|grep ":"`
      if test "$nottfedit" = ""; then
        nonttfname=""
        nonttfname=`
#        gawk -v ttfname=$ttfname '
        gawk  '
        {if (substr(ttfname,1,1) == "n")
            {print substr(ttfname,2,length(ttfname)-1)}
        }' ttfname=$ttfname tmp@  `
        if test ! "$nonttfname" = ""; then
          nonttffile=`grep "$nonttfname" $TEXMF/ttfonts.map|gawk '{print $1 " " $2} '`
#          nonttffile=`echo "$nonttffile"|gawk -v nonttfname=$nonttfname '
          nonttffile=`echo "$nonttffile"|gawk  '
          {atloc=index($1,"@"); beforeat=substr($1,1,atloc-1)
          if (beforeat == nonttfname){print $2;exit}} ' nonttfname=$nonttfname `
#          nonttffilelower=`gawk -v nonttffile=$nonttffile '
          nonttffilelower=`gawk  '
          {print tolower(nonttffile)}' nonttffile=$nonttffile tmp@ `
   
          if test ! -s $nonttffile; then
             if test -s $nonttffilelower; then
                 nonttffile=$nonttffilelower
                 ttfyes="1"
             fi
          else
             ttfyes="1"
          fi

          if test "$ttfyes" = "1"; then
#             ttffiledir=`gawk -v ttffile=$ttffile '
             ttffiledir=`gawk  '
             {llen=length(ttffile);i=1
              for (i;i<=llen;i++)
                 {ch=substr(ttffile,llen-i+1,1)
                  if (ch == "/")
                    {print substr(ttffile,1,llen-i);break}
                 }
             }' ttffile=$ttffile tmp@ `
             if test ! -d $ttffiledir; then
                mkdir -p $ttffiledir
             fi
             genttf $nonttffile
             mv -f $ttfname".ttf" $ttffile
          fi
        fi           
      fi
#    fi
  fi
 fi


 if test "$ttfyes" = "1"; then
    if test "$VARTEXFONTS" = ""; then
      varfontsdir=$VARFONTS
    else
      varfontsdir=$VARTEXFONTS 
    fi
    gsfontsdir=$varfontsdir/type1/fonts
    export gsfontsdir

#  sfd=`
#  grep "$ttfname@" $TEXMF/ttfonts.map| \
#  gawk -v ttfname=$ttfname '

  sfd=`
  grep "$ttfname@" $TEXMF/ttfonts.map| \
  gawk  '
  BEGIN{FS="@"}{if ($1 == ttfname){print $2;exit}}END{}' ttfname=$ttfname `

  case "$sfd" in
  "UBig5") eid="1";;
    "UGB") eid="1";;
   "big5") eid="4";;
   "Big5") eid="4";;
    "euc") eid="3";;
    "EUC") eid="3";;
  esac
 fi


OIFS=$IFS; IFS=$SEP
set x `"$MT_MKTEXNAM" $NAME $DPI $MODE $DEST`; shift
IFS=$OIFS

PKDEST="$1"
TFMDEST="$2"
PKDESTDIR=`echo "$PKDEST" | sed 's%/[^/][^/]*$%%'` # can't rely on dirname
TFMDESTDIR=`echo "$TFMDEST" | sed 's%/[^/][^/]*$%%'`
PKNAME=`basename "$PKDEST"`
TFMNAME=$NAME.tfm
GFNAME=$NAME.$DPI'gf'

if test -r "$TFMDESTDIR/$TFMNAME"; then
  echo "$progname: $TFMDESTDIR/$TFMNAME already exists." >&2
  echo "$TFMDESTDIR/$TFMNAME" >$STDOUT
  "$MT_MKTEXUPD" "$TFMDESTDIR" $TFMNAME
  exit
fi

# Try to create the destdir first. Do not create fonts, if this fails.
"$MT_MKTEXDIR" "$TFMDESTDIR"
if test ! -d "$TFMDESTDIR"; then
  echo "$progname: mktexdir $TFMDESTDIR failed."
  exit 1
fi

# Modify for ttf2tfm

  if test "$ttfyes" = "1"; then
     nameline=`sed -n "/$NAME/p" $TEXMF/fontname/special.map`
     if [ "$nameline" = "" ]; then
      sed "s/avmmv/$ttfnamepfb/g" $TEXMF/tex/chinese/special.add >> special.tmp
#      chadd
      cat special.tmp >> $TEXMF/fontname/special.map
      rm -f special.tmp
     fi
     TFMNAME=$NAME.tfm

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

     cmd="ttf2tfm $ttffile -q -P 3 -E $eid $ttfname@$sfd@"
  else
    cmd="mf \mode:=$MODE; mag:=$MAG; scrollmode; input $NAME"
  fi


#cmd="mf \mode:=$MODE; mag:=$MAG; nonstopmode; input $NAME"
echo "$progname: Running $cmd"

if test "$ttfyes" = "1"; then
  eval "$cmd > /dev/null"
  if test $? -eq 0; then
    :
  else
    echo "ttf2tfm failed." >&2
    exit 1
  fi
else
 $cmd </dev/null || {
  grep '^!' $NAME.log >$$.errs 2>/dev/null
  grep '^! Strange path' $$.errs >$$.strange 2>/dev/null
  if cmp $$.errs $$.strange >/dev/null 2>&1 \
    && test -s $$.strange >/dev/null 2>&1; then
    echo "$progname: warning: \`$cmd' caused strange path errors." >&2
  else
    echo "$progname: \`$cmd' failed." >&2
    test -s $NAME.log && mv -f $NAME.log "$KPSE_DOT"
    exit 1;
  fi
}
fi


if test "$ttfyes" = "1"; then
 nottf2pfb=""
 if test "$pfbfile" = "y"; then
  mv -f $ttfname$plane.tfm $ttfnamepfb$plane.tfm
  nottf2pfb=`which ttf2pfb|grep ":"`
  if test "$nottf2pfb" = ""; then
    if test "$GBENV" != "gbenv"; then
      ttf2pfb  -f $ttfnamepfb -pid 3 -eid 4 -c -plane $plane \
        -o $ttfnamepfb$plane.fnt $ttffile
    else
      ttf2pfb  -f $ttfnamepfb -pid 3 -eid 3 -c -plane $plane -o \
         $ttfnamepfb$plane.fnt $ttffile
    fi
   t1asm -b $ttfnamepfb$plane.fnt $ttfnamepfb$plane.pfb 2> /dev/null 
   echo "$ttfnamepfb$plane.pfb"

#   mv $ttfnamepfb$plane.pfb $gsfontsdir
   if test ! -d $gsfontsdir; then
      mkdir -p $gsfontsdir
   fi
#   chmvpfb
   chmod go-w *_pfb??.pfb
   mv -f *_pfb??.pfb $gsfontsdir/
   rm -f $ttfnamepfb$plane.fnt

   if test "$GBENV" != "gbenv"; then
      filestrno=58
   else
      filestrno=35
   fi

   lineinfontmap=`grep "$ttfnamepfb$filestrno" $psfontsmap`
   lineinfontmapa=`grep "$ttfnamepfb$filestrno" $pdftexmap`

   rm -f psfontsmap@
   if test "$lineinfontmap" = "" -o "$lineinfontmapa" = ""; then

    echo "Modify file psfonts.map, pdftex.map  ..."
    i=1
    while [ $i -le $filestrno ]; do
      f=$i
      if [ $i -lt 10 ]; then
         f="0$i"
      fi
      if test "$i" = "1"; then 
         echo " " >> psfontsmap@
      fi 
      echo "$ttfnamepfb$f  $ttfnamepfb$f   <$ttfnamepfb$f.pfb" >> psfontsmap@
     i=`expr $i + 1`
    done
#    chaddpfbname      
    cat psfontsmap@ >> $psfontsmap 
    cat psfontsmap@ >> $pdftexmap
   fi
  fi
 fi
fi


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

# Install the TFM file carefully, since others may be working simultaneously.
# Use cp when mv fails, since DOS will fail mv for deeply-nested directories.

if test "$ttfyes" != "1"; then
  mv $TFMNAME "$TFMDESTDIR/tfm$$.tmp" 2>/dev/null \
     || cp $TFMNAME "$TFMDESTDIR/tfm$$.tmp" || exit 1
  cd "$TFMDESTDIR" || exit 1
  chmod `kpsestat -xst,go-w .` tfm$$.tmp
  test -r $TFMNAME || mv tfm$$.tmp $TFMNAME || exit 1
else

#   mv $ttfname*.tfm "$TFMDESTDIR" 2>/dev/null \
#     || cp $ttfname*.tfm "$TFMDESTDIR" || exit 1

   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 `kpsestat -xst,go-w .` $ttfname*.tfm
fi

# OK, success with the TFM.
"$MT_MKTEXUPD" "$TFMDESTDIR" $TFMNAME
echo "$TFMDESTDIR/$TFMNAME" >$STDOUT
echo "$progname: $TFMDESTDIR/$TFMNAME: successfully generated." >&2

# Since we probably made a GF(->PK) file, too, may as well install it if
# it's needed.
cd $TEMPDIR
if test "$ttfyes" = "0"; then
if test -r $GFNAME && test ! -f "$PKDESTDIR/$PKNAME"; then
  gftopk ./$GFNAME $PKNAME || exit 1
  "$MT_MKTEXDIR" "$PKDESTDIR"
  mv $PKNAME "$PKDESTDIR/pk$$.tmp" 2>/dev/null \
    || cp $PKNAME "$PKDESTDIR/pk$$.tmp" || exit 1
  cd "$PKDESTDIR" || exit 1
  if test -f $PKNAME; then
    rm -f pk$$.tmp
  else
    chmod `kpsestat -xst,go-w .` pk$$.tmp
    mv pk$$.tmp $PKNAME
    "$MT_MKTEXUPD" "$PKDESTDIR" $PKNAME
  fi
fi
fi
) 1>&2 </dev/null

