#!/bin/sh

ptexsrc=`pwd`
texsrc="../kpathsea-2.6"

#--------------------------------------------------
cd $texsrc ; texsrc=`pwd`
if [ ! -d $texsrc ]; then
	echo "pTeXsetup: $texsrc directory is not found."
	exit
fi

case $1 in
    -euc)  kanji=EUC;  ikanji=EUC;  fkanji=SJIS ;;
    -sjis) kanji=SJIS; ikanji=SJIS; fkanji=EUC ;;
    -jis)  kanji=JIS;  ikanji=EUC;  fkanji=SJIS ;;
     *)    echo "    Usage: `basename $0` -euc|-sjis|-jis" ; exit ;;
esac

#--------------------------------------------------
# Copy, Patch and replace "output-kanji-code".
#--------------------------------------------------
if [ ! -d $texsrc/web2c/ptex ]; then
  cp -r $texsrc/web2c/tex $texsrc/web2c/ptex
  cp -r $ptexsrc/web2c $texsrc
fi

cd $texsrc
if [ ! -f ptex.patch ]; then
  cp $ptexsrc/ptex.patch $texsrc
  patch -p -s -b .org < ptex.patch
  cd $texsrc/web2c/web
  touch tangleboot.*
fi

cd $texsrc/web2c/lib
sed -e "s/output-kanji-code/$kanji/
	s/internal-kanji-code/$ikanji/" kanji.h.in > kanji.h

cd $texsrc/web2c/ptex
sed -e "/^#ifndef JPN/,/^#else JPN/d;
	/^#ifdef JPN/d; /^#endif JPN/d;
	/^#ifdef $fkanji/,/^#endif $fkanji/d;
	/^#ifdef $ikanji/d; /^#endif $ikanji/d;
	s/output-kanji-code/$kanji/" ptex.chg > tex.ch

cd $texsrc/web2c/fontutil
sed -e "s/output-kanji-code/$kanji/" pltotf.chg > pltotf.ch
sed -e "s/output-kanji-code/$kanji/" tftopl.chg > tftopl.ch

cd $ptexsrc
exit
#--- EOF -----------------------------------------------
