#!/bin/sh
export TEXMF=/usr/share/texmf
export TTF2PKINPUTS=.:$TEXMF//
export TTF2TFMINPUTS=.:$TEXMF//
export PATH=/usr/share/texmf/chitex:/bin:/usr/bin:.
# By Hung-Yih Chen    
#           Last updated  Sep. 9, 1998
# This script is used to compile chinese (Big5)
# plain TeX source file
#
# Usage: chitex sourcefile
#
# where sourcefile is a plain TeX source filename
# Example:
#         chilatex math1   (or chilatex math1.tex)
# when you have math1.tex 
#

export VARTEXFONTS=/var/lib/texmf



fileno=1
export fileno
if test ! -d .tmp00; then
   mkdir .tmp00
fi


PWD=`pwd`  
/bin/rm -f *temp@ maintemp*@ maintmp*@ fileb@ *.@@@ *.temp$ *.temp$ trasl*@
/bin/rm -f addmac*@ filemap@ trancode@ *.tmp00

if test ! -s $1.tex
then
 if test ! -s $1; then 
   filename=`find /usr/share/texmf/tex -name $1 -print`
   if test "$filename" = ""; then
      filename=`find /usr/share/texmf/tex -name $1.tex -print`
      if test "$filename" = ""; then
         exit
      fi
   fi
 else
   filename=$1
 fi
else 
 filename=$1.tex
fi


  bname=`basename $filename`                    
  filea=`echo $bname|cut -d. -f 1` 
  fileb=`echo $bname |cut -d. -f 2`
  export filea=$filea

echo $filename > texfile@

chitranp $1


cut -d% -f 1 $filea.tmp00|grep "\\\end" > tmp@
cut -d% -f 1 $filea.tmp00|grep "\\\bye" > tmp@@

end=`
gawk '
BEGIN{}
{
 end=$0
 if (end !="")
 {
  gsub("\t"," ",end)
  endloc=index(end,"\134end")
  endnext=substr(end,endloc+4,1)
  haveend=0
  if (end !="")
     {if (endnext == "
"||endnext == " "|| endnext == "\134"||endnext == "")
         {haveend=1}
     }
  if (haveend == 1)
     {print "end"}
  else
     {print ""}
 }
 else
     {print ""}
}
END{} ' tmp@ `

bye=`
gawk '
BEGIN{}
{
 bye=$0
 if (bye != "")
 {lenn=length(bye)
  gsub("\t"," ",bye)
  byeloc=index(bye,"\134bye")
  byenext=substr(bye,byeloc+4,1)
  havebye=0
  if (bye !="")
     {if (byenext == "
"||byenext == " "|| byenext == "\134"\
          ||byenext == "")
         {havebye=1}
     }
  if (havebye == 1)
     {print "bye"}
  else
     {print ""}
 }
 else
     {print ""}
}
END{} ' tmp@@ `

if [ "$end" = "" -a "$bye" = "" ]; then
   echo "\\bye" >> $filea.tmp00
   echo -e " \a "
   echo " "
   echo " "
   echo "Warning! Warning! "
   echo "Missing command \\end or \\bye "
   echo "Now ChiTeX insert it to your document temporarily. "
   echo "    "
   echo -e "* Press any key to continue\c"
   read inkey
fi



/bin/rm -f *temp@ maintemp*@ maintmp*@ fileb@ *.@@@ 


virtex \&chitex $filea.tmp00|ctran


#echo "****************************************               "
#echo "*****  ChiTeX version 6.0.8 - by                         "
#echo "*****     (Hung-Yih Chen)  yih@math.ncu.edu.tw)   "
#echo "*****  ߤjǼƾǨt (Dept. of Math., National Central Univ.)"
##echo "*****  (Dept. of Math., National Central Univ.)          "
#echo "****************************************               "

/bin/rm -f tmp@@ *.tmp@* *.temp$ *.tmp!* 
/bin/rm -f transl@*  addmac*@ *.tmp0*  *@
/bin/rm -rf .tmp00

exit


