#!/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 
#




if test -s HaveCJKutf8@; then
   rm -f HaveCJKutf8@ 
fi

if test "$1" = "-utf8"; then
   if test -s $2; then
      FFname=$2
   else
      if test -s $2".tex"; then
         FFname=$2".tex"
      else
         if test "$2" = "-dvipdfm"; then
            if test -s $3; then
               FFname=$3
            else
               if test -s $3".tex"; then
                  FFname=$3".tex"
               fi
            fi    
            Havedvipdfm="y"  
         fi
      fi
   fi
   HaveUtf="y"
else
   if test "$1" = "-dvipdfm"; then
      if test -s $2; then
         FFname=$2
      else
         if test -s $2".tex"; then
            FFname=$2".tex"
         fi
      fi
      Havedvipdfm="y"
   else
     if test -s $1; then
        FFname=$1
     else
        if test -s $1".tex"; then
           FFname=$1".tex"
        fi
     fi
   fi   
fi


if test "$FFname" != ""; then
chGetCJK $FFname
if test -s HaveCJKutf8@ ; then
   HaveUtf8="y"
fi

if test "$HaveUtf" = "y" -a "$HaveUtf8" = "y"; then
   shift
   if test "$Havedvipdfm" = "y"; then
      shift
   fi
else
   if test "$Havedvipdfm" = "y" -a "$HaveUtf8" = "y"; then
      shift
   fi
fi
fi

# echo "HaveUtf8=" $HaveUtf8

# exit

export LC_ALL=C

pwddir=`pwd`

  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


if test -s /bin/gawk-3.1.4; then
   gawkbin="gawk-3.1.4"
else    
   gawkbin="gawk"
fi

export gawkbin

echo "y" > "tmp@"


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@
TEXMFA=`
$gawkbin '
{
  inline=$0
  gsub(/web2c/,"",inline)
  inline=substr(inline,1,length(inline)-1)
  print inline
}' tmp@ `



texmffile=`kpsexpand '$TEXMFDIST' `
if test "$texmffile" != ""; then
TEXMFDIR=` gawk '
   {lbr=index(texmffile,"{")
    sep=index(texmffile,",")
    dash=index(texmffile,"-")
    texmfpre=substr(texmffile,1,dash)
    if (lbr > 0)
       {texlive=substr(texmffile,lbr+1,sep-lbr-1) 
        texmf =texmfpre texlive
       }
    else
       {if (dash > 0)
           {texlive=substr(texmffile,dash+1) 
            texmf =texmfpre texlive
           } 
       }    
    print texmf
   }' texmffile=$texmffile tmp@ `
fi


#TEXMF=`kpsexpand '$TEXMF'`
#TEXMF=`kpsexpand '$TEXMF'`


if test "$TEXMFDIR" != "" -a -d "$TEXMFDIR"; then
   if test ! -d "$TEXMFA/source"; then
      TEXMFA=$TEXMFDIR
   fi   
fi


#TEXMF=`kpsexpand '$TEXMFMAIN|cut -d! -f 1' `
TEXMFMAIN=`kpsexpand '$TEXMFMAIN'|cut -d! -f 1 `

#TEXMFA=$TEXMFMAIN
export TEXMFA

if test ! -s /usr/bin/lambda; then
   aleph="y"
   export aleph
fi


 if test "$1" = "-xdvi" -a "$2" != ""; then
    chitexccc $2 "-xdvi"
    exit
 fi

 if test "$1" = "-psv" -a "$2" != ""; then
    chitexccc $2 "-psv"
    exit
 fi

 if test "$1" = "-dvipdfm" -a "$2" != ""; then
    chitexcc $1 $2 "nul" "-dvipdfm"
    exit
 fi

 if test "$1" = "-omega" -a "$2" = "-dvipdfm" -a "$3" !=  ""; then
:
    chitexcc $1 $2 $3 "-omegadvipdfm"
    exit
 fi

 if test "$1" = "-utf8" -a "$2" = "-dvipdfm" -a "$3" !=  ""; then
    echo "yes" > dvipdfm2.dat
    chitexcc $1 $2 $3 "-utf8dvipdfm"
    exit
 fi


 if test "$1" = "-xelatex" -o "$1" = "-xetex"; then
#    if test "$2" != ""; then
       if test -s "$2"; then
          xelatex $2        
       else
          if test -s "$2.tex"; then          
             xelatex "$2".tex        
          else
             filename=`find $TEXMFA/tex/ -follow -name $2 -print`
             if test "$filename" = ""; then
                filename=`find $TEXMFA/tex/ -follow -name $2.tex  -print`
                if test "$filename" = ""; then
                   exit
                else   
                   filenamea=`basename $filename`
                   xelatex "$filenamea"
                fi
             else
                xelatex $filename           
             fi
          fi
       fi   
       exit    
#    else
#       exit
#    fi    
 fi


if test -s /bin/gawk-3.1.4; then
   gawkbin="gawk-3.1.4"
else    
   gawkbin="gawk"
fi

export gawkbin

preaddpercent="y"
export preaddpercent

echo "yes" > tmp@
latex=`which latex`
bindir=`
$gawkbin '
BEGIN{}
{texloc=index(latex,"/latex")
 bindir=substr(latex,1,texloc-1)
 print bindir
}
END{} ' latex="$latex" tmp@ `

unixname=`uname|awk '{print $0}'|tr '[A-Z]' '[a-z]'`
export unixname

chrun="y"
if test "$chrun" = "y"; then
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

texmffile=`kpsexpand '$TEXMFDIST' `
if test "$texmffile" != ""; then
TEXMFDIR=` gawk '
   {lbr=index(texmffile,"{")
    sep=index(texmffile,",")
    dash=index(texmffile,"-")
    texmfpre=substr(texmffile,1,dash)
    if (lbr > 0)
       {texlive=substr(texmffile,lbr+1,sep-lbr-1) 
        texmf =texmfpre texlive
       }
    else
       {if (dash > 0)
           {texlive=substr(texmffile,dash+1) 
            texmf =texmfpre texlive
           } 
       }    
    print texmf
   }' texmffile=$texmffile tmp@ `
fi

if test "$TEXMFDIR" != "" -a -d "$TEXMFDIR"; then
   TEXMF=$TEXMFDIR
fi

#export TEXMF
fi

TEXMFMAIN=`kpsexpand '$TEXMFMAIN'|cut -d! -f 1 `
export TEXMFMAIN

  if test -s /usr/bin/kile -o -s /usr/local/bin/kile; then
      if test ! "$LOGNAME" = "root"; then
         Hdir="/home/$LOGNAME"
      else
         Hdir="/root"
      fi
      if test ! -d $Hdir/.kde/share/config; then
         mkdir -p $Hdir/.kde/share/config
      fi

       if test -s $Hdir/.kde/share/config/kilerc; then
          chitexstr=`grep "chitex" $Hdir/.kde/share/config/kilerc`
       fi
       if test "$chitexstr" = ""; then
         cp $TEXMFA/tex/chinese/kilerc $Hdir/.kde/share/config/kilerc 2>/dev/null
       fi   
  fi

#  cp $TEXMF/*.sfd .
#   NowDir=`pwd`
#   export NowDir
 rm -f missfont.log
   
  if test -s /usr/bin/lyx -o -s /usr/local/bin/lyx; then
      if test ! "$LOGNAME" = "root"; then
         Hdir="/home/$LOGNAME"
      else
         Hdir="/root"
      fi
      if test ! -d $Hdir/.lyx; then
         mkdir -p $Hdir/.lyx
      fi
       chitexstr=""
       if test -s $Hdir/.lyx/preferences; then
          chitexstr=`grep "chitex" $Hdir/.lyx/preferences`
       fi
       if test "$chitexstr" = ""; then
         cp $TEXMFA/tex/chinese/preferences $Hdir/.lyx/preferences
       fi   
  fi

  if test -s /usr/bin/lyx -o -s /usr/local/bin/lyx; then
     if test "$1" = "-lyx"; then    
       lname=`echo $3|cut -d. -f 2`
       if test "$lname" != "tex"; then
          exit
       fi

       if test "$2" = "-chinese" -o "$2" = "-english"; then
        if test "$3" != ""; then
           fontenc=`grep "\usepackage\[T1\]{fontenc}" $3`
           if test "$fontenc" != ""; then
             fontenc="y"
           fi  
           inputenc=`grep "\usepackage\[utf8\]{inputenc}" $3`
           if test "$inputenc" != ""; then
             inputenc="y"
           fi  
           babel=`grep "\usepackage{babel}" $3`                      
           if test "$babel" != ""; then
             babel="y"
           fi  

           if test "$fontenc" = "" -o  "$inputenc" = "" -o "$babel" = ""; then
             gawk -v fontenc=$fontenc -v babel=$babel -v inputenc=$inputenc '
             {inline=$0
              print inline > "tmpfile@"
              doccls=index($0,"\\documentclass")
              doccls2=index($0,"\\documentclass") 
              if (doccls > 0 || doccls2 > 0)
                 {
                  if (fontenc == "")
                     {print "\\usepackage[T1]{fontenc}" > "tmpfile@"}
                  if (inputenc == "")
                     {print "\\usepackage[utf8]{inputenc}" > "tmpfile@"}
                  if (babel == "")
                     {print "\\usepackage{babel}" > "tmpfile@"}
                 }
             }' $3
             cp -f tmpfile@ $3
           fi
           tex2lyx -f $3 
           bname=`echo $3|cut -d. -f 1`
           gawk -v lan=$2 '
           {inline=$0
            enorch=index($0,"\\language ")
            if (enorch > 0)
              {if (lan == "-chinese")
                  {
                   inline="\\language chinese-traditional"
                  }
               else
                  {
                  inline="\\language english"
                   }     
              }
              print inline > "tmpfile2@"
           }' $bname.lyx   
           cp tmpfile2@ $bname.lyx   
           lyx $bname.lyx   
        fi
       fi 
       exit
     fi
  fi

  if test "$1" = "--interaction" -a "$2" = "nonstopmode"; then
     shift 
     shift
  fi

  if test "$1" = "-interaction=nonstopmode"; then
     shift 
  fi

  if test "$1" = "-noinputenc"; then  
     noinputenc="y"
     export noinputenc
#     shift
     if test "$2" != "-utf8"; then
      if test "$2" != "-dvipdfm" -a "$2" != "-pdftex"; then
       if test ! -s $2.tex; then
         if test ! -s $2; then 
           exit
         else
          filenamea=$2
         fi
       else 
        filenamea=$2.tex
       fi
      else
       if test "$2" = "-pdftex"; then
          pdftexfile="y"
          export pdftexfile
       fi
       if test ! -s $3.tex; then
        if test ! -s $3; then 
         exit
        else
         filenamea=$3
        fi
       else 
         filenamea=$3.tex
       fi
      fi
      NoUtf="y"
      export NoUtf        
         gawk '
         {inline=$0
          utf=index(inline,"\\usepackage[utf8]")
          if (utf ==  1)
             {inline="%" inline}
          utf=index(inline,"\\usepackage{babel}")
          if (utf ==  1)
             {inline="%" inline}
          utf=index(inline,"\\usepackage[T1]")
          if (utf ==  1)
             {inline="%" inline}
          CJK=index(inline,"\\usepackage{CJK}")
          if (CJK != 0)
             {inline="%" inline}
          print inline > "tmpfile@"
         }'  $filenamea
         
         if test -s tmpfile@; then
            mv -f tmpfile@  $filenamea
         fi 
      
      if test -s $filenamea; then
        iconv -f UTF8 -t BIG5 < $filenamea > tmpfile@
        mv $filenamea $filenamea.bak
        bkfile=$filenamea.bak
        orfile=$filenamea
        export bkfile
        export orfile
        mv -f tmpfile@ $filenamea 
        shift
      fi  
     else
      shift 
    fi  
  fi


  if test "$1" = "-utf8" -a "$2" = "-dvipdfm"; then
     if test ! -s "$3.tex"; then
      if test ! -s "$3"; then 
        filename=`find $TEXMFA/tex -follow -name $3 -print`
        if test "$filename" = ""; then
           filename=`find $TEXMFA/tex/ -follow -name $3.tex  -print`
           if test "$filename" = ""; then
              exit
           else
              filenamea=`basename $filename`
           fi
        fi
      else
        filenamea=$3
      fi
     else 
      filenamea=$3.tex
     fi

     if test "$filenamea" = ""; then 
        fname=`echo $filename|cut -d. -f 1`
     else
        fname=`echo $filenamea|cut -d. -f 1`
     fi

     chitex -utf8 $filenamea
#     chitexa -utf8 $3

#     (dvipdfmx -q $fname.dvi 2>\dev\null)
      export LC_CTYPE=zh_TW.Big5
#      exit
     
     (dvipdfmx $fname.dvi)
#exit     
     export LC_CTYPE=zh_TW.Utf8 
     if test -s /usr/bin/evince -o -s /usr/local/bin/evince; then          
         evince $fname.pdf
     else   
       if test -s /usr/local/Adobe/Acrobat7.0/bin/acroread; then     
         acroread $fname.pdf
       else
         if test -s /usr/bin/xpdf; then
            xpdf $fname.pdf
         fi   
       fi   
     fi  
     
     exit
  fi



#PSFONTSMAP=`kpsewhich psfonts.map`
#export PSFONTSMAP

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

#CMAPFONTS=.:$TEXMF/fonts/cmap//

#export TEXPKS
#export CMAPFONTS
#export TEXMF
#export VARTEXFONTS
#export TEXMFCNF
#export TTF2PKINPUTS
#export TTFONTS
#export TTFONTS
export TTF2TFMINPUTS
#export TFMFONTS
#export TEXMFDBS
#export T1FONTS
#export TEXMFDBS

 RUNXDVIA="no"
 export RUNXDVIA



  if test ! -s $HOME/".NotFirstRun"; then
     echo " "; echo " "
     echo "    **********************************************"
     echo "    *  Your TeX document should be saved in Big5 *"
     echo "    *  or in GB and do not saved in UTF-8        *"
     echo "    *                                            *"
     echo "    *  Ignore this message if your TeX document  *"     
     echo "    *  have been in Big5 or GB                   *"
     echo "    **********************************************"             
     echo "     "
     echo "    Press Enter key to continue to compile"
     echo "    or Press 'Ctrl C' to quit              " 

     echo "yes" > $HOME/".NotFirstRun"

     read xxxxxxxxxxxx
  fi   

  if test "$LOGNAME" = "root"; then
     runxdviaa=`grep "RUNXDVIA" /$bindir/xdvi`
     if test "$runxdviaa" = ""; then
        cp -f $bindir/xdvi.org $bindir/xdvi
     fi
     
     if test -s $TEXMFA/fonts/map/dvipdfmx/cid-x.map; then
        umingaa=`grep "uming@" $TEXMFA/fonts/map/dvipdfmx/cid-x.map`  
        if test "$umingaa" = ""; then
           cp -f $TEXMFA/dvipdfm/config/cid-x.map $TEXMFA/fonts/map/dvipdfmx/cid-x.map
           cp -f $TEXMFA/dvipdfm/CMap/* $TEXMFA/fonts/cmap
        fi
     fi   
  fi
  
  rm -f dvipdfm.dat nextrun@ newfile@no
  rm -f plaintex@
    
  if test "$1" = "-utf8" -a "$2" = "-dvipdfm"; then
     dvipdfmutf="y"
     echo "y" > ch@utf8
     shift
  fi
  
  if test "$1" = "-dvipdfm" -a "$2" != "omega"; then
  
#  if test "$1" = "-dvipdfm"; then
     if test ! -s $2.tex
     then
      if test ! -s $2; then 
        filename=`find $TEXMFA/tex -follow -name $2 -print`
        if test "$filename" = ""; then
           filename=`find $TEXMFA/tex/ -follow -name $2.tex  -print`
           if test "$filename" = ""; then
              exit
           else
#              cp -f $filename .   
              filenamea=`basename $filename`
           fi
        fi
      else
        filename=$2
      fi
     else 
      filename=$2.tex
     fi

     if test "$filenamea" = ""; then 
        fname=`echo $filename|cut -d. -f 1`
     else
        fname=`echo $filenamea|cut -d. -f 1`
     fi

     echo "yes" > dvipdfm.dat
  
     if test "$dvipdfmutf" = "y"; then
        dvipdfmutf="y"
        export dvipdfmutf
        chitexa -utf8 $filename
#        chitexorg -utf8 $filename
#        ( dvipdfmx -q $fname.dvi  2>\dev\null)
        export LC_CTYPE=zh_TW.Big5
#read xxxxx        
        ( dvipdfmx $fname.dvi)
        export LC_CTYPE=zh_TW.Utf8
        exit
     else   
#           chitexorg $filename
         chitexcc $filename $fname
         exit
     fi   

     rm -f dvipdfm.dat tmplog@@ tmplog@@@

     if test ! -s Big5.sfd; then     
       ln -s $TEXMFA/Big5.sfd Big5.sfd
     fi  

     if test -s $TEXMFA/big5.sfd; then
        ln -s $TEXMFA/big5.sfd $pwddir/big5.sfd        
     fi   

     if test ! -s UBig5.sfd; then
#        ln -s $TEXMF/UBig5.sfd $pwddir/UBig5.sfd             
        ln -s "$TEXMFA/UBig5.sfd" UBig5.sfd             
     fi   
     cp -f $TEXMFA/dvipdfm/CMap/ETen-B5-UCS2 .

     
     echo "dvipdfmx $fname.dvi" > rundvipdfm@
     chmod +x rundvipdfm@

     ./rundvipdfm@

exit

     if test ! -s $pwddir/unicode.sfd; then     
        ln -s $TEXMFA/unicode.sfd $pwddir/unicode.sfd        
     fi   
     if test ! -s $pwddir/Unicode.sfd; then          
        ln -s $TEXMFA/unicode.sfd $pwddir/Unicode.sfd             
     fi   
     if test ! -s $pwddir/EUC.sfd; then          
        ln -s $TEXMFA/EUC.sfd $pwddir/EUC.sfd             
     fi   
     if test ! -s $pwddir/UBg5plus.sfd; then          
        ln -s $TEXMFA/UBg5plus.sfd $pwddir/UBg5plus.sfd             
     fi   
     if test ! -s $pwddir/UGB.sfd; then          
        ln -s $TEXMFA/UGB.sfd $pwddir/UGB.sfd             
     fi   

#     (dvipdfmx -q $fname.dvi 2>\dev\null)

#     dvipdfmx -q $fname.dvi 2>\dev\null
#        export LC_CTYPE=zh_TW.Big5


#     dvipdfmx -q $fname.dvi

      export LC_CTYPE=zh_TW.Utf8
     ln -s $TEXMFA/dvipdfm/config/cix-map cix-map

     dvipdfmx $fname.dvi
     rm -f *.sfd
      export LC_CTYPE=zh_TW.Utf8
           
     if test -s /usr/bin/evince -o -s /usr/local/bin/evince; then          
         evince $fname.pdf
     else   
       if test -s /usr/local/Adobe/Acrobat7.0/bin/acroread; then     
         acroread $fname.pdf
       else
         if test -s /usr/bin/xpdf; then
            xpdf $fname.pdf
         fi   
       fi   
     fi  
     exit
  fi 


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 '#'



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";;           
   "-chitexupdate") option="-chitexupdate"    
                  opno="1";;           
   "-fonts") option="-fonts"    
                  opno="1";;           
   "-fontstyle") option="-fontstyle"    
                  opno="1";;           
   "-chitexconfig" ) option="-chitexconfig"
                     opno="1";;
   "-pdftex") option="-pdftex"
              opno="1";;
   "-omega") option="-omega"
              opno="5";;              
   "-utf8") option="-utf8"
              opno="1";;              
#   "-xetex") option="-xetex"
#              opno="6";;                         
   "-w") option="-w"
              opno="1";;              
   "-kile") option="-kile"
              opno="1";;              
   *) option="none"
      code="big5"
      opno="0";;
esac

if test -s /usr/bin/kile -o -s /usr/local/bin/kile; then
 if test "$option" = "-kile"; then
   if test -s $2; then
      kile $2
   else
      if test -s $2.tex; then
         kile $2.tex
      else
         kile
      fi   
   fi   
   exit
 fi
fi 

if test -s /usr/bin/texmaker -o -s usr/local/bin/texmaker; then
 if test "$option" = "-w"; then
   if test -s $2; then
      texmaker $2
   else
      if test -s $2.tex; then
         texmaker $2.tex
      else
         texmaker
      fi   
   fi   
   exit
 fi
fi 
    

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

if test "$option" = "-fontstyle"; then
   clear
   echo " "; echo " "
   echo "    AiϥΤUCrO"
   echo " "
   echo "        \\it          % Oᤤ嬰 "
   echo "        \\bf          % Oᤤ嬰"
   echo "        \\chbfit      % Oᤤ嬰ʱ "
   echo " "
   exit
fi


if test "$option" = "-fonts"; then
   clear
   echo " "; echo " "
   echo "    Aܤ֥iϥΤUCrO:"
   echo "        \\uming    % 幩ө"
   echo "        \\ukai     % 幩"   
   echo "        \\MOESUNG  % Ш| 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 "        MORE ..., Press Return key to continue"
   read xxxxx

   clear
   echo " "
   echo "        \\hannomext %Han Nom B (Ext-B XRr)"
   echo "          (rΩﶵ -utf8 sĶ, Y chitex -utf8 OsĶ)"
   echo "          (ݥܺ:"
   echo " http://sourceforge.net/project/showfiles.php?group_id=153105&package_id=172061&release_id=376968"
   echo "           r HannomH.zip, i}o 'Han Nom B.ttf' W hannomb.ttf  b"
   echo "            /usr/share/fonts/truetype  p "
   echo "            /usr/share/texmf/fonts/trutype ؿ )"       
   echo "          (rXѦ:"
   echo "              http://www.chinesecj.com/code/ext-b.php )  "  
   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
 rm -f nextrun@
 rm -f manification@
 rm -f typsize@
 rm -rf tmp00@
 rm -f def@macro
  
  
  
chitexdvipsv(){
   if test "$optvar" = "-xdvi"; then
      command="chitexdvi"
   else
    if test "$optvar" = "-psv" -o "$optvar" = "-dvipsgv"  -o "$optvar" = "-dvips"; then
       command="chitexpsv"
       if test "$optvar" = "-dvips"; then
          command="dvips"
       fi
    else
       command="ex"
    fi
   fi
   if test "$command" != "ex"; then
       :
#       shift
#      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


if test "$1" = "-omega" -a "$2" = "-xdvi"; then
   command="ex"
else   
  if test "$1" = "-omega" -a "$2" = "-psv"; then 
     command="ex"
  else
     if test "$1" = "-utf8" -a "$2" = "-xdvi"; then 
        command="ex"
     else
        if test "$1" = "-utf8" -a "$2" = "-psv"; then 
           command="ex"
        else
          if test "$1" = "-utf8" -a "$2" = "-pdftex"; then         
             exit
          else
             chitexdvipsv
          fi  
        fi   
     fi   
  fi   
fi  


   if test "$command" != "ex"; then
      shift
   fi


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

if test "$1" = "-xetex" -o "$2" = "-xetex"; then
   opno="6"
   xetexfile="y"
fi


if test "$1" = "-omega" -o "$2" = "-omega"; then
   opno="5"
fi


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

menu () {
   clear
   echo " "
   echo "  Usage: chitex [option [option]] sourcefile "
   echo " "
   echo "  where sourcefile is a TeX/LaTeX source file, option are:"
#   echo "  option are:"
   echo "  compile        TeX/LaTeX document if with no option "
   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 "  -dvipdfm       with compile + dvipdfm + acroread "
   if test ! -s /usr/bin/lambda; then   
      echo "  -omega          compile for Utf8 document in aleph"
      echo "  -omega -dvipdfm compile for Utf8 document in aleph"   
   else
      echo "  -omega          compile for document in omega"
      echo "  -omega -dvipdfm compile for document in omega"   
   fi   
   echo "  -utf8          compile for document in Utf8"
   echo "  -utf8 -dvipdfm compile for document in Utf8"   
   echo "  -xelatex       compile with xelatex in Utf8"

   if test -s /usr/bin/texmaker -o -s /usr/local/bin/texmaker; then   
   echo "  -w             call texmaker  "      
   fi   
   if test -s /usr/bin/kile -o -s /usr/local/bin/kile; then   
   echo "  -kile          call klie  "      
   fi   
   if test -s /usr/bin/lyx -o -s /usr/bin/lyx; then   
      echo "  -lyx -chinese  call lyx to edit utf8 file in chinese title"      
      echo "  -lyx -english  call lyx to edit utf8 file in english title"      
      
   fi   

#   echo "  -utf8  -pdftex  compile for document in Utf8"   
   echo "  "
   echo "           MORE ..., Press Return key to continue"
   read xxxxx

   clear

   echo "  -gb      using in GBchinese, this option must appear as the 1st option"
   echo "           using '\GBchinese' in GB chinese 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 "  -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 "  -nonstop  same as '-batch'"

   echo "  -usechitex Give the use of chitex (chitex's command, fonts and "
   echo "             fontstyle command, special macros in chitex,...) "
   echo "  -examples some chitex compile command examples "
   echo "  -chitexhelp  call program 'chitexhelp'"
   echo "  -chitexconfig  call program 'chitexconfig'"
   echo "  -chitexupdate  Update chitex if is supper user"
   echo "  -fonts    help to use default chinese fonts macros"
   echo "  -fontsyle help to use chinese fontstyle macros"
   echo "  -new      message of new version"
   echo "  -help    help"
   echo " "
   exit
}

if test "$1" = "-usechitex"; then
   clear
   echo " ";echo "   "
   echo "   "
   echo "           Now a help document will be read in by 'vi', "
   echo "           after read in, you could exit it by:"
   echo "               Press 'Esc' "
   echo "               Press ':'   "
   echo "               Enter 'q'   "
   echo "     "     
    
   echo "           There three interfaces: "
   echo "           0. UTF-8 "
   echo "           1. Big5 chinese                               "
   echo "           2. GB   chinese                               "
   echo "                                                         "
   echo $echomode "           To choose one of the above, enter '0', '1' or '2': " \
       "$echocr"
   read inkey
  
   if test "$inkey" = "0"; then
        vi $TEXMFA/doc/chitex/README.usechitex.utf
   else
      if test "$inkey" = "1"; then      
         vi $TEXMFA/doc/chitex/README.usechitex
      else          
         vi $TEXMFA/doc/chitex/README.usechitexgb
      fi
   fi   
      exit
   fi

if test "$1" = "-examples"; then
   clear
   echo "  "
   echo "   In the following are examples which you may test each of it"
   echo "   The given tex files are in $TEXMF/tex/chinese. You could "
   echo "   test it anywhere. Or you may give command: chitex -chitexhelp"
   echo "   to test other demo tex files."
   echo " "
   echo "   0. To compile TeX/LaTeX documents, as:"
   echo "         chitex math2  (or chitex math2.tex) "
   echo "      (The command could compile both LaTeX file or plain TeX file,"
   echo "        where math2.tex is a LaTeX file)"
   echo "   1. To compile/preview with xdvi/gv for TeX/LaTeX file, as:"
   echo "         chitex -xdvi math2  (or chitex -psv math2)      "
   echo "   2. To compile/preview with omega/lambda for TeX/LaTeX file, as:"
   echo "         chitex -omega -xdvi math2 (or chitex -omega -psv math2) "
   echo "   3. To compile/preview with omega/lambda by dvipdfm for"
   echo "      TeX/LaTeX file, as:"
   echo "         chitex -omega -dvipdfm math2_dvipdfm"
   echo "   4. To compile/preview TeX/LaTeX file (encoded in utf-8) as:"
   echo "         chitex -utf8 -xdvi testutf (or chitex -utf8 -psv testutf) "
   echo " "
   echo "   5. To compile/preview LaTeX file (encoded in utf-8) as:"
   echo "         chitex -xelatex math2_xetex "
   echo " "

   echo "   MORE ..., Press Return key to continue"

   read xxxxx

   clear

   echo "   6. To compile/preview with pdftex for TeX/LaTeX file, as:"   
   echo "         chitex -pdftex math2_pdftex "
   echo "   7. To compile/preview LaTeX file with makeindex and bibtex, as:"      
   echo "         chitex mabibind  "
   echo "         cmakeindex mabibind "
   echo "         cbibtex mabibind "
   echo "         chitex -xdvi mabibind "
   echo "   8. To compile/preview with xdvi/gv for TeX/LaTeX file, as:"
   echo "         chitex -xdvi math1 "
   echo "      (The demo file math1.tex is a plain TeX file)" 
   echo "   9. To compile/preview with xdvi/gv for TeX/LaTeX file, as:"   
   echo "          chitex -dvipdfm maverb "
   echo "      (The demo file mabverb.tex is to test verbatim like "
   echo "       latex macros used in chitex)"
   echo "  10. To compile/preview with xdvi/gv for TeX/LaTeX file, as:"   
   echo "          chitex -psv charex2 "
   echo "      (The demo file charex2.tex is to show special effect "
   echo "       by using pstricks' package)  "
   echo " "
   echo "  MORE ..., Press Return key to continue"
   read xxxxx

   clear
  
   echo "  11. To compile/preview with xdvi/gv for TeX/LaTeX file, as:"   
   echo "          chitex -xdvi samp2 "
   echo "      (The demo file samp2.tex is to test latex document of"
   echo "       book style using table of contents)"
   echo "  12. Other files in $TEXMF/tex/chinese you can test are:"
   echo "         samp1.tex, samp_quotcha.tex "
   echo "         math2gb.tex, math1gb.tex,..."
   echo " "
   exit
fi


if test "$1" = "-chitexupdate"; then
        chitexupdate
        exit
fi

if test "$1" = "-new"; then
        clear
        echo "    "; echo " "
        echo "    chitex s\Χ:"
        echo "  "
        echo "    A. s\:"
        echo "      1. jTg script, @t"
        echo "      2. chitex [J omega/lambda ( Knuth TeX  extension "
        echo "         J. Plaice and Y. Haralambous ҵoi, ϥ unicode X),"
        echo "         夣X, sĶ|ֳt, nH omega/lambda sĶ, iUOp:"
        echo "                  chitex -omega math2.tex"
        echo "      3. chitex iϥ utf-8 X(isĶ岧r), nsĶip: "
        echo "                  chitex -utf8 testutf.tex"
        echo "        (testutf.tex s utf-8 X(iΦp gedit  editor))  "
#        echo "      4. unix YguW update, |^_ chitex Ӫ]w"
#        echo "      5. chitex |۰ʽuWs(|߰), ]iʽuWs"
#        echo "      5. chitex iHuWs, p:"
#        echo "                  chitex -chitexupdate "
        echo "    "
        echo "    B. :"
        echo "      1. bibtex   makeindex   keyname uର^"
        echo "         (ªiΤ keyname, s@tקאּP^媺"
        echo "          latex @, uର^)"
        echo "      2. (a) w] section-like Oұo title ҧt嬰"
        echo "             YnDi[J@ \input sectitlenonbf.tex"
        echo "         (b) w] headline ҧt嬰, YnDi[J@"
        echo "             \input headlinenonsl.tex"
        echo "    "
#        echo "      (YnHªsĶi chitexold, chilatexold N chitex, chilatex)"        echo " "
#        echo $echomode "     Press Enter key to continue: " $echocr        
   exit
fi


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

if test "$1" = "-new"; then
#   acroread $TEXMF/tex/chinese/math2a_dvipdfm.pdf
   exit
fi

if test "$1" = "-utf8"; then   
 if test "$2" = "-xdvi" -o "$2" = "-psv" -o "$2" = "-pdftex"; then   
   utf8="y"
   export utf8
   echo "y" > ch@utf8   
   shift
   if test "$1" = "-xdvi"; then      
      shift
      command="chitexdvi"
      export command
   fi
   if test "$1" = "-psv"; then
      command="chitexpsv"      
      export command
      shift
   fi
   if test "$1" = "-pdftex"; then      
      exit
#      command=""
#      export command
      echo "y" > ch@utf8   
      pdftex="y"
      pdftexutf8="y"      
      export pdftexutf8
      shift
   fi
 else
   utf8="y"
   export utf8
   echo "y" > ch@utf8   
   shift
      
 fi   
fi

if test "$1" = "-omega"; then 
   omega="y"
   export omega
   shift
   if test "$1" = "-dvipdfm"; then      
      shift
      dvipdfm="y"
      export dvipdfm
   fi
   if test "$1" = "-xdvi"; then      
      shift
      command="chitexdvi"
      export command
   fi
   if test "$1" = "-psv"; then      
      shift
      command="chitexpsv"
      export command
   fi

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 "$1" = "-utf88"; then   
   utf8="y"
   export utf8
   echo "y" > ch@utf8   
   shift
   if test "$1" = "-pdftex"; then
      exit
#      pdftexutf8="y"
#      shift
   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" = "-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
  else
   if test "$1" = "-xetex"; then
      xetex="y"
      export xetex
      shift
   fi   
  fi
 fi 
fi

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


if test "$pdftex" = "y"; then
   virtexbin="pdftex"
else
   if test "$omega" = "y"; then
      virtexbin="lambda"
   else
      virtexbin="latex"
   fi  
fi

if test "$xetex" = "y"; then
   virtexbin="xelatex"
fi

option="0"; opno="0"

if test "$1" = "-e"; then
   option="e"
   opno="1"
else
   if test "$1" = "-batch"; then
        option="batch"
        shift
        optionbatch="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 "$optionbatch" = "batch"; then
   if test "$1" = "-psv"; then
      option="psv"
      command="chitexpsv"
   fi
   if test "$1" = "-xdvi"; then
      option="xdvi"
      command="chitexdvi"      
   fi
   shift   
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



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 $TEXMFA/tex -follow -name $1 -print`
   if test "$filename" = ""; then
      filename=`find $TEXMFA/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=`
$gawkbin -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
#####

nottfcheck="yy"

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   

  if test "$xetex" = "y"; then
   latexfmt="xelatex"
  fi


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

if test "$newchitran" = "y" ; then
  docu=`
  chitexsub1 $filename `
else
  docu=`
  $gawkbin '
BEGIN{
      scr[1]="scrreprt": scr[2]="scrartcl"
      scr[3]="scrbook": scr[4]="scrletter"      
}
{ 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")}
     for (i=1; i<=4; i++)
       {srcprt=instr(inline,"{" src[i] "}")
        if (srcprt > 0)
           {print "komascript"
            break}
       }      
  fi    

  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" = "komascript"; then
   koma="y"
fi


DOCU=$docu
export DOCU

#FILENAME=$filename
#export FILENAME

UTF8=$utf8
export UTF8
OMEGA=$omega
export OMEGA
PDFTEX=$pdftex
export PDFTEX

#  if test -s $filename"@.bak"; then
#     rm -f $filename"@.bak"
#  fi
  
#chaddchitex


# exit

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

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

 /bin/rm -f repl*@ nextrun@

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

  /bin/rm -f NeedUpdate@

testf="n"  

if test "$testf" = "y"; then  
 if test -s $TEXMFA/tex/chinese/latexdate; then
   rm -f NeedUpdate@ 2>/dev/null
   pdfetexnew=`ls -l $bindir/pdfetex`
   texnew=`ls -l $bindir/tex`
   omeganew=`ls -l $bindir/omega`
   echo "yes" > tmp@
   gawk  '
   {
    chinesedir=texmf "/tex/chinese/latexdate"
    while (getline inl < chinesedir > 0)               
      {if (index(inl,"pdfetexdate")>0)
          {eq=index(inl,"="); pdfetexdate=substr(inl,eq+1)}
       if (index(inl,"texdate")>0)
          {eq=index(inl,"="); texdate=substr(inl,eq+1)}
       if (index(inl,"omegadate")>0)
          {eq=index(inl,"="); omegadate=substr(inl,eq+1)}
      }
      
    split(pdfetexnew,a)
    if (length(a[6])==3){a[6]="0" a[6]}
    if (length(a[7])==1){a[7]="0" a[7]}    
    pdfetexdatenew=a[6] a[7]
    split(texnew,a)
    if (length(a[6])==3){a[6]="0" a[6]}
    if (length(a[7])==1){a[7]="0" a[7]}    
    texdatenew=a[6] a[7]
    split(omeganew,a)
    if (length(a[6])==3){a[6]="0" a[6]}
    if (length(a[7])==1){a[7]="0" a[7]}    
    omegadatenew=a[6] a[7]

    if (pdfetexdate!=pdfetexdatenew||texdate!=texdatenew||omegadate!=omegadatenew)
       {print "NeedUpdate" > "NeedUpdate@"}
    
   }' texmf=$TEXMFA pdfetexnew="$pdfetexnew" texnew="$texnew" omeganew="$omeganew" tmp@


   if test -s NeedUpdate@; then
      echo "   It will start to update chinese fmt files "
      echo "  "
      echo "   Press Enter key to continue"
      read xxxxxxx
      chmkfmt

     pdfetex=`ls -l $bindir/pdfetex`
     tex=`ls -l $bindir/tex`
     omega=`ls -l $bindir/omega`
     echo "yes" > tmp@
   
     gawk '
     {chinesedir=texmf "/tex/chinese/latexdate"
      split(pdfetex,a)
      if (length(a[6])==3){a[6]="0" a[6]}
      if (length(a[7])==1){a[7]="0" a[7]}    
      pdfetexdate=a[6] a[7]
      print "pdfetexdate=" pdfetexdate > chinesedir
      split(tex,a)
      if (length(a[6])==3){a[6]="0" a[6]}
      if (length(a[7])==1){a[7]="0" a[7]}    
      texdate=a[6] a[7]
      print "texdate=" texdate > chinesedir    
      split(omega,a)
      if (length(a[6])==3){a[6]="0" a[6]}
      if (length(a[7])==1){a[7]="0" a[7]}    
      omegadate=a[6] a[7]
      print "omegadate=" omegadate > chinesedir    
     }' texmf=$TEXMFA pdfetex="$pdfetex" tex="$tex" omega="$omega" tmp@
      
   fi
 fi

   rm -f "NeedUpdate@"
   rm -f "NeedUpdate2@"   
   cidx==`ls -l $TEXMFA/dvipdfm/config/cid-x.map`
   ttfonts=`ls -l $TEXMFA/ttfonts.map`
   echo "yes" > tmp@
   gawk  '
   {
    split(cidx,a)
    if (length(a[5])<5 || a[4]< "70000")
       {print "NeedUpdate" > "NeedUpdate@"}
    split(ttfonts,a)       
    if (length(a[5])<5 || a[4]< "17000")       
       {print "NeedUpdate" > "NeedUpdate2@"}  
   }' texmf=$TEXMF cidx="$cidx" ttfonts="$ttfonts" tmp@

   if test -s NeedUpdate@; then
      echo "Start to update cid-x.map"
      read xxxxxxxx   
      cp -f $TEXMFA/dvipdfm/config/cid-x.map.org $TEXMFA/dvipdfm/config/cid-x.map 
   fi
   if test -s NeedUpdate2@; then
      echo "Start to update ttfonts.map"
      read xxxxxxxx   
      cp -f $TEXMFA/ttfonts.map.org $TEXMFA/ttfonts.map
   fi

    if test -s $TEXMFA/tex/chinese/datenow; then
       dateold=`cat $TEXMFA/tex/chinese/datenow`
    else 
       dateold=""   
    fi
    datenow=`date`
    rm -f datechange

   if test "$dateold" != ""; then
    gawk '
    { 
      split(dateold,a)
      dateold=a[3]
      if (length(dateold)==1){dateold="0" dateold}
      split(datenow,a)
      datenow=a[3]
      if (length(datenow)==1){datenow="0" datenow}
      if (dateold != datenow)
         {print "datechange" > "datechange"}    
    }' dateold="$dateold" datenow="$datenow" tmp@
    
   fi
fi 

   if test -s /usr/bin/lftp; then
    if test -s datechange -o "$dateold" = ""; then
    :
#       date > $TEXMFA/tex/chinese/datenow
#       chitexupdatebk
    fi
   fi  

  if test "$utf8" = "y"; then
    mv -f dvipdfm2.dat dvipdfm.dat
    chitrancutf $filename
  else
     if test "xetex" = "y"; then
        cp $filename $filea.tmp00
     else
        chitrancc $filename
     fi  
  fi  

  if test -s HaveBg5@; then
     HaveBg5="y"
  fi

  if test -s HaveCJK@; then
     HaveCJK="y"
  fi

  if test -s $filename"@.bak"; then
     mv -f $filename"@.bak" $filename
  fi 

if test "$testf" = "y"; then
  inbigocp=`ls -l $TEXMFA"/omega/ocp/char2uni/inbig5.ocp"`
  inbigocplen=`echo $inbigocp|fgrep "67720"` 
  if test "$inbigocplen" = ""; then
     cp $TEXMFA"/omega/ocp/char2uni/inbig5.ocp" $TEXMFA"/omega/ocp/char2uni/inbig5.ocp.org" 
  fi
fi

  if test -s $filea.log; then
     isomega=`fgrep "This is Omega" $filea.log`
     if test "$isomega" = ""; then
        if test "$omega" = "y"; then
           omegachg="y"
        fi
     else
        if test "$omega" = ""; then
           omegachg="y"
        fi
     fi
     if test "$omegachg" = "y"; then
        rm -f $filea.toc $filea.lot $filea.lof $filea.aux $filea.bbl $filea.ind $filea.idx
     fi
  fi
  
#else
#   chitran $filename
#fi


if test -s tmpmac@; then
   mv -f tmpmac@ tmp@mac
fi

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

if test -s dvipdfm.dat -o "$dvipdfm" = "y" ; then
   dvipdfmdat="y"
   export dvipdfmdat
fi

if test -s chbold@; then
   chbold="y"
   export chold
fi


#if test -s chbold@ -o "$dvipdfmdat" = "y"; then
if test "$HaveCJK" = ""; then
$gawkbin -v chbold=$chbold -v dvipdfmdat=$dvipdfmdat '
BEGIN{}
{print $0 > "maintemp@"
 if (documentclasspos == 0||bgdocumentpos==0)
  {inline=$0
   gsub("\\%","##",inline)
   per=index(inline,"%")
   if (per > 1)
      {inline=substr($0,1,per-1)}
   else
      {inline=$0}
   documentclasspos=index(inline,"\134documentclass")
   bgdocumentpos=index(inline,"\134begin{document")
   if (documentclasspos > 0)
      { if (chbold=="y")
          {print "\\usepackage{chboxit}" > "maintemp@"}
#           print "\\input defchbfp" >"maintemp@"}
#        else
#           {print "\\input defchbfp" >"maintemp@"}
      }
   if (bgdocumentpos > 0)
      {if (dvipdfmdat=="")
#         {}  
         {print "\\input defchbfp" >"maintemp@"}
       else
         {print "\\input defchbf" >"maintemp@"}   
      }
  }
}
END{} '  $filea.tmp00
mv -f maintemp@ $filea.tmp00

fi

#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

if test "$HaveCJK" = ""; then
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
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

#if test ! -s /usr/bin/lambda; then
#   aleph="y"
#fi


compilelatex () {
if test "$option" = "e"; then
   if test "$pdftex" = "y"; then
      ln -s $TEXMFA-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
      if test "$HaveBg5" = "y"; then
         bg5pdflatex $filea.tex
      else
         pdflatex $filea.tmp00
      fi   
      rm -f pdftex.map 2>\dev\null
   else
      if test "$omega" = "y"; then
       if test "$aleph" = "y"; then
         if test "$HaveBg5" = "y"; then
            lamed $filea.tex
         else   
            lamed $filea.tmp00
         fi   
       else  
         if test "$HaveBg5" = "y"; then
            lambda $filea.tex
         else   
            lambda $filea.tmp00
         fi   
       fi  
      else
#         if test "$koma" = "y"; then
#            latex \&chitexls $filea.tmp00
#         else
#            latex \&$latexfmt $filea.tmp00
#         fi   
         if test "$HaveBg5" = "y"; then
            bg5latex $filea.tex
         else   
            latex $filea.tmp00
         fi   
      fi   
   fi   
else
   if test "$option" = "batch" -o "$optionbatch" = "batch"; then
       if test "$pdftex" = "y"; then
          ln -s $TEXMFA-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
         if test "$HaveBg5" = "y"; then
            bg5pdflatex $filea.tex
         else   
            pdflatex $filea.tmp00|cctran 2> /dev/null
         fi   
         rm -f pdftex.map 2>\dev\null
       else
          if test "$omega" = "y"; then       
           if test "$aleph" = "y"; then          
             if test "$HaveBg5" = "y"; then
                lamed $filea.tex
             else   
                lamed $filea.tmp00
             fi
           else
             if test "$HaveBg5" = "y"; then
                lambda $filea.tex
             else   
                lambda $filea.tmp00
             fi
           fi  
          else
             if test "$HaveBg5" = "y"; then
                bg5latex $filea.tex
             else   
                latex $filea.tmp00|cctran 2> /dev/null 
             fi   
          fi    
        fi   
   else
   
     if test "$option" = "nonstop"; then
        if test "$pdftex" = "y"; then
           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map           
#           pdflatex \&$latexfmt $filea.tmp00|cctran 2> /dev/null
           if test "$HaveBg5" = "y"; then
              bg5pdflatex $filea.tex
           else   
              pdflatex $filea.tmp00|cctran 2> /dev/null
           fi   
           rm -f pdftex.map 2>\dev\null           
        else
        
           if test "$omega" = "y"; then       
            if test "$aleph" = "y"; then                     
              if test "$HaveBg5" = "y"; then
                 lamed $filea.tex
              else   
                 lamed $filea.tmp00 2> /dev/null
              fi   
            else
              if test "$HaveBg5" = "y"; then
                 lambda $filea.tex
              else   
                 lambda $filea.tmp00 2> /dev/null
              fi   
            fi  
           else
             if test "$HaveBg5" = "y"; then
                bg5latex $filea.tex
             else   
                latex $filea.tmp00|cctran 2> /dev/null
             fi   
#             fi   
           fi   
        fi
     else
       if test "$pdftexutf8" = "y"; then
          : 
#           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
#           pdflatex \&pdfchitexlu $filea.tmp00|ctran 2> errf@.dat
#           rm -f pdftex.map 2>\dev\null                      
       else
        if test "$pdftex" = "y"; then
           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
           if test "$HaveBg5" = "y"; then
              bg5pdflatex $filea.tex
           else   
#              cp $TEXMF/ttfonts.map . 
              pdflatex $filea.tmp00
           fi   
           rm -f pdftex.map           
        else
           if test "$utf8" = "y"; then
#             if test "$koma" = "y"; then
#                latex \&chitexlus $filea.tmp00
#             else  
#                latex \&chitexlu $filea.tmp00
#             fi   
             if test "$HaveBg5" = "y"; then
                bg5latex $filea.tex
             else   
                latex $filea.tmp00
             fi   
           else
              if test "$omega" = "y"; then     
               if test "$aleph" = "y"; then                                     
                 if test "$HaveBg5" = "y"; then
                    exec lamed $filea.tex
                 else   
                    exec lamed $filea.tmp00
                 fi   
               else
                 if test "$HaveBg5" = "y"; then
                    exec lambda $filea.tex
                 else   
                    exec lambda $filea.tmp00
                 fi   
               fi  
              else
#                 latex \&$latexfmt $filea.tmp00|ctran 2> errf@.dat 
                 if test "$HaveBg5" = "y"; then
                    bg5latex $filea.tex
                 else   
                    latex $filea.tmp00|ctran 2> errf@.dat 
#                    latex $filea.tmp00
                 fi   
##                 latex \&$latexfmt $filea.tmp00 2> errf@.dat 
              fi   
           fi   
        fi
       fi 
     fi
   fi
fi


#exit

#if test "$xetex" = "y"; then
#   xelatex  $filea.tmp00
#fi
}


compilelatexold () {
if test "$option" = "e"; then
   if test "$pdftex" = "y"; then
      ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
      pdflatex  \&$latexfmt $filea.tmp00
      rm -f pdftex.map 2>\dev\null
   else
      if test "$omega" = "y"; then
         lambda $filea.tmp00
      else
         if test "$koma" = "y"; then
            latex \&chitexls $filea.tmp00
         else
            latex \&$latexfmt $filea.tmp00
         fi   
      fi   
   fi   
else
   if test "$option" = "batch" -o "$optionbatch" = "batch"; then
       if test "$pdftex" = "y"; then
          ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
          pdflatex \&$latexfmt $filea.tmp00|cctran 2> /dev/null
          rm -f pdftex.map 2>\dev\null
       else
          if test "$omega" = "y"; then       
             lambda \&ochitexl $filea.tmp00
          else
             if test "$koma" = "y"; then
                latex \&chitexls $filea.tmp00|cctran 2> /dev/null 
             else  
                latex \&$latexfmt $filea.tmp00|cctran 2> /dev/null 
             fi   
#             latex -interaction=errorstopmode \&$latexfmt $filea.tmp00              
          fi    
        fi   
   else
     if test "$option" = "nonstop"; then
        if test "$pdftex" = "y"; then
           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map           
           pdflatex \&$latexfmt $filea.tmp00|cctran 2> /dev/null
           rm -f pdftex.map 2>\dev\null           
        else
           if test "$omega" = "y"; then       
              lambda \&ochitexl $filea.tmp00 2> /dev/null
           else
             if test "$koma" = "y"; then
                latex \&chitexls $filea.tmp00|cctran 2> /dev/null 
             else  
                latex \&$latexfmt $filea.tmp00|cctran 2> /dev/null
             fi   
           fi   
        fi
     else
       if test "$pdftexutf8" = "y"; then
          : 
#           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
#           pdflatex \&pdfchitexlu $filea.tmp00|ctran 2> errf@.dat
#           rm -f pdftex.map 2>\dev\null                      
       else
        if test "$pdftex" = "y"; then
           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
#           pdflatex \&$latexfmt $filea.tmp00|ctran 2> errf@.dat
           pdflatex \&$latexfmt $filea.tmp00 2> errf@.dat
           rm -f pdftex.map           
        else
           if test "$utf8" = "y"; then
             if test "$koma" = "y"; then
                latex \&chitexlus $filea.tmp00
             else  
                latex \&chitexlu $filea.tmp00
             fi   
           else
              if test "$omega" = "y"; then       
                 lambda \&ochitexl $filea.tmp00 2> errf@.dat 
              else
                 latex \&$latexfmt $filea.tmp00|ctran 2> errf@.dat 
#                 latex \&$latexfmt $filea.tmp00 2> errf@.dat 
              fi   
           fi   
        fi
       fi 
     fi
   fi
fi

}

      if test "$noinputenc" = "y"; then
         if test -s tmpfile@; then
            rm -f tmpfile@
         fi
         gawk '
         {inline=$0
          utf=index(inline,"\\usepackage[utf8]")
          if (utf ==  1)
             {inline="%" inline}
          CJK=index(inline,"\\usepackage{CJK}")
          if (CJK != 0)
             {inline="%" inline}
          print inline > "tmpfile@"
         }'  $filea.tmp00
         if test -s tmpfile@; then
            mv -f tmpfile@  $filea.tmp00
         fi 
      fi


compilelatex

  if test "$utf8" = "y"; then
     if test -s $filea".out"; then
        chtoutf $filea".out"
     fi 
     if test -s mtmp@; then   
        iconv -f UTF-8 -t Big5 -o mtmp@ mtmp@     
        mv -f mtmp@ $filea".out"     
     fi   
  fi


    if test "$NoUtf" = "y"; then
       mv -f $bkfile $orfile
    fi

#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

   if test -s $filea.idx; then
      msgind="yes"
#      exit
   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
#     if test "$omega" != "y"; then
        chicodeb $filea
        if test "$utf8" = "y"; then
           iconv -f UTF-8 -t Big5 -o tex@out tex@out
        fi
        mv -f tex@.out $filea".out"
#     fi   
     rm -f *.sfd
  fi


   compilelatex



  if test -s def@macro -a "$dvipdfm" = "y"; then
    if test -s $TEXMFA/Big5.sfd; then
        ln -s $TEXMFA/Big5.sfd $pwddir/Big5.sfd
     else
        if test -s $TEXMFA/big5.sfd; then
           ln -s $TEXMFA/big5.sfd $pwddir/Big5.sfd        
        fi   
     fi   
     ln -s $TEXMFA/UBig5.sfd $pwddir/UBig5.sfd        
#     dvipdfmx -q $filea.dvi 2>\dev\null
     export LC_CTYPE=zh_TW.Big5

#     exit
# echo "HERE"     
     dvipdfmx $filea.dvi
     ln -s $TEXMFA/unicode.sfd $pwddir/unicode.sfd        
     ln -s $TEXMFA/unicode.sfd $pwddir/Unicode.sfd             
     rm -f *.sfd     
     export LC_CTYPE=zh_TW.Utf8
  fi

  fi

  /bin/rm -f dvipdfmdat

   if test "$Havedvipdfm" = "y" -a "$HaveUtf8" = "y"; then
      dvipdfmx $filea".dvi"
      evince $filea".pdf"
   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 main@* def@macro *.ta$ errf@.dat *@*.*
cp .tmp00/*.aux . >/dev/null 2>&1


fileno=
inputno=
labelcount=
bitemcount=
export fileno
export inputno
export labelcount
export bitemcount
rm -f *.sfd newfile@no
rm -f ttfonts.map
rm -f sec*@
rm -f tex@out

fi  #end latex



if test "$docu" = "plaintex" -o "$docu" = "plainamstex" -o "$docu" = "amstex"; then
#  if test "$utf8" = "y"; then
#     exit
#  else

    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 "    **************************************************"

#   fi
   
 plaintex="y"
 export plaintex

 /bin/rm -f typesize@

newchitran="y"

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

  rm -f havemag@

  gawk '
  {inline=$0
   per=index(inline,"%")  
   if (per>0)
      {if (per>1)
        {ch=substr(inline,per-1,1)
         if (ch != "\\")
            {inline=substr(inline,1,per-1)}
        }
       else
        {inline=substr(inline,1,per-1)}   
      }  
   if (bye==0){bye=index(inline,"\\bye")}
   if (end==0){end=index(inline,"\\end")}
   if (enddocument==0){enddocument=index(inline,"\\enddocument")}
print inline > "clog"
   if (index(inline,"\\magnification")>0 && bye==0 && end==0 && enddocument==0)
      {print "y"> "havemag@"}
  }' $filename


 if test ! -s havemag@; then
  gawk '
  BEGIN{
  print "\\magnification 1000" > "mainf@"
  }
  {print $0 > "mainf@"
  }' $filename
  cp -f mainf@ $filename
 fi


  if test "$utf8" = "y"; then
     chitrancutf $filename
  else
     chitrancc $filename
  fi  

  if test -s $filename"@.bak"; then
     mv -f $filename"@.bak" $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 "$HaveCJK" = ""; then

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" = ""  -a "enddoc" = "" ]; 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
fi

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


if test "$HaveCJK" = ""; then
if test "$option" = "e"; then
   if test "$pdftex" = "y"; then   
      ln -s $TEXMFA-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
#      pdftex \&$texfmt $filea.tmp00
      pdftex $filea.tmp00
      rm -f pdftex.map 2>\dev\null           
   else
#      tex \&$texfmt $filea.tmp00
      tex $filea.tmp00
   fi
else
   if test "$option" = "batch" -o "$optionbatch" = "batch"; then
      if test "$pdftex" = "y"; then
         ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
#         pdftex \&$texfmt $filea.tmp00|cctran 2> /dev/null
         pdftex $filea.tmp00|cctran 2> /dev/null
         rm -f pdftex.map 2>\dev\null           
      else
#         tex \&$texfmt $filea.tmp00|cctran 2> /dev/null
         tex $filea.tmp00|cctran 2> /dev/null
      fi   
   else
     if test "$option" = "nonstop"; then
        if test "$pdftex" = "y"; then
            ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map           
#            pdftex \&$texfmt $filea.tmp00|cctran 2> /dev/null
            pdftex $filea.tmp00|cctran 2> /dev/null
            rm -f pdftex.map 2>\dev\null           
        else
#            tex \&$texfmt $filea.tmp00|cctran 2> /dev/null        
            tex $filea.tmp00|cctran 2> /dev/null        
        fi   
     else
       if test "$pdftexutf8" = "y"; then
       :
#           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
#           pdftex \&pdfchitexu $filea.tmp00|ctran 2> errf@.dat
#           rm -f pdftex.map           
       else
        if test "$pdftex" = "y"; then
           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
#           pdftex \&$texfmt $filea.tmp00|ctran 2> errf@.dat
           pdftex $filea.tmp00|ctran 2> errf@.dat
           rm -f pdftex.map 2>\dev\null                      
        else
           if test "$utf8" = "y"; then
#              tex \&chitexu $filea.tmp00
              tex $filea.tmp00
           else
              if test "$omega" = "y"; then       
#                 omega \&ochitex $filea.tmp00 2> errf@.dat 
#                 omega $filea.tmp00 2> errf@.dat 
                 omega $filea.tmp00
              else
#                 tex \&$texfmt $filea.tmp00|ctran 2> errf@.dat 
                 tex $filea.tmp00|ctran 2> errf@.dat 
              fi   
           fi   
        fi
       fi 
     
#        if test "$pdftex" = "y"; then
#           ln -s $TEXMF-var/fonts/map/pdftex/updmap/pdftex_dl14.map pdftex.map   
#           pdftex \&$texfmt $filea.tmp00|ctran 2> errf@.dat
#           rm -f pdftex.map           
#       else
#           tex \&$texfmt $filea.tmp00|ctran 2> errf@.dat    
#       fi    

     fi
   fi
fi
fi

  rm -f plaintex@
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@ *.ta$ errf@.dat *@*.*

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" -o "$optionbatch" = "batch"; 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

 
  if test "$qmark" != "" -o ! -s $filea.log \
    -o "$option" = "e" -o "$option" = "batch" -o "$option" = "nonstop"; then
   :
  else 
#    if test "$command" = "chitexdvi" -a -s $fila.dvi; then
    if test "$command" = "chitexdvi"; then
       xdvi  $filea.dvi &
    else
       if test "$command" = "chitexpsv"; then
          if test "$unixname" = "freebsd"; then
             /bin/csh $bindir/dvipsc $filea.dvi
          else
             dvips $filea.dvi
          fi   
          if test -s /usr/bin/gv; then
             gv $filea.ps &
          else
             evince $filea.ps
          fi   
       else
          if test "$command" = "dvips"; then
             if test "$unixname" = "freebsd"; then          
                /bin/csh $bindir/dvips $filea.dvi             
             else
                dvips $filea.dvi
             fi
          else
             if test "$dvipdfm" = "y" -o "$pdftex" = "y"; then
              if test "$dvipdfm" = "y"; then
                rm -f dvipdfm.dat tmplog@@ tmplog@@@
                if test -s $TEXMFA/Big5.sfd; then
                   ln -s $TEXMFA/Big5.sfd $pwddir/Big5.sfd 2>\dev\null       
                else
                   if test -s $TEXMFA/big5.sfd; then
                      ln -s $TEXMFA/big5.sfd $pwddir/Big5.sfd 2>\dev\null              
                   fi   
                fi   
                ln -s $TEXMFA/UBig5.sfd $pwddir/UBig5.sfd 2>\dev\null       
#                dvipdfmx -q $filea.dvi 2>\dev\null
                export LC_CTYPE=zh_TW.Big5
                dvipdfmx -q $filea.dvi
                ln -s $TEXMFA/unicode.sfd $pwddir/unicode.sfd 2>\dev\null       
                ln -s $TEXMFA/unicode.sfd $pwddir/Unicode.sfd 2>\dev\null                   
                rm -f *.sfd
                export LC_CTYPE=zh_TW.Utf8
             fi
             
              if test "$pdftexfile" = "y" -a "$noinputenc" = "y"; then
                 Noevince="y"
              fi
              
              if test -s $filea.pdf -a "$Noevince" != "y"; then
                if test -s /usr/bin/evince -o -s /usr/local/bin/evince; then          
                   evince $filea.pdf &
                else
                   if test -s /usr/local/Adobe/Acrobat7.0/bin/acroread; then     
                      acroread $filea.pdf &
                   else   
                      if test -s /usr/bin/xpdf; then
                          xpdf $filea.pdf &
                      fi   
                   fi   
                fi  
              fi 
             fi
          fi   
       fi
    fi
  fi

rm -f *.sfd newfile@no

#command="chitexdvi"
  
#  rm -f plaintex@  

rm -f ch@utf8 tmp@

#chfff > /dev/null

#echo "$filename" > FFFname@

chftp > /dev/null

echo "$FFname" > $HOME"/.ctexftp/TexName"


exit

