#!/bin/sh

  if test "$#" = "0" -o "$1" = "--help";then
      option="help"
  fi
  if test "$option" = "help"; then
   clear
   echo " "; echo " "
   echo " Usage: genttf inputfile "
   echo " "
   echo "        where inputfile is a unicoded TrueType file of "
   echo "           幩r, uwcxmf.ttf and bitstream's Cyberbit.ttf "
   echo "        and the outputfile will be a Big5 encoeded or GB encoded"
   echo "        TrueType file"
   echo "        name as the basename of inputfile but preceded with "
   echo "        character 'n'"
   echo " "
   echo " Example: give commmand"
   echo "              genttf /f/fonts/bsmi00lp.ttf  "
   echo "          will make a Big5 encoded Truetype file: "
   echo "              ./nbsmi00lp.ttf "
#   echo " Remark: Appeared error mesaage 'invlid checksum ...' may be neglected." 
   echo " "
   exit
  fi

gawkbin="gawk"

echo "yes" > tmp@
texmfcnffile=`kpsexpand -w cnf texmf.cnf`
TEXMFCNF=`
$gawkbin  '
{texmfcnfloc=index(texmfcnffile,"/texmf.cnf")
 print substr(texmfcnffile,1,texmfcnfloc-1)
}' texmfcnffile=$texmfcnffile tmp@ `

echo $TEXMFCNF > tmp@
TEXMF=`
$gawkbin '
{
  inline=$0
  gsub(/web2c/,"",inline)
  inline=substr(inline,1,length(inline)-1)
  print inline
}' tmp@ `


ttfname=`basename $1|cut -d. -f 1`                    

afmno="34.afm"
encodeno="3 4"
afmname="bkai34.afm"

if test "$ttfname" != "cyberbit" -a "$ttfname" != "Cyberbit" \
   -a "$ttfname" != "uwcxmf";  then
   ttfnametmp=$ttfname

 if test "$GBENV" != ""; then
    afmno="33.afm"
    encodeno="3 3"
    afmname="gkai33.afm"
 fi
 cp $TEXMF/tex/chinese/$afmname $ttfnametmp$afmno
else
 if test "$ttfname" = "uwcxmf"; then
   ttfnametmp="uwcxmf"
   afmname="uwcxmf34.afm"
 else
   ttfnametmp="Cyberbit"
   afmname="cyber34.afm"
 fi
   cp $TEXMF/tex/chinese/$afmname $ttfnametmp"34.afm"
fi


echo "make n"$ttfnametmp".ttf"
echo "Wait ..."
 ttf_edit force $1 font $ttfnametmp$afmno $encodeno encode "n"$ttfnametmp".ttf" \
   gen 2>/dev/null

rm -f $ttfnametmp$afmno

exit