#!/bin/sh
#
#  This is a part of ChiTeX.
#  This script is created by 
#    Hung-Yih Chen ()  (yih@math.ncu.edu.tw)
#               (Dept. of Mathematics, National Central Univ.)
#           Last updated  April. 19, 2000
# 
#                All rights preserved   
#

#quickmode="y"


#gawkbin="gawk"


filename=$1

if test "$filename" = ".tex"; then 
   exit
fi



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


echo "yes" > tmp@

  bname=`basename $filename`                    

  filea=`echo $bname|cut -d. -f 1` 

rm -f maintemp*@ maintmpb@

  echoprompt=`echo -n "."`
  if test "$echoprompt" = "."; then
     echomode="-n ."
  else
     echoprompt=`echo  ".\c"`
     if test "$echoprompt" = "."; then
        echomode=" .\c"
     else 
        echomode="-e .\c"
     fi
  fi

echo $echomode


#cp $filename maintemp@


rm -f *.tmp@@
if test -s "ask@"; then rm -f ask@; fi

if test "$chbold" = ""; then
 if test -s chbold@; then
   chbold=`echo "y"`
   export chbold
 fi
fi


#nochprotect="y"

  /bin/rm -f nextrun@



chitran0nn -v preaddpercent=$preaddpercent -v plaintex=$plaintex -v utf8=$utf8 -v omega=$omega $filename > mainfiletmp@  

#gawk -f /usr/bin/chitran0nn.awk -v preaddpercent=$preaddpercent -v plaintex=$plaintex -v utf8=$utf8 -v omega=$omega $filename > mainfiletmp@  

#gawk -f /usr/bin/chitran0nn3.awk -v omega=$omega mainfiletmp@ > maintemp@

chitran0nn3 -v omega=$omega mainfiletmp@ > maintemp@

#   gawk -f /usr/bin/chitran0nn3.awk mainfiletmp@ > maintemp@

#fi

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


  stexf=""
  
  if test "$stexf" != ""; then
     if test "$omega" != "y"; then
#        sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
#        chicode -u |chicodelistc > $filea.tmp00.tmp@ 
#        sed 's/\\makeatother/\\relax/g' $filea.tmp00.tmp@ > $filea.tmp00

#        sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
        chicode -u maintemp@ |chicodelistc > $filea.tmp00
#        sed 's/\\makeatother/\\relax/g' $filea.tmp00.tmp@ > $filea.tmp00

     else
        sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
        chicodelistc > $filea.tmp00.tmp@ 
        sed 's/\\makeatother/\\relax/g' $filea.tmp00.tmp@ > $filea.tmp00
#        mv -f maintemp@ $filea.tmp00.tmp@
#        sed 's/\\@@!#?@enput/\\input/g' $filea.tmp00.tmp@| \
#        chicode -u |chicodelistc > $filea.tmp00.tmp@         
#        sed 's/\\makeatother/\\relax/g' $filea.tmp00.tmp@ > $filea.tmp00
     fi
     stexf=""
  else
     if test "$omega" != "y" -a "$utf8" != "y"; then  
#        sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
#        chicode -u |chicodelistc > $filea.tmp2@
#        sed 's/\\makeatother/\\relax/g' $filea.tmp2@ > $filea.tmp3@
#        sed 's/\\@@?!lineend@@chinese@@!@\$\*?char//g' $filea.tmp3@ > $filea.tm@
        if test "$HaveBg5" = "y"; then
           bg5conv < maintemp@ > $filea.tex
        else
           chicode -u maintemp@|chicodelistc > $filea.tex
        fi    
#        chicode -u maintemp@|chicodelistc > $filea.tex

#        sed 's/\\makeatother/\\relax/g' $filea.tmp2@ > $filea.tmp3@
#        sed 's/\\@@?!lineend@@chinese@@!@\$\*?char//g' $filea.tmp3@ > $filea.tm@

#        mainat=`echo $filea|fgrep "main@"`
#        if test "$mainat" != ""; then        
#           cp $filea.tm@ $filea.tex
#        fi
         
     else
#         cp maintemp@ $filea.tex
#         chicodelistc maintemp@ > $filea.tex
        sed 's/\\@@!#?@enput/\\input/g' maintemp@  > $filea.tex
#        sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
#        chicodelistc > $filea.tmp2@
#        sed 's/\\makeatother/\\relax/g' $filea.tmp2@ > $filea.tmp3@
#        sed 's/\\@@?!lineend@@chinese@@!@\$\*?char//g' $filea.tmp3@ > $filea.tm@

#       mainat=`echo $filea|fgrep "main@"`
#       if test "$mainat" != ""; then        
#           cp $filea.tm@ $filea.tex
#        fi
        
#        chicodelistc > $outfilename.tmp@
#        sed 's/\\makeatother/\\relax/g' $outfilename.tmp@ > $outfilename.tmp2@
#        sed 's/\\@@?!lineend@@chinese@@!@\$\*?char//g' $outfilename.tmp2@ > $outfilename
     fi   
  fi


  if test -s nextrun@; then
     chmod +x nextrun@
     ./nextrun@
  fi   

  exit

     
