#!/bin/sh
#
# This script is a part of ChiTeX
# By Hung-Yih Chen    
#           Last updated  Jan. 20, 2000
#
#                All rights preserved   
#
# This script is used to compile both chinese (Big5/GB)
# plain TeX or LaTeX source file
#
# Usage: chitex [-option [option]] sourcefile
#
#         chitex math2   (or chitex math2.tex)
# Example:
#         chitex math2   (or chitex math2.tex)
# when you have math2.tex 
#



newchitran="y"

#nottfcheck="y"      # if remove the first character '#' of this line,
                     #   it will not check the change of directories
                     #   of chinese TrueType files. The contents of
                     #   those directories will the the content of
                     #   file 'chitex.fdfile' which define definitions
                     #   of chinese fonts. So if the contents of
                     #   the directories (of chinese TrueType files)
                     #   (.../texmf/fonts/chinese/ttf) is as before,
                     #   then you may do such removing of "#' if
                     #   your system work slowly in shell scripts.
                     # As I know, systems of 'linux' and 'freebsd' works
                     #   quickly in shell scripts, so need not do this
                     #   removing of '#'

#quickmode="y"     
#export quickmode    # if remvove the first character '#' of this and 
                     #   the above line,
                     #   it will not have the utlility of "\filecontents"
                     #   for chinese to accellerlate the speed of
                     #   ChiTeX for some system which works slowly in shell
                     #   scripts
                     # As I know, systems of 'linux' and 'freebsd' works
                     #   quickly in shell scripts, so need not do this
                     #   removing of '#'

gawkbin="gawk"

option="0"; opno="0"                               
                                                   
case "$1" in
   "-big5")option="big5"; code="big5"
          opno="1"
          codeoption="y";;
   "-gb") option="gb";  code="gb"
          opno="1"
          codeoption="y";;
   "-e")  option="e"
          opno="1"
          code="big5";;
   "-batch") option="batch"
             opno="1"
             code="big5";;
   "-nonstop") option="nonstop"
               opno="1"
               code="big5";;
   "-c")   option="0"
          opno="1"
          code="big5";;
   "-help") option="-help"
             opno="1";;
   "-chitexhelp") option="-chitexhelp"    
                  opno="1";;           
   "-fonts") option="-fonts"    
                  opno="1";;           
   "-chitexconfig" ) option="-chitexconfig"
                     opno="1";;
   "-pdftex") option="-pdftex"
              opno="1";;
   *) option="none"
      code="big5"
      opno="0";;
esac
    

if test "$option" = "-chitexhelp"; then
   chitexhelp
   exit
fi

if test "$option" = "-fonts"; then
   clear
   echo " "; echo " "
   echo "    Aܤ֥iϥΤUCrO:"
   echo " "
   echo "        \\ming       % 幩ө"
   echo "        \\wdming     % 幩ө"
   echo "        \\wdmingb    % 幩ө  (uΩϥ dvipdfm)"
   echo "        \\wdmingbi   % 幩ө  (uΩϥ dvipdfm)"

   echo "        \\wdkai      % 幩"
   echo "        \\wdkaib     % 幩ʷ  (uΩϥ dvipdfm)"
   echo "        \\wdkaibi    % 幩ײʷ  (uΩϥ dvipdfm)"

   echo "        \\idming     % s"
   echo "        \\idmingb    % s餧  (uΩϥ dvipdfm)"
   echo "        \\idmingbi   % sײ餧  (uΩϥ dvipdfm)"

   echo "        \\moekai     % Ш|"
   echo "        \\moekaib    % Ш|ʷ  (uΩϥ dvipdfm)"
   echo "        \\moekaibi   % Ш|ײʷ  (uΩϥ dvipdfm)"

   echo "        \\moesung    % Ш|ө"
   echo "        \\moesungb   % Ш|餧ө  (uΩϥ dvipdfm)"
   echo "        \\moesungbi  % Ш|ײ餧ө  (uΩϥ dvipdfm)"
   echo " "
   echo "        MORE ..., Press Return key to continue"
   read xxxxx

   clear
   echo " "
   echo "        \\MING     % 幩ө Adobe Type1 r"
   echo "        \\IDMING   % s Adobe Type1 r"
   echo "        \\MOEKAI   % Ш| Adobe Type1 r "
   echo "        \\KAI      %  Adobe Type1 r"
   echo "        \\FS       % 駺 Adobe Type1 r"
   echo "        \\LI       %  Adobe Type1 r"
   echo "        \\MR       %  Adobe Type1 r"
   echo "        \\WUMING   % d (cwtex) Adobe Type1 r"
   echo "        \\WUFS     % d駺 (cwtex) Adobe Type1 r"
   echo "        \\WUHEI    % d (cwtex) Adobe Type1 r"
   echo " "
   echo "        \\songGB   % 駺 (GB X, j s[Y)"
   echo "        \\kaiGB    %  (GB X, j s[Y)"
   echo "        \\SONGGB   % 駺 (GB X, j s[Y) Adobe Type1 r"
   echo "        \\KAIGB    %  (GB X, j s[Y) Adobe Type1 r"
   echo " "
   exit
fi


if test "$option" = "-chitexconfig"; then
   chitexconfigmenu
   exit
fi



#fi

chitexdvipsv(){
   if test "$optvar" = "-xdvi"; then
      command="chitexdvi"
   else
    if test "$optvar" = "-psv" -o "$optvar" = "-dvipsgv"  -o "$optvar" = "-dvips"; then
       command="chitexpsv"
    else
       command="ex"
    fi
   fi
   if test "$command" != "ex"; then
      if test "$optvar" = "-dvips"; then
        $command -nogv $optb $optc $optd $opte $optf $optg $opth $opti
      else
        $command $optb $optc $optd $opte $optf $optg $opth $opti
      fi
      exit
   fi
}


optvar=$1; opta=$1;  optb=$2;  optc=$3;  optd=$4;  opte=$5; 
optf=$6; optg=$7;  opth=$8;  optg=$9


chitexdvipsv



if test "$1" = "-pdftex" -o "$2" = "-pdftex"; then
   opno="3"
fi

if test "$#" = "0" -o "$opno" = "1" -a "$#" = "1" \
  -o "$#" = "2" -a "$codeoptin" = "y" ; then
      option="help"
fi



menu () {
   clear
   echo " "; echo " "
   echo "  Usage: chitex [option [option]] sourcefile "
   echo " "
   echo "  where sourcefile is a TeX/LaTeX source file, option are:"
#   echo "  option are:"
   echo "  -pdftex  compile with pdftex/pdflatex"
   echo "  -xdvi    with compile + xdvi "
   echo "  -psv     with compile + dvips + gv(ghostview)"
   echo "  -dvipsgv with compile + dvips + gv(ghostview)"
   echo "  -dvips   with compile + dvips"
   echo "  -gb      using in GBchinese, this option must appear as the 1st option"
#   echo "           this option must appear as the 1st option"
   echo "           using '\GBchinese' in GB chinese document may omit this option"
#   echo "           document may omit this option"
   echo "  -big5    using in Big5 chinese (default is with option -big5)" 
   echo "           this option must appear as the 1st option"
   echo "  -e       with error message and .log file non-chineses"
   echo "  -c       with chinese error message and .log file, default is with option -c"
#   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'"
#   echo "           could spend less time."
#   echo "           There is a command 'chilatexb' which is equivalent to"
#   echo "           'chilatex -batch' and is used for 'lyx'."
   echo "  -nonstop  same as '-batch'"
   echo "  -chitexhelp  call program 'chitexhelp'"
   echo "  -chitexconfig  call program 'chitexconfig'"
   echo "  -fonts    help to use default chinese fonts macros"
   echo "  -help    help"
   echo " "
   exit
}

if test "$option" = "help"; then
   menu
fi



if test "$1" = "-pdftex"; then
   pdftex="y"
   export pdftex
   shift

   optvar=$1; opta=$1;  optb=$2;  optc=$3;  optd=$4;  opte=$5; 
   optf=$6; optg=$7;  opth=$8;  optg=$9
   chitexdvipsv

   if test "$1" = "-big5"; then
      option="big5"; code="big5"
      opno="1"
      codeoption="y"
      shift
   else
      if test "$1" = "-gb"; then
         option="big5"; code="gb"
         opno="1"
         codeoption="y"
         shift
      fi
   fi
else  
 if test "$codeoption" = "y"; then
   shift
   optvar=$1; opta=$1;  optb="-"$code;  optc=$2;  optd=$3;  opte=$4; 
   optf=$5; optg=$6;  opth=$7;  optg=$8;  opti=$9
   chitexdvipsv
   if test "$1" = "-pdftex"; then
      pdftex="y"
      export pdftex
      shift
   optvar=$1; opta=$1;  optb="-"$code;  optc="-pdftex";  optd=$2;  opte=$3; 
   optf=$4; optg=$5;  opth=$6;  optg=$7;  opti=$8
   chitexdvipsv

   fi
 fi
fi

if test "$pdftex" = "y"; then
#   virtexbin="pdfvirtex"
   virtexbin="pdftex"
else
   virtexbin="virtex"
fi


#rm -f tmp@


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
if test "$#" = "0" -o "$opno" = "1" -a "$#" = "1"; then
   option="help"
fi



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

if test "$option" = "help"; then
   menu
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 test "$1" = "-e" -o "$1" = "-batch" -o "$1" = "-nonstop" -o "$1" = "-c"
#then
#   shift 1
#fi

optpre=$1
#if test "$1" = "-e" -o "$1" = "-batch" -a "$#" = "1"; then
if test "$1" = "-e"; then
    shift 1
else
    if test "$1" = "-batch"; then
       shift 1
    else
       if test "$1" = "-nonstop"; then
          shift 1
       else
          if test "$1" = "-c"; then
             shift 1
          fi
       fi
    fi
fi

if test "$optpre" = "-e" -o "$optpre" = "-c" \
    -o "$optpre" = "-batch" -o "$optpre" = "-nonstop"; then
   optvar=$1; opta=$1;  optb=$optpre;  optc=$2;  optd=$3;  opte=$4; 
   optf=$5; optg=$6;  opth=$7;  optg=$8;  opti=$9
   chitexdvipsv
fi

searchtexmf="y"
if test "$searchtexmf" = "y"; then
VARTEXFONTS=`kpsexpand '$VARTEXFONTS' `
if test "$VARTEXFONTS" = ""; then
   VARTEXFONTS=`kpsexpand '$VARFONTS' `
   export VARFONTS
   export VARTEXFONTS
else
   export VARTEXFONTS
fi

echo "yes" > tmp@
texmfcnffile=`kpsexpand -w cnf texmf.cnf`
TEXMFCNF=`
$gawkbin  '
{texmfcnfloc=index(texmfcnffile,"/texmf.cnf")
 print substr(texmfcnffile,1,texmfcnfloc-1)
}' texmfcnffile=$texmfcnffile tmp@ `

echo $TEXMFCNF > tmp@
TEXMF=`
$gawkbin '
{
  inline=$0
  gsub(/web2c/,"",inline)
  inline=substr(inline,1,length(inline)-1)
  print inline
}' tmp@ `

fi

#TEXMFCNF=$TEXMF/web2c
TTF2PKINPUTS=.:$TEXMF
TTF2TFMINPUTS=.:$TEXMF
TTFONTS=.:$TEXMF/fonts/chinese/ttf//
TFMFONTS=.:$TEXMF/fonts/tfm//:$VARTEXFONTS/tfm//
TEXMFDBS=$TEXMF:$VARTEXFONTS
#TEXINPUTS=.:$TEXMF/tex//
T1FONTS=.:$TEXMF/fonts/type1//:$VARTEXFONTS/type1/fonts//

export TEXMF
export VARTEXFONTS
#export TEXMFCNF
export TTF2PKINPUTS
export TTFONTS
export TTF2TFMINPUTS
export TFMFONTS
export TEXMFDBS
#export TEXINPUTS
export T1FONTS

if test "$code" = "gb"; then
   GBENV="gbenv"
   export GBENV
else
   GBENV=""   
   export GBENV
fi


fileno=1
export fileno
inputno=0
export inputno
labelcount=0
export labelcount
bitemcount=0
export bitemcount
chitranno=0
export chitranno
extno=0
export extno
#addperc=
#export addperc
#beginpercprev=
#export beginpercprev



if test ! -d .tmp00; then
   mkdir .tmp00
fi


PWD=`pwd`  

/bin/rm -f *@ *.tmp00 hyper@ref
if test -s ch@labelfile; then /bin/rm -f  ch@labelfile;fi


chbold=
export chbold

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

echo "yes" > tmp@

new="y"
if test "$new" = "y"; then
newfilename=""
newfilename=`
gawk -v filename=$filename -v extno=$extno '
{
 llen=length(filename)
 i=1; chinese=0
 for (i;i<=llen;i++)
   {ch=substr(filename,i)
    if (ch > "\240" && ch < "\372")
       {chinese=1; break}
   } 
 if (chinese==1)
    {
     while (getline inline < ".tmp00/texf.tab">0)
      {++recno
       sp=index(inline," ")
       chfilename=substr(inline,1,sp-1)
       if (filename==chfilename)
          {rinline=substr(inline,sp+1)
           gsub("^ *","",rinline)
           newfname=rinline; found=1
          }
      }        
     close(".tmp00/texf.tab")

     if (found==0)
       {
        if (recno>1000)
          {recno=0; print "yes" > "recnomax@"}
           newfname="m@@ch@" recno+1 "@.tex"
        if (recno>1000)
          {print filename " " newfname > ".tmp00/texf.tab"}
        else
          {print filename " " newfname >> ".tmp00/texf.tab"}
       }
     
     print newfname
    }
}' tmp@ `  


  if test -s recnomax@; then
     echo " "
     echo "  File name table too large. "
     echo "  Please run chitex from the beginning."
     echo " "
  fi

  oldfilename=$filename
  export oldfilename

if test "$newfilename" != ""; then
   cp -f $filename $newfilename
#   oldfilename=$filename
   chinesef=1
   bname=`basename $filename`                    
   oldfilea=`echo $bname|cut -d. -f 1` 
   filename=$newfilename
fi
fi

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

  filea=$filea
  export filea


echo $filename > texfile@


#if test -s $filea.tex; then
if test -s $filenamea.tex; then
   texfile=$filea.tex
else
   texfile=$filea
fi


# Determine GB' mode if have \GBchinese in source document
#GBchinese=`fgrep "\\\\\\GBchinese" $filename|cut -d% -f 1`
#bgdoc=`fgrep "\\\\\\begin{document}" $filename|cut -d% -f 1`


#if test "$GBchinese" != ""; then
#  if test "$bgdoc" != ""; then
  gbchinese=`
   $gawkbin '
   {
    per=index($0,"%"); 
    if (per >0)
       {inline=substr($0,1,per-1)}
    else
       {inline=$0}
    bgdoc=index(inline,"\134begin{document}")
    if (bgdoc == 0)
       {
        gbchinese=index(inline,"\134GBchinese")
        if (gbchinese > 0)
           {print "y";exit}
       }
    else
       {exit}
   }' $filename  `
#  fi

  
#  if test "$bgdoc" = "" -o "$gbchinese" = "y"; then
  if test "$gbchinese" = "y"; then
   code="gb"  
   GBENV="gbenv"
  else
   code="big5"
   GBENV=""
  fi
  export GBENV

#else
#   code="big5"
#   GBENV=""
#   export GBENV
#fi

##### Check whether the contents of dir.$TEXMF/fonts/chinese/ttf
#     has been changed. If so, then run chitexconfig -chitex.fdf
#####
if test "$nottfcheck" = ""; then
if test -f $VARTEXFONTS/ttf; then rm -f $VARTEXFONTS/ttf; fi
if test ! -d $VARTEXFONTS/ttf; then
  if test "$LOGNAME" = "root"; then
    mkdir $VARTEXFONTS/ttf
    chmod a+rw $VARTEXFONTS/ttf
  fi
fi

/bin/ls $TEXMF/fonts/chinese/ttf > ttffiletmp
/bin/ls $VARTEXFONTS/ttf >> ttffiletmp
#/bin/ls $VARTEXFONTS/type1/fonts >> ttffiletmp

if test -s $VARTEXFONTS/ttf/ttffile; then
   changettf=`diff ttffiletmp $VARTEXFONTS/ttf/ttffile`
fi

if test "$changettf" != ""; then
   chitexconfig -chitex.fdf
fi
if test -f ttffiletmp; then
   cp -f  ttffiletmp  $VARTEXFONTS/ttf/ttffile
   if test "$LOGNAME" = "root"; then
      chmod a+rw $VARTEXFONTS/ttf/ttffile
   fi
   /bin/rm -f ttffiletmp
fi
fi # end of nottfcheck


if test "$code" = "big5"; then
  if test "$pdftex" = "y"; then
   texfmt="pdfchitex"
   latexfmt="pdfchitexl"
  else
   texfmt="chitex"
   latexfmt="chitexl"
  fi
else
  if test "$pdftex" = "y"; then
   texfmt="pdfchitexg"
   latexfmt="pdfchitexlg"
  else
   texfmt="chitexg"
   latexfmt="chitexlg"
  fi
fi   


#docu=`
#$gawkbin -f chitexsub1.awk $filename `

if test "$newchitran" = "y" ; then
  docu=`
  chitexsub1 $filename `
else
  docu=`
  $gawkbin '
BEGIN{}
{ inline=$0
     inlinetmp=inline; newline=""
     CMDSTR="\\%"; NEWCMDSTR="##"
     while (inlinetmp != "")  
       {cmdpos=index(inlinetmp,CMDSTR)
        if (cmdpos>0)
          {newline=newline substr(inlinetmp,1,cmdpos-1) NEWCMDSTR
           inlinetmp=substr(inlinetmp,cmdpos+length(CMDSTR))
          }
        else
          {newline=newline inlinetmp; inlinetmp=""}
       }
     inline=newline

#  gsub("\\%","##",inline)
  perc=index(inline,"%")
  if (perc > 0)
     {inline=substr($0,1,perc-1)}
  else
     {inline=$0}

  if (doculoc == 0)
    {doculoc=index(inline,"\134documentclass")}
  else
    {if (docusty == 0)
      {docusty=index(inline,"\134documentstyle")}
     else
      {if (begindocu == 0)
         {begindocu=index(inline,"\134begin{document}")}
       else
         {if (begindocua == 0)
           {begindocua=index(inline,"\134begin {document}")}
          else
           {if (enddocu == 0)
               {enddocu=index(inline,"\134enddocument")}
            else
             {if (inputamstexa == 0)
                {inputamstexa=index(inline,"\134input amstex")}
              else
               {if (inputamstexb == 0)
                 {inputamstexb=index(inline,"\134input  amstex")}
                else
                 {if (inputamstexc == 0)
                    {inputamstexc=index(inline,"\134input   amstex")}
                  else
                   {if (inputamstexd == 0)
                      {inputamstexb=index(inline,"\134input    amstex")}
                    else
                       {if (inputamstexe == 0)
                          {inputamstexe=index(inline,"\134input     amstex")}
                       }
                   }
                 }
               }
             }
           }
         }
      }
    }


  if (inputamstexa > 0 || inputamstexb > 0 || inputamstexc > 0 \
     || inputamstexd >0 || inputamstexe > 0)
     {inputamstex=1;print "plainamstex"# > "docufile@"
      exit}

  if (doculoc > 0)
    {print "latex" # > "docufile@"
     documentclass=1;exit}
  
  if (docusty > 0)
     {documentstyle=1}
  if (begindocu > 0 || begindocua > 0)
     {begindocument=1}
  if (enddocu > 0)
     {enddocument=1}
# }
}
END{
#print documentclass " " documentstyle " " begindocument

  if (documentclass == 0 && documentstyle == 1 && inputamstex == 0)
     {if (begindocument == 1)
         {print "latex" # > "docufile@"
         }
      else
        {if (enddocument == 1)
            {
             print "amstex" # > "docufile@"
            }
         else
            {print "amstexorlatex" # > "docufile@"
            }
        }
     }
}' $filename `

fi # if "$newchitran" = "y"; then 



if test "$docu" = ""; then
   docu="plaintex"
fi

if test "$docu" = "amstexorlatex"; then
typechoose="0"
 while test "$typechoose" = "0"
 do
 echo "    *****************************************"
 echo "    *                                       *"  
 echo "    *   Tell me the type of your document:  *"
 echo "    *   1. LaTeX document                   *"
 echo "    *   2. AmsTeX document                  *"
 echo "    *                                       *"  
 echo "    *****************************************"   
 echo " "
 echo -n "   Type '1' or '2' to choose one of the above: "
 read inkey

 if test "$inkey" != "1" -a "$inkey" != "2"; then
    typechoose="0"
 else
    typechoose="1"
 fi

 if test "$inkey" = "1"; then
    docu="latex"
 else
    docu="amstex"
 fi 
 done

fi


if test "$docu" = "latex"; then

 echo "    ************************************************"
 echo "    *                                              *"
 echo "    *   The compiled document is a LaTeX document  *"
 echo "    *                                              *"
 echo "    ************************************************"

/bin/rm -f repl*@

if test "$newchitran" = "y"; then
   chitranc $filename
else
   chitran $filename
fi


if test -s defmacro@; then
   mv -f defmacro@ def@macro
fi

if test -s chbold@; then
$gawkbin '
BEGIN{}
{print $0 > "maintemp@"
 if (documentclasspos == 0)
  {inline=$0
   gsub("\\%","##",inline)
   per=index(inline,"%")
   if (per > 1)
      {inline=substr($0,1,per-1)}
   else
      {inline=$0}
   documentclasspos=index(inline,"\134documentclass")
   if (documentclasspos > 0)
      {print "\\usepackage{chboxit}" > "maintemp@"}
  }
}
END{} ' $filea.tmp00

mv -f maintemp@ $filea.tmp00

fi  


#$gawkbin '
#BEGIN{print "\\makeatletter\\let\\ch@RunBoldIt\\relax\makeatother" > "maintemp@"}
#{
#   inline=$0
#   print inline > "maintemp@"
#} ' $filea.tmp00

# mv -f maintemp@ $filea.tmp00


if test -s ch@labelfile; then
   mv -f ch@labelfile ch@label
fi

enddoc=`cut -d% -f 1 $filea.tmp00|fgrep "\\end{document}"`
if test "$enddoc" = ""; then
   enddocsp=`cut -d% -f 1 $filea.tmp00|fgrep "\\end {document}"`
fi
if test "$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@ *.@@@ 
/bin/rm -f *@

echo "yes" > tmp@
if test -s errf@.dat; then /bin/rm -f errf@.dat; fi

compilelatex () {
if test "$option" = "e"; then
   latex \&$latexfmt $filea.tmp00
else
   if test "$option" = "batch"; then
       latex \&$latexfmt $filea.tmp00|cctran 2> /dev/null
   else
     if test "$option" = "nonstop"; then
        latex \&$latexfmt $filea.tmp00|cctran 2> /dev/null
     else
       latex \&$latexfmt $filea.tmp00|ctran 2> errf@.dat
     fi
   fi
fi
}


compilelatex



#echo $$filea

  if test -s $filea.toc -o -s $filea.lof -o -s $filea.lot; then
     secondcompile="1"
  else
     if test -s $filea.log; then
        msg=`fgrep "LaTeX Warning: Reference" $filea.log|fgrep "undefined" `
        msga=`fgrep "LaTeX Warning: Label(s) may have changed" $filea.log `
        if test "$msg" != "" -o "$msga" != ""; then
           secondcompile="1" 
        fi
     fi
  fi          
# fi


   msgbbl=`fgrep "bibdata" $filea.aux`
   if test -s $filea.idx; then
      msgind="yes"
   fi


#  if test "$msgbbl" != ""; then
#     clear
#     echo "     *************************************"
#     echo "     *                                    "
#     echo "     *  You have used bibtex's utility.   "
#     echo "     *  May you need use command:         "
#     echo "     *       cbibtex $filea               "
#     echo "     *  or                                "
#     echo "     *       cbibtex ***                  "
#     echo "     *  where *** denote .bib file        "
#     echo "     *  And then give command:            "
#     echo "     *     chitex $filsa                  "
#     echo "     *  or other compile+preview command  "
#     echo "     *  to compile again                  "
#     echo "     *                                    "
#     echo "     *************************************"     
#  fi
#  if test "$msgind" != ""; then
#     clear
#     echo "     ***************************************"
#     echo "     *                                    "
#     echo "     *  You have used makeindex's utility.   "
#     echo "     *  May you need use command:         "
#     echo "     *       cmakeindex $filea               "
#     echo "     *  And then give command:            "
#     echo "     *     chitex $filsa                  "
#     echo "     *  or other compile+preview command  "
#     echo "     *  to compile again                  "
#     echo "     *                                    "
#     echo "     *************************************"     
#  fi
#
#
#  if test "$msgbbl" != "" -o "$msgind" != ""; then
## Mw script
#  echoprompt=`echo -n "."`
#  if test "$echoprompt" = "."; then
#     echomode="-n "; echocr=""
#  else
#     echoprompt=`echo  ".\c"`
#     if test "$echoprompt" = "."; then
#        echomode=""; echocr="\c"
#     else 
#        echomode="-e "; echocr="\c"
#     fi
#  fi
#     
#   choosemenu="y"
#   while test "$choosemenu" = "y"
#   do
#     clear
#     echo " "
#     echo "     c. Continue to next run of compile "
#     echo "     q. Quit to run 'cbibtex' or 'cmakeindex' "  
#     echo "     "
#     echo $echomode "     Type 'c' or 'q' to choose one of the above: " \
#                            $echocr
#     read inkey
#     case "$inkey" in
#     "c") secondcompile="1"; choosemenu="n" ;;
#     "q") secondcompile=""; choosemenu="n" ;;
#     "*") choosemenu="y" ;;
#     esac
#   done
#  fi   

  if test "$secondcompile" = "1"; then
   if test "$code" = "big5"; then
     echo "*******************************"
     echo "*                             *"
     echo "*      }lĤG^sĶ         *"
     echo "*                             *"
     echo "*******************************"
   else
     echo "*******************************"
     echo "*                             *"
     echo "*      ʼڶر         *"
     echo "*                             *"
     echo "*******************************"
   fi
     echo " "

#     chilatex $1


  if test -s def@macro; then
     chicodeb $filea
     mv -f tex@.out $filea".out"
  fi

   compilelatex

  fi

if test -s pfbfile@; then
   texhash
fi

#/bin/rm -f tmp@@ *.tmp@* *.temp$ *.tmp!* 
#/bin/rm -f transl@*  addmac*@ *.tmp0*  *@  ch@export* ch@labelfile
/bin/rm -f *@ *.tmp00 ch@labelfile
cp .tmp00/*.aux . >/dev/null 2>&1

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

fi  #end latex





if test "$docu" = "plaintex" -o "$docu" = "plainamstex" -o "$docu" = "amstex"; then
 echo "    **************************************************"
 echo "    *                                                *"
 if test "$docu" = "plaintex"; then
     echo "    *  The compiled document is a plain TeX document *"
 else
     echo "    *    The compiled document is a AmsTeX document  *"
 fi
 echo "    *                                                *"
 echo "    **************************************************"



if test "$newchitran" = "y"; then
   chitranpc $filename
else
   chitranp $filename
fi


if test -s chbold@; then
$gawkbin '
#BEGIN{print "\\input chboxit" > "maintemp@"}
{print $0 > "maintemp@"
}
END{} ' $filea.tmp00

mv -f maintemp@ $filea.tmp00

fi  


if test "$docu" = "amstex"; then
   echo "\\input amstex" > add@
   cat $filea.tmp00 >> add@
   mv -f add@ $filea.tmp00
   docu="plainamstex"
fi   


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


end=`
$gawkbin '
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=`
$gawkbin '
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@@ `

enddoc=`
$gawkbin '
BEGIN{}
{
 enddoc=$0
 if (enddoc != "")
 {lenn=length(enddoc)
  gsub("\t"," ",enddoc)
  enddocloc=index(enddoc,"\134enddocument")
  enddocnext=substr(enddoc,enddocloc+12,1)
  haveenddoc=0
  if (enddoc !="")
     {if (enddocnext == "
"||enddocnext == " "|| enddocnext == "\134"\
          ||enddocnext == "")
         {haveenddoc=1}
     }
  if (haveenddoc == 1)
     {print "enddoc"}
  else
     {print ""}
 }
 else
     {print ""}
}
END{} ' tmp@@@ `



if test "$docu" = "plaintex"; then
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
fi

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


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


if test "$option" = "e"; then
   tex \&$texfmt $filea.tmp00
else
   if test "$option" = "batch"; then
      tex \&$texfmt $filea.tmp00|cctran 2> /dev/null
   else
     if test "$option" = "nonstop"; then
        tex \&$texfmt $filea.tmp00|cctran 2> /dev/null
     else
       tex \&$texfmt $filea.tmp00|ctran 2> errf@.dat
     fi
   fi
fi


if test -s pfbfile@; then
   texhash
fi

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


 if test -s $filea.log; then
  qmark=`
  $gawkbin '
  BEGIN{}
  {  
   if (qmark == "")
    {
     qmarkloc=index($1,"?")
     if (qmarkloc == 1)
        {qmark=1
         print "qmark"}
    }
  }
  END{} ' $filea.log `

  if test "$qmark" != "" -o ! -s $filea.log; then
#     clear
     echo " "
     echo "       ******************************************"
     echo "       *                                        *"
     echo "       *                Warning!                *"
     echo "       *  Compiled TeX document may have errors  *"
     echo "       *                                        *"
     echo "       ******************************************"
     echo "  "
  fi
 fi

  nopage=`
  $gawkbin '
  BEGIN{}
  {  
   if (nopage == "")
    {
     nopageloc=index($0,"No pages of output")
     if (nopageloc == 1)
        {nopage=1
         print "nopage"}
    }
  }
  END{} ' $filea.log `


  if test "$nopage" != ""; then
     echo "       ******************************************"
     echo "       *                                        *"
     echo "       *  Compiled TeX document may has errors. *"
     echo "       *  No dvi file generated.                *"
     echo "       *                                        *"
     echo "       ******************************************"
     echo " "
     exit
  fi


# rm -f tmp@

if test "$qmark" != "" -o ! -s $filea.log \
 -o "$option" = "e" -o "$option" = "batch" -o "$option" = "nonstop"; then
if test "$code" = "big5"; then
echo "****************************************               "
echo "*****  ChiTeX version 6.1.2 - by                         "
echo "*****     (Hung-Yih Chen) (yih@math.ncu.edu.tw)   "
echo "*****  ߤjǼƾǨt (Dept. of Math., National Central Univ.)"
echo "****************************************               "
else
echo "****************************************               "
echo "*****  ChiTeX version 6.1.2 - by                         "
echo "*****     (Hung-Yih Chen) (yih@math.ncu.edu.tw)   "
echo "*****  ѧѧϵ (Dept. of Math., National Central Univ.)"
echo "****************************************               "
fi

fi

GBENV=""
export GBENV
chitranno=
export chitranno

if test "$chinesef" = "1"; then
   if test -s $filea.pdf; then
      /bin/mv -f $filea.pdf $oldfilea.pdf
   fi
   if test -s $filea.dvi; then
     /bin/mv -f $filea.dvi $oldfilea.dvi
   fi
   /bin/mv -f $filea.log $oldfilea.log
   cp -f $filea.aux $oldfilea.aux
   filea=$oldfilea
fi


   ctranbb $filea.log


if test -s  tmplog@@; then
   mv tmplog@@ $filea.log
fi
/bin/rm -f tmp@ tmplog@ m@@ch@*.tex m@@ch@*.bib def@macro hyper@ref
/bin/rm -f errf@.dat


exit

