#!/bin/sh
#
# This file is part of the CJK macro package ver. 4.1.3 (20-Jun-1997).
#
# InstallFont -- install a CJK TrueType font as a set of tfm and .pk files.
#                Further .pk files will be created by MakeTeXPK.
#
# IT'S NOT NECESSARY TO USE THIS SCRIPT WITH teTeX! THE teTeX SCRIPT FILES
# PROVIDED BY THE CJK PACKAGE CAN CREATE TFM FILES ON THE FLY TOO.
#
# The first parameter is the TeX name of the TrueType font as given in
# ttf2pk.cfg, the second is the language the font belongs to (e.g. chinese;
# this parameter will subdivide the directory tree further), the third
# parameter the encoding of the font. The other parameters are the wanted
# resolutions (at least one must be given).
 
# The location of ttf2pk configuration file
: ${TTF2PKDIR=/dos/texmf/ttf2pk}

# The location of the web2c fonts directory tree
: ${fontdir=/usr/local/lib/texmf/fonts}
 
if test $# -lt 4; then
  echo ""
  echo "Usage: $0 TrueTypefont language encoding resolution [resolution ...]" >&2
  echo ""
  echo "    valid encoding values are:"
  echo "      Big5, EUC (for KS, GB etc.), SJIS, and Unicode."
  echo ""

  exit 1
fi
 
ttfname=$1
lang=$2
enc=`echo $3 | sed -e 's/^\(.\).*/\1/'`

if test "$enc" = "B" || test "$enc" = "b"; then
  nmb_subfonts=55
elif test "$enc" = "E" || test "$enc" = "e" || \
     test "$enc" = "S" || test "$enc" = "s"; then
  nmb_subfonts=35
elif test "$enc" = "U" || test "$enc" = "u"; then
  nmb_subfonts=256
else
  echo "Invalid encoding."
  echo "Valid values are Big5, EUC, SJIS, and Unicode."

  exit 1
fi
 
test -f $TTF2PKDIR/ttf2pk.cfg || \
  { echo "$0: $TTF2PKDIR/ttf2pk.cfg does not exist." >&2; exit 1; }
 
destdir=$fontdir/$lang/$ttfname

test -d $destdir/pk/modeless \
  || mkdir -p $destdir/pk/modeless \
  || { echo "$0: Could not mkdir $destdir/pk/modeless." >&2; exit 1; }
test -d $destdir/tfm \
  || mkdir -p $destdir/tfm \
  || { echo "$0: Could not mkdir $destdir/tfm." >&2; exit 1; }
 
shift
shift
shift

pattern='^'`echo $ttfname | sed -e 's/[0-9][0-9]$//'`
ttfline=`egrep "$pattern" $TTF2PKDIR/ttf2pk.cfg | sed -e 's/^.*://'`
dottfline="ttfline=\"$ttfline\""
eval "$dottfline"


for DPI do
  i=1
  while test $i -le $nmb_subfonts; do
    if test $nmb_subfonts -eq 256; then
      f=`echo $i | awk '{ printf "%02x", $1-1; }'`
    else
      f=$i
      if test $i -lt 10; then
        f="0$i"
      fi
    fi

    echo "./MakeTTFPK ${ttfname}$f ${ttfname}${f}.${DPI}pk $f \"$ttfline\" $DPI"
    eval "./MakeTTFPK ${ttfname}$f ${ttfname}${f}.${DPI}pk $f \"$ttfline\" $DPI"

    i=`expr $i + 1`
  done

  mv *.${DPI}pk $destdir/pk/modeless
  mv *.tfm $destdir/tfm
  echo ""
done
