#!/bin/sh
PATH=/bin:/usr/bin:/usr/local/bin

TEXMFMAIN=`kpsexpand '$TEXMFMAIN'`

# Some mktexpk incorrectly calls ttf2pk with -p option, delete it.
if [ ! -z "`grep "ttf2pk -p" ${PREFIX}/bin/mktexpk`" ]
then
  cp ${PREFIX}/bin/mktexpk ${PREFIX}/bin/mktexpk.CJK
  sed -e "s/ttf2pk -p/ttf2pk/" ${PREFIX}/bin/mktexpk.CJK > ${PREFIX}/bin/mktexpk
  rm ${PREFIX}/bin/mktexpk.CJK
fi

# Install ttf2pk binary in ${PORTSDIR}/print/freetype, data in ${TEXMF}/ttf2pk,
# and link ${TEXMF}/ttf2tfm to ${TEXMF}/ttf2pk.
(cd ${PORTSDIR}/print/freetype/work/freetype-1.3.1/contrib/ttf2pk;
 ./configure --prefix=${PREFIX} --with-kpathsea-dir=${PREFIX};
 make depend all install;
 rm -fr ${TEXMFMAIN}/ttf2pk ${TEXMFMAIN}/ttf2tfm;
 cp -R data ${TEXMFMAIN}/ttf2pk;
 cd ${TEXMFMAIN}; )

# fontname/special.map: add arb5kai/arb5sung/argbkai/argbsung entries.
if [ -n "`grep arb5kai ${TEXMFMAIN}/fontname/special.map`" ]
then
  echo Seems arb5kai already in special.map, file untouched.
else
  echo "@c Arphic BIG5 Kaiti TTF" >> ${TEXMFMAIN}/fontname/special.map
  echo "arb5kai		big5		arb5kai" >> ${TEXMFMAIN}/fontname/special.map
fi
if [ -n "`grep arb5sung ${TEXMFMAIN}/fontname/special.map`" ]
then
  echo Seems arb5sung already in special.map, file untouched.
else
  echo "@c Arphic BIG5 Mingti TTF" >> ${TEXMFMAIN}/fontname/special.map
  echo "arb5sung		big5		arb5sung" >> ${TEXMFMAIN}/fontname/special.map
fi
if [ -n "`grep argbkai ${TEXMFMAIN}/fontname/special.map`" ]
then
  echo Seems argbkai already in special.map, file untouched.
else
  echo "@c Arphic GB Kaiti TTF" >> ${TEXMFMAIN}/fontname/special.map
  echo "argbkai		gb		argbkai" >> ${TEXMFMAIN}/fontname/special.map
fi
if [ -n "`grep argbsung ${TEXMFMAIN}/fontname/special.map`" ]
then
  echo Seems argbsung already in special.map, file untouched.
else
  echo "@c Arphic GB Sungti TTF" >> ${TEXMFMAIN}/fontname/special.map
  echo "argbsung		gb		argbsung" >> ${TEXMFMAIN}/fontname/special.map
fi

# ttf2pk/ttfonts.map: add arb5kai/arb5sung/argbkai/argbsung entries.
if [ -n "`grep arb5kai ${TEXMFMAIN}/ttf2pk/ttfonts.map`" ]
then
  echo Seems arb5kai already in ttfonts.map, file untouched.
else
  echo "arb5kai@UBig5@   arb5_kai Pid=3 Eid=1" >> ${TEXMFMAIN}/ttf2pk/ttfonts.map
fi
if [ -n "`grep arb5sung ${TEXMFMAIN}/ttf2pk/ttfonts.map`" ]
then
  echo Seems arb5sung already in ttfonts.map, file untouched.
else
  echo "arb5sung@UBig5@   arb5_sung Pid=3 Eid=1" >> ${TEXMFMAIN}/ttf2pk/ttfonts.map
fi
if [ -n "`grep argbkai ${TEXMFMAIN}/ttf2pk/ttfonts.map`" ]
then
  echo Seems argbkai already in ttfonts.map, file untouched.
else
  echo "argbkai@UGB@   argb_kai Pid=3 Eid=1" >> ${TEXMFMAIN}/ttf2pk/ttfonts.map
fi
if [ -n "`grep argbsung ${TEXMFMAIN}/ttf2pk/ttfonts.map`" ]
then
  echo Seems argbsung already in ttfonts.map, file untouched.
else
  echo "argbsung@UGB@   argb_sung Pid=3 Eid=1" >> ${TEXMFMAIN}/ttf2pk/ttfonts.map
fi

# ttf2pk/UBig5.sfd: apply patch to support Eten's extention
(cd ${TEXMFMAIN}/ttf2pk;
 patch < ${FILESDIR}/UBig5.sfd.diff;
 rm -f UBig5.sfd.orig; )

# web2c/texmf.cnf
if [ -n "`grep TTF2PKINPUTS ${TEXMFMAIN}/web2c/texmf.cnf`" ]
then
  echo Seems TTF2PKINPUTS already set in texmf.cnf, file untouched.
else
  echo "% ttf2pk data directory" >> ${TEXMFMAIN}/web2c/texmf.cnf
  echo "TTF2PKINPUTS = \$TEXMF/ttf2pk//" >> ${TEXMFMAIN}/web2c/texmf.cnf
fi
if [ -n "`grep TTF2TFMINPUTS ${TEXMFMAIN}/web2c/texmf.cnf`" ]
then
  echo Seems TTF2TFMINPUTS already set in texmf.cnf, file untouched.
else
  echo "% ttf2tfm data directory" >> ${TEXMFMAIN}/web2c/texmf.cnf
  echo "TTF2TFMINPUTS = \$TEXMF/ttf2tfm//" >> ${TEXMFMAIN}/web2c/texmf.cnf
fi

# Clean redundant files created during patch phase.
rm -f ${TEXMFMAIN}/tex/latex/CJK/Bg5/c00kai.fd.orig
rm -f ${TEXMFMAIN}/tex/latex/CJK/Bg5/c00song.fd.orig
rm -f ${TEXMFMAIN}/tex/latex/CJK/GB/c10kai.fd.orig
rm -f ${TEXMFMAIN}/tex/latex/CJK/GB/c10song.fd.orig

# Generate tfm fonts for Arphic TTFs.
rm -fr ${TEXMFMAIN}/fonts/tfm/arphic/arb5kai
rm -fr ${TEXMFMAIN}/fonts/tfm/arphic/arb5sung
rm -fr ${TEXMFMAIN}/fonts/tfm/arphic/argbkai
rm -fr ${TEXMFMAIN}/fonts/tfm/arphic/argbsung
mkdir -p ${TEXMFMAIN}/fonts/tfm/arphic/arb5kai ${TEXMFMAIN}/fonts/tfm/arphic/arb5sung ${TEXMFMAIN}/fonts/tfm/arphic/argbkai ${TEXMFMAIN}/fonts/tfm/arphic/argbsung
(cd ${TEXMFMAIN}/fonts/tfm/arphic/arb5kai; ttf2tfm ${TEXMFMAIN}/fonts/truetype/arphic/arb5_kai -P 3 -E 1 arb5kai@${TEXMFMAIN}/ttf2pk/UBig5@)
(cd ${TEXMFMAIN}/fonts/tfm/arphic/arb5sung; ttf2tfm ${TEXMFMAIN}/fonts/truetype/arphic/arb5_sung -P 3 -E 1 arb5sung@${TEXMFMAIN}/ttf2pk/UBig5@)
(cd ${TEXMFMAIN}/fonts/tfm/arphic/argbkai; ttf2tfm ${TEXMFMAIN}/fonts/truetype/arphic/argb_kai -P 3 -E 1 argbkai@${TEXMFMAIN}/ttf2pk/UGB@)
(cd ${TEXMFMAIN}/fonts/tfm/arphic/argbsung; ttf2tfm ${TEXMFMAIN}/fonts/truetype/arphic/argb_sung -P 3 -E 1 argbsung@${TEXMFMAIN}/ttf2pk/UGB@)

# Update ls-R
texconfig rehash

# Messages to the user
echo "-------------------------------------------------------------------------"
echo "CJK is now installed. You may use bg5latex or gbklatex wrt"
echo "BIG5 or GB encodings."
echo
echo If you want to install other TTF fonts, you have to manually edit:
echo  ${TEXMFMAIN}/fontname/special.map
echo  ${TEXMFMAIN}/ttf2pk/ttfonts.map
echo  ${TEXMFMAIN}/web2c/texmf.cnf
echo  ${TEXMFMAIN}/tex/latex/CJK/Bg5/c00kai.fd and
echo   ${TEXMFMAIN}/tex/latex/CJK/GB/c10kai.fd, for Kai family fonts.
echo  ${TEXMFMAIN}/tex/latex/CJK/Bg5/c00song.fd and
echo   ${TEXMFMAIN}/tex/latex/CJK/GB/c10song.fd, for Sung family fonts.
echo Then use ttf2tfm to generate corresponding tfm fonts.
echo
echo "Happy CJKing!"
echo "-------------------------------------------------------------------------"
