#!/bin/sh
# InstallFont -- install a (Big 5 encoded) Chinese TrueType font as a set of
#                .tfm and .pk files. Further .pk files will be created by
#                MakeTeXPK.
#
#  Usage: InstallFont [-o] TrueTypefont resolution [resolution ...]
#
#  where option -o is to make only ordinary chinese "pk" fonts but also make
#  all "tfm" fonts
#
# The first parameter (after the option -o) is the name of the TrueType font 
# (without path and extension), the other parameters the wanted resolutions 
# (at least one  must be given).

alias gawk='awk'
SAVEPWD=`pwd` 

TTF2PKINPUTS=/usr/share/texmf
TTF2TFMINPUTS=/usr/share/texmf

export TTF2PKDIR
export TTF2TFMDIR



# The location of Chinese TrueType fonts
: ${ttfdir=/usr/share/texmf/fonts/chinese/ttf}

# The location of the web2c fonts directory tree
: ${fontdir=/usr/share/texmf/fonts}
 
if test $# -lt 2; then
  echo ""
  echo "  Usage: $0 [-o] TrueTypefont resolution [resolution ...]" >&2
  echo " "
  echo "  where option -o is to make only ordinary chinese \"pk\" fonts but also" >&2
  echo "  make all \"tfm\" fonts" >&2
  echo " "
  exit 1
fi

if test $# -eq 2; then
  if test "$1" = "-o"; then
  echo ""
  echo "  Usage: $0 [-o] TrueTypefont resolution [resolution ...]" >&2
  echo " "
  echo "  where option -o is to make only ordinary chinese \"pk\" fonts but also" >&2
  echo "  make all \"tfm\" fonts" >&2
  echo " "
  exit 1
  fi
fi

if test "$1" = "-o"; then
   ttfname=$2
   option="1"
   nn=24
else
   ttfname=$1
   option="0"
   nn=55
fi
 
test -d $ttfdir || \
  { echo "$0: $ttfdir does not exist." >&2; exit 1; }

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

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

# Clean up on normal or abnormal exit. DESTDIR changes, hence the eval.
trap "cd /; eval rm -rf $TEMPDIR \$DESTDIR/pktmp.$$" 0 1 2 15
 
#destdir=$fontdir/chinese/$ttfname
destdir=/usr/share/texmf/fonts/pk/chinese/$ttfname
desttfmdir=/usr/share/texmf/fonts/tfm/chinese/$ttfname

test -d $destdir \
  || mkdir -p $destdir \
  || { echo "$0: Could not mkdir $destdir." >&2; exit 1; }
test -d $desttfmdir \
  || mkdir -p $desttfmdir \
  || { echo "$0: Could not mkdir $desttfmdir." >&2; exit 1; }
 
export ttfdir
     ttfline=" -e Big5      "$ttfdir"/"$ttfname".ttf"
     dottfline="ttfline=\"$ttfline\""
     eval "$dottfline"

if test "$option" = "1"; then
   shift 2
else
   shift 1
fi

  if test -d /usr/share/texmf; then
     texmf="1"
     if test ! -s /usr/share/texmf/tex/chinese/chitex.fdf; then
         chitexfdf="nul"
         mkdir -p /usr/share/texmf/tex/chinese
     fi
     chinese="/usr/share/texmf/tex/chinese"
  fi

  if test -d /usr/local/lib/texmf; then
     texmf="2"
     if test ! -s /usr/local/lib/texmf/tex/chinese/chitex.fdf; then
         chitexfdf="nul"
         mkdir -p /usr/local/lib/texmf/tex/chinese
     fi
     chinese="/usr/local/lib/texmf/tex/chinese"
  fi


if test -s $chinese/chitex.fdf; then
   sed -n /\\chscale{/p $chinese/chitex.fdf > chscale1@.@@@
   sed 's/{/ /g' chscale1@.@@@ > chscale2@.@@@
   sed 's/}/ /g' chscale2@.@@@ > chscale1@.@@@
   sed 's/.*\\chscale//g' chscale1@.@@@ > chscale2@.@@@
fi


/bin/rm -f setvar@

for DPI do


if test -s chscale2@.@@@; then
gawk -v ttfname=$ttfname -v dpi=$DPI '
BEGIN{}
{if (ttfname == $1)
  {
  if (dpi <= 315)  
     {print dpi "/1000" "*" $2 "+0.5" > "setvar@"}
  fi
  if (dpi > 315 && dpi <= 345)   
     {print dpi "/1000" "*" $3 "+0.5" > "setvar@"}
  fi
  if (dpi > 345 && dpi <= 396)   
     {print dpi "/1000" "*" $4 "+0.5"  > "setvar@"}
  fi
  if (dpi > 396 && dpi <= 475)
     {print dpi "/1000" "*" $5 "+0.5" > "setvar@"}  
  fi
  if (dpi > 475 && dpi <= 632)   
     {print dpi "/1000" "*" $6 "+0.5" > "setvar@"}
  fi
  if (dpi > 632)   
     {print dpi "/1000" "*" $7 "+0.5" > "setvar@"}
  fi
  }
 fi}
END{}' chscale2@.@@@
fi

#echo "Here 0" >&2
#cat setvar@ >&2
#read xxyyzz 

/bin/rm -f chscale?@.@@@

if test -s setvar@; then 
   ddpi=`bc -l < setvar@`
   ddpi=`echo $ddpi | sed 's/\..*//'`
else
   ddpi=$DPI
fi


  i=1
  while [ $i -le 55 ]; do
    f=$i
    if [ $i -lt 10 ]; then
      f="0$i"
    fi

    
# eval "MakeTTFPK ${ttfname}$f ${ttfname}${f}.${ddpi}pk $f \"$ttfline\" $ddpi"

#    ttf2pk -q ${ttfname}$f  $ddpi

    if [ $i -le $nn ]; then

      if [ $i -eq 9 ]; then
          null="0"
#         ttf2pk -q ${ttfname}09  $ddpi    
      else
         echo "${ttfname}${f}.${DPI}pk " >&2
         ttf2pk -q ${ttfname}$f  $ddpi
         mv ${ttfname}${f}.${ddpi}pk $destdir
      fi

    fi
#    echo "${ttfname}${f}.tfm"

     i=`expr $i + 1`
  done

  /bin/rm -f setvar@  

         echo "${ttfname}09.${DPI}pk " >&2
         ttf2pk -q ${ttfname}09  $ddpi
         mv ${ttfname}09.${ddpi}pk $destdir


#echo "nn= $nn"

#  i=1
#  while [ $i -le $nn ]; do
#    f=$i
#    if [ $i -lt 10 ]; then
#      f="0$i"
#    fi
#     echo "${ttfname}${f}.${ddpi}pk "
#     mv ${ttfname}${f}.${ddpi}pk $destdir
#     mv ${ttfname}${f}.${ddpi}pk $SAVEPWD
#     i=`expr $i + 1`
#  done
#  rm *.${DPI}pk

#  mv *.tfm $desttfmdir
  echo ""
done


exit


 if test "$texmf" = "1"; then 
   if test ! "$chitexfdf" = "nul"; then
      null=""
      cp $chinese/chitex.fdf chitex.fdf.tmpp@
   fi
 fi

 if test "$texmf" = "2"; then 
   if test ! "$chitexfdf" = "nul"; then
      null=""
      cp $chinese/chitex.fdf chitex.fdf.tmpp@
   fi
 fi

if test -s fntname@; then
  /bin/rm -f fntname@
fi

if test -s prefnt@.bat; then
   /bin/rm  -f prefnt@ 
fi   


if test ! "$chitexfdf" = "nul"; then
gawk '
BEGIN{xx=0}
{
ll=index($0,"%")
if (ll > 0)
  {if (ll == 1 )
      {leftstr=""
       rightstr=$0}
   else
      {leftstr=substr($0,1,ll-1)
       rightstr=substr($0,ll,length($0))}
   fi
  }
else 
   {leftstr=$0
    rightstr=""}
fi
#print leftstr

if (leftstr != "")
  {if (xx == 0)
    {ll2=index(leftstr,"\\endinput")
     if (ll2 > 0)
       {xx=1}
     fi}
   fi
   
   if (xx == 1)    
    {print "%" $0 > "chitex.fdf.tmp@"}
   else
    {print $0 > "chitex.fdf.tmp@"} 
   fi
  }
else 
   {print $0 > "chitex.fdf.tmp@"} 
fi
}
END{}' chitex.fdf.tmpp@
fi


if test ! "$chitexfdf" = "nul"; then
gawk -v ttfname=$ttfname '
BEGIN{xx=0}
{
ll=index($0,"%")
if (ll > 0)
  {if (ll == 1 )
      {leftstr=""
       rightstr=$0}
   else
      {leftstr=substr($0,1,ll-1)
       rightstr=substr($0,ll,length($0))}
   fi
  }
else 
   {leftstr=$0
    rightstr=""}
fi

if (leftstr != "")
  {
   ll2=index(leftstr,"\\choosechfont{")
   if (ll2 > 0)
      {
       ll3=index(leftstr,"}")
       rightstr=substr(leftstr,ll3+1,length(leftstr)-ll3)
       ll5=index(rightstr,"{")+1
       ttfnameold=substr(rightstr,ll5,index(rightstr,"}")-ll5)

       if (ttfname == ttfnameold)
          {xx=1
           fontname=substr(leftstr,ll2+14,ll3-ll2-14)
           print fontname
           }
       fi
       }
   fi   
  }
fi     
}    
END{
if (xx == 0)
   {print "yes" > "fntname@"}
#   choosefnt="echo \"\\choosechfont\{"fontname"\}""\{"ttfname"\}\" >> chitex.fdf.tmp@" 
#   {print choosefnt > "appendtmp@"}
else
   {
   print "clear" > "prefnt@"
   print "echo \"    \" " > "prefnt@"
   print "echo \"    \" " > "prefnt@"
   print "echo \"    \" " > "prefnt@"
   print "echo \"     \
**********************************************************************  \" " > "prefnt@"
   print "echo \"     * The font \\\"" ttfname "*.pk\\\" has been named previously as " \
   fontname "             *\" " > "prefnt@"
   print "echo \"     * One can use \\" fontname " in TeX document to select the font \\\"" \
 ttfname "*.*pk\\\"  *\" "  > "prefnt@" 
   print "echo \"     \
**********************************************************************  \" " > "prefnt@"
   print "echo \"    \" " > "prefnt@"
   print "echo \"    \" " > "prefnt@"
   print "echo -n \"      Press enter key to continue\"" > "prefnt@"
   print "read diriveb" > "prefnt@"
    }
fi
}' chitex.fdf.tmp@
     
fi

texhash

exit

if test "$chitexfdf" = "nul" -o -s fntname@; then
inline="n"
while test "$inline" = "n" 
do
clear
echo "           "
echo "           "
echo "           "
echo " \
     *******************************************************************  "
echo "      *  The $ttfname*.${DPI}pk fonts have been intalled.                    *"
echo "      *  In the following, you should give a name for this chinese font.*" 
echo "      *  The name you give will be used latex in your TeX document.     *"
echo "      *  For example, if the font \"ntukai*.*pk\" (generated from         *"
echo "      *  truetype file \"ntukai.ttf\") has been installed and you give    *"
echo "      *  the name:                                                      *"                                                     
echo "      *                       kai                                       *"
echo "      *  for this font, then one can give the command \kai to select    *"
echo "      *  chinese font \"kai\" in one's TeX document.                      *"
echo " \
     *******************************************************************   "

echo "      "
echo "      Give a name for the font \"$ttfname\" (the name should contain only" 
echo -n "      english alphabetical characters): "

read fontname

inline="y"
echo "$fontname" > tmp@
/bin/rm -f tmpp@
gawk '
BEGIN{}
{ll=length($0)+1
 xx=1
 while (xx < ll) 
 {fstr=substr($0,xx,1)
 if (fstr !~ /[a-z A-Z]/) 
    {inlinea="echo \"n\" > tmpp@"
     system(inlinea)
     xx=ll}
 else
     xx=xx+1
 fi
 }
}
END{}' tmp@

#/bin/rm -f tmp@

if test -s tmpp@; then
   inline="n"
   echo "      "
   echo "      Error! The name you give for the font \"$ttfname\" is:"
   echo "                        $fontname     "
   echo "      It contains some non english alphabetical characters which is "
   echo "      illegal. Reenter the name again!"
   echo "    "
   echo -n "      Press enter key to continue"
   read drivea   
else
   echo "     "
   echo "      The name you give is: $fontname" 
   echo "     "
   echo -n "       Accpept this? (y/n)"
   read inline
fi

done

fi

#/bin/rm -f tmpp@

if test -s fntname@; then
   echo "\choosechfont{$fontname}{$ttfname}%" >> chitex.fdf.tmp@
 if test -s chscale2@.@@@; then
   gawk  -v ttfname=$ttfname -v fontname=$fontname '
   BEGIN{} 
   {if (ttfname == $1)
    {out= "echo \"\\chfntscale{" fontname "}{" $2 "}{" $3  "}{" $4 "}{" $5 "}{" $6  "}{" $7 "}%\" >> chitex.fdf.tmp@" 
#    print "Here " out
    system(out)}
   }
   END{}' chscale2@.@@@
 fi  

#   /bin/rm -f fntname@
fi

# read xxyyzz

if test -s prefnt@; then
   chmod +x prefnt@
   prefnt@
   /bin/rm -f prefnt@ 
fi   


if test "$chitexfdf" = "nul"; then

echo "%%%%%%%%%%%%%%%%%%" > chitex.fdf.tmp@

echo "% The following macro \choosechfont is a macro used to choose and associate " >> chitex.fdf.tmp@
echo "% chinese pk font files to a name (macro) which we could use in the TeX" >> chitex.fdf.tmp@         
echo "% document. The form of this macro is " >> chitex.fdf.tmp@                                          
echo "%              \choosechfont{$$$$$}{@@@} " >> chitex.fdf.tmp@
echo "%      means we choose chinese fonts files @@@01.pk, @@@02.pk,..., and " >> chitex.fdf.tmp@
echo "%      @@@01.tfm, @@@02.tfm,... and name the fonts we choose as $$$$$  " >> chitex.fdf.tmp@         
echo "%                                    " >> chitex.fdf.tmp@
echo "%      For example, the command      " >> chitex.fdf.tmp@                                           
echo "%            \choosechfont{kai}{ntukai}   " >> chitex.fdf.tmp@
echo "%      means we have chinese fonts files ntukai01.pk, ntukai02.pk,... and       " >> chitex.fdf.tmp@
echo "%      ntukai01.tfm, ntukai02.tfm,..., and the above command let us             " >> chitex.fdf.tmp@
echo "%      choose chinese fonts ntukai*.* and name it as "kai". Then we can give    " >> chitex.fdf.tmp@
echo "%      command                                                                  " >> chitex.fdf.tmp@
echo "%                    \kai                                                       " >> chitex.fdf.tmp@
echo "%      in our TeX document to mean we have chosen "kai" () as chinese       " >> chitex.fdf.tmp@
echo "%      font after this command.                                                 " >> chitex.fdf.tmp@
echo "                                                                                " >> chitex.fdf.tmp@


echo "% Edit and modify the following to meet your requirment. However, it no harm    " >> chitex.fdf.tmp@
echo "% to keep some lines unchanged if you do not really have that fonts files,      " >> chitex.fdf.tmp@
echo "% See ChiTeX.README.                                                            " >> chitex.fdf.tmp@
echo "                                                                                " >> chitex.fdf.tmp@
echo "%\choosechfont{kai}{ntukai}%                                                     " >> chitex.fdf.tmp@
echo "%\choosechfont{li}{ntuli}%                                                      " >> chitex.fdf.tmp@
echo "%\choosechfont{fs}{ntufs}%                                                      " >> chitex.fdf.tmp@
echo "%\choosechfont{ming}{ntumm}%                                                    " >> chitex.fdf.tmp@
echo "%\choosechfont{li}{ntuli}%                                                      " >> chitex.fdf.tmp@
echo "%\choosechfont{kaisu}{b5kai}%                                                   " >> chitex.fdf.tmp@
echo "                                                                                " >> chitex.fdf.tmp@

echo "% The following macro \choosefontadv is similar to the above macro              " >> chitex.fdf.tmp@
echo "% \choosechfont but have the third parameter to setup the default space         " >> chitex.fdf.tmp@
echo "% between chinese characters of the font that belongs to. Since different       " >> chitex.fdf.tmp@
echo "% fonts have different real metric property, we want this to eable having       " >> chitex.fdf.tmp@
echo "% uniform spaces that we want.                                                  " >> chitex.fdf.tmp@
echo "                                                                                " >> chitex.fdf.tmp@
echo "% The form of this macros is \choosechfontadv{kai}{ntukai}{n} where n is        " >> chitex.fdf.tmp@
echo "% a positive integer with   0 < n < 256. The effect of this is to have          " >> chitex.fdf.tmp@
echo "% spaces n x 0.02 pt. The default is n = 10.                                    " >> chitex.fdf.tmp@
echo "                                                                                " >> chitex.fdf.tmp@
echo "                                                                                " >> chitex.fdf.tmp@
echo "\wuming%             %This gives "wuming" as the default chinese font                      " >> chitex.fdf.tmp@
echo "\\choosechfont{$fontname}{$ttfname}%" >> chitex.fdf.tmp@
echo "                                                                                " >> chitex.fdf.tmp@

fi


if test "$texmf" = "1"; then
      mv chitex.fdf.tmp@ /usr/share/texmf/tex/chinese/chitex.fdf
fi

if test "$texmf" = "2"; then
   mv chitex.fdf.tmp@ /usr/local/lib/texmf/tex/chinese/chitex.fdf
fi

if test "$chitexfdf" = "nul" -o -s fntname@; then
   clear
   echo "    " 
   echo "    "
   echo "    "
   echo "    **********************************************************************"
   echo "    * The font \"$ttfname*.*pk\" has been named  as \"$fontname\"                    *" 
   echo "    * One can use \\$fontname in TeX document to select the font \
\"$ttfname*.*pk\"  *"
   echo "    **********************************************************************"
   echo "    " 
   echo "    "
   echo -n "      Press enter key to continue"
   read diriveb

fi

exit     
     
     
     
     
     
