#!/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  Nov. 25, 1998
#
# This script is used to compile chinese (Big5)
# LaTeX source file
#
# Usage: chilatex [-option] sourcefile
#
# where sourcefile is a LaTeX source filename# Example:
#         chilatex math2   (or chilatex math2.tex)
# when you have math2.tex 
#

option="0"; opno="0"

if test "$1" = "-e"; then
   option="e"
   opno="1"
else
   if test "$1" = "-batch"; then
        option="batch"
        opno="1"
   else
      if test "$1" = "-nonstop"; then
         option="nonstop"
         opno="1"
      else
         if test "$1" = "-help"; then
            option="help"
            opno="1"
         else
            if test "$1" = "-c"; then    
               option="0"
               opno="1"
            fi
         fi
      fi
   fi
fi


if [ "$#" = "0" -o "$opno" = "1" -a "$#" = "1" ]; then
   option="help"
fi

#if [ "$opno" = "1" -a "$#" = "1" ]; then
#   option="help"
#fi


if test "$option" = "help"; then
   echo " "; echo " "
   echo "  Usage: chilatex [option] sourcefile "
   echo " "
   echo "  where  sourcefile is a LaTeX source file"
   echo "  option are:"
   echo "  -e       with error message and .log file non-chineses"
   echo "  -c       with chinese error message and chinese .log file"
   echo "           default is with option -c"
   echo "  -batch   with chinese .log file generated at run time"
   echo "           but without prompt error message which is only suitable"
   echo "           for compiling in 'batchmode' or in 'nonstopmode' and"
   echo "           could spend less time."
   echo "           There is a command 'chilatexb' which is equivalent to"
   echo "           'chilatex -batch' and is used for 'lyx'."
   echo "  -nostop  same as '-batch'"
   echo "  -help    help"
   echo " "
   exit
fi

if test $# -gt 1; then
if test "$2" = "-e"; then
   option="e"
else
   if test "$2" = "-batch"; then
      option="batch"
   else
      if test "$2" = "-nonstop"; then
         option="nonstop"
      else 
         if test "$2" = "-c"; then    
            option="0"
         fi
      fi
   fi
fi
fi


if [ "$1" = "-e" -o "$1" = "-batch" -o "$1" = "-nonstop" -o "$1" = "-c" ] ; then
   shift 1
fi




fileno=1
export fileno
inputno=0
export inputno
labelcount=0
export labelcount
bitemcount=0
export bitemcount

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 ch@export* ch@label*

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@

#echo $1 > stemp@
#chitranx

chitran $1

enddoc=`cut -d% -f 1 $filea.tmp00|grep "\\end{document}"`
enddocsp=`cut -d% -f 1 $filea.tmp00|grep "\\end {document}"`
if [ "$enddoc" = "" -a "$enddocsp" = "" ]; then
   echo "\\end{document}" >> $filea.tmp00
   echo -e " \a "
   echo " "
   echo " "
   echo "Warning! Warning! "
   echo "Missing command \\end{document} "
   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@ *.@@@ 



if [ "$option" = "e" ]; then
   virtex \&chitexl $filea.tmp00
else
   if [ "$option" = "batch" ]; then
      virtex \&chitexl $filea.tmp00|cctran 2> /dev/null
   else
     if [ "$option" = "nonstop" ]; then
        virtex \&chitexl $filea.tmp00|cctran 2> /dev/null
     else
       virtex \&chitexl $filea.tmp00|ctran
     fi
   fi
fi

if [ "$option" = "e" -o "$option" = "batch" -o "$option" = "nonstop" ]; then
echo " "
echo "****************************************               "
echo "*****  ChiTeX version 6.0.9 - by                         "
echo "*****     (Hung-Yih Chen)     "
echo "*****  ߤjǼƾǨt "
echo "*****  (Dept. of Math., National Central Univ.)"
echo "*****  email: yih@math.ncu.edu.tw "
echo "****************************************               "
fi



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

export fileno=
export inputno=
export labelcount=
export bitemcount=

exit

