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

echo "y" > tmp@

gawkbin="gawk"

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

export TEXMF

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

filename=$1

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


#unadd=`echo $filename|fgrep "@unadd"`
#if test "$unadd" != ""; then
#   addperc=0
#else
#   add=`echo $filename|fgrep "@add"`
#   if test "$add" != ""; then
#      addperc=1
#   fi
#fi
#export addperc

#filename=`echo "$filename"|\
#  sed -e 's#.@unadd##' -e 's#.@add##'`


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

stexf=`
$gawkbin  '
BEGIN{}
{if (index($0,filename) > 0)
  {print "yes"}
 else
  {print ""}
}
END{}' filename=$filename texfile@`

#echo $inputpath".tmp00"

#outfilename=`echo "$filename"|sed 's#/#c#g'`


#if test "$inputpath" != "" -a "$stexf" = "" -a -s $outfilename; then
#   outfilename=$inputpath".tmp00/$outfilename"
#else
#   outfilename=$inputpath".tmp00/$outfilename"
#fi
#if test ! -d $inputpath".tmp00"; then
#   mkdir -p $inputpath".tmp00"
#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

if test -s /usr/bin/dos2unix; then
   dos2unix -n $filename maintemp@ 2> /dev/null
else
   cp $filename maintemp@
fi   

rm -f *.tmp@@


#chitran01  tmpfile@@ 


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


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



/bin/rm -f klinetemp*@

rm -f nextrun@
#nochprotect="y"



#gawk '
#BEGIN{}
#{
#   inline=$0
#   subinline=$0
#   percent=0
#   percent=index(inline,"%")
#   if (percent > 0)
#   {
#     inlinetmp=subinline; 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=""}
#       }
#     subinline=newline
#
##        gsub("\\\\%","##",subinline)
#        percent=index(subinline,"%")
#   }
#   
#
#   if (percent > 0)
#      {#tail="!!@%@!%@ch@tmp%%% " substr(inline,percent)
#       inline=substr(inline,1,percent-1)
#       }
#
#  if (nonautoaddpercent==0)
#     {nonautoaddpercent=index(inline,"\\nonautoaddpercent")}
#
#   verbpos=index(inline,"\\verb")  
#   verbatimpos=index(inline,"\\begin{verbatim")
#   endverbatimpos=index(inline,"\\end{verbatim")
#   boxedverbatimpos=index(inline,"\\begin{boxedverbatim}")
#   endboxedverbatimpos=index(inline,"\\end{boxedverbatim}")
#   listingpos=index(inline,"\\begin{listing")
#   endlistingpos=index(inline,"\\end{listing")
#   verbatimwritepos=index(inline,"\\begin{verbatimwrite}")
#   endverbatimwritepos=index(inline,"\\end{verbatimwrite}")
#   verbatimtabpos=index(inline,"\\begin{verbatimtab}")
#   endverbatimtabpos=index(inline,"\\end{verbatimtab}")
#   chtabverbatimpos=index(inline,"\\chtabverbatim")
#   endchtabverbatimpos=index(inline,"\\endchtabverbatim")
#
#   verbatimcmdpos=index(inline,"\\begin{verbatimcmd}")
#   endverbatimcmdpos=index(inline,"\\end{verbatimcmd}")
#
#   allttpos=index(inline,"\\begin{alltt}")
#   endallttpos=index(inline,"\\end{alltt}")
#
#   chprotect=index(inline,"\\chprotect")
#   unchprotect=index(inline,"\\unchprotect")
#
# if (chprotect > 0)
#    {verbatim=0}
# if (unchprotect > 0)
#    {verbatim=1}
#
# if (verbatimpos > 0)
#    {verbatimmark=verbatimmark+1}
# if (endverbatimpos > 0)
#    {verbatimmark=verbatimmark-1}
#
# if (verbatimtabpos > 0)
#    {verbatimtabmark=verbatimtabmark+1}
# if (endverbatimtabpos > 0)
#    {verbatimtabmark=verbatimtabmark-1}
#
# if (chtabverbatimpos > 0)
#    {chtabverbatimmark=chtabverbatimmark+1}
# if (endchtabverbatimpos > 0)
#    {chtabverbatimmark=chtabverbatimmark-1}
#
# if (boxedverbatimpos > 0)
#    {boxedverbatimmark=boxedverbatimmark+1}
# if (endboxedverbatimpos > 0)
#    {boxedverbatimmark=boxedverbatimmark-1}
#
# if (listingpos > 0)
#    {listingmark=listingmark+1}
# if (endlistingpos > 0)
#    {listingmark=listingmark-1}
#
# if (allttpos > 0)
#    {allttmark=allttmark+1}
# if (endallttpos > 0)
#    {allttmark=allttmark-1}
#
# if (verbatimmark+boxedverbatimmark+listingmark+allttmark+verbatimtabmark+chtabverbatimmark==1)
#    {bgverbatimmark=1}
# else
#    {bgverbatimmark=0}
#
# if (verbatimmark<0||boxedverbatimmark<0||listingmark<0||\
#       allttmark<0||verbatimtabmark<0||chtabverbatimmark<0)
#    {edverbatimmark=-1}
# else
#    {edverbatimmark=1}
#
#
# if (verbpos>0||verbatimpos > 0 || allttpos > 0||verbatimcmdpos > 0||verbatimtabpos > 0 \
#     ||boxedverbatimpos > 0||listingpos > 0|| verbatimwritepos > 0||chtabverbatimpos>0)
#    {verbatim=1}
# if (endverbatimpos > 0||endallttpos > 0||endverbatimcmdpos > 0||endverbatimtabpos > 0\
#      ||endboxedverbatimpos > 0||endlistingpos > 0 ||endverbatimwritepos >0 \
#      ||endchtabverbatimpos >0)
#    {verbatim=0}
#
# if (verbatim==0 && nochexpand==0)
#    { nochexpand=index(inline,"\\nochexpand")
#      if (nochexpand>0)
#         {nochexpand=1; edverbatimmark=0}
#    }
# if (verbatim==0 && chexpand==0)
#    { chexpand=index(inline,"\\chexpand")
#      if (chexpand>0)
#         {nochexpand=0; edverbatimmark=1}
#    }
#
# if (verbatim == 0 && nochexpand==0|| allttmark==1 && nochexpand==0)
#    {
#        if (index(inline,"\\newenvironment") > 0)
#           {
#            if (havechnulcpmmand=="")
#               {havechnulcpmmand="y"
#                print "\\makeatletter"
#                print "\\newcommand{\\ch@nulcommand}{\\relax}"
#                print "\\makeatother"
#               }
#           }
#    }
# print $0
#}
#END{} ' maintemp@ | \

  cp maintemp@ mmmtmp_tmp_tmp
  CVFNAME="mmmtmp_tmp_tmp"
  export CVFNAME
  chaddchitex
  mv $CVFNAME maintemp@


if test -s plaintex@; then
   chitranutf -v plaintex=$plaintex  -v preaddpercent=$preaddpercent maintemp@  > maintemppp@
else   
#gawk -f /usr/bin/chitranutf.awk -v preaddpercent=$preaddpercent maintemp@ > maintemppp@
   chitranutf -v preaddpercent=$preaddpercent maintemp@  > maintemppp@
fi

   
#gawk -f /usr/bin/chitranutf.awk -v preaddpercent=$preaddpercent maintemp@ > maintemppp@

chitranutf2 maintemppp@ > maintempp@
#gawk -f ./chitranutf2.awk -v plaintex=$plaintex  maintemppp@ > maintempp@

if test -s maintempp@; then
   mv -f  maintempp@ maintemp@
fi


  if test "$stexf" != ""; then
     mv maintemp@ $filea.tmp00
  else
     if test "$2" = "input"; then
        mv maintemp@ $filea.tmp@
     else
        mv maintemp@ $filea"tmp@.tex"
     fi    
  fi


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

exit

  /bin/rm -f nexttemp*@ 


if test -s trancode@; then rm -f trancode@; fi
if test -s maintemp2@;then rm -f maintemp2@; fi

if test ! -s klinetemp@ -a ! -s klinetemp3@ -a ! -s klinetemp2@ \
    -a ! -s klinetemp5@ ; then 


  if test "$stexf" != ""; then
#     chicode -u |chicodelistc > $filea.tmp00
     sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
     chicode -u |chicodelistc > $filea.tmp00.tmp@ 
     sed 's/\\makeatother/\\relax/g' $filea.tmp00.tmp@ > $filea.tmp00
     stexf=""
  else
#     chicode -u |chicodelistc > $outfilename
     sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
     chicode -u |chicodelistc > $outfilename.tmp@
     sed 's/\\makeatother/\\relax/g' $outfilename.tmp@ > $outfilename.tmp2@
     sed 's/\\@@?!lineend@@chinese@@!@\$\*?char//g' $outfilename.tmp2@ > $outfilename
  fi
  exit

else

if test -s klinetemp@; then
chitran5 -v inputpath=$inputpath klinetemp@
fi


new="y"
if test "$new" = "y"; then
if test -s klinetemp2@; then
gawk '
BEGIN{print "\\bibliography"}
{
 FS="\\\134bibliography" 
 fnum=2 
 while (fnum <=NF)
     {print $fnum 
     fnum = fnum + 1
      }
 }
END{}' klinetemp2@ | \
chitran5 -v inputpath=$inputpath 
fi


if test -s klinetemp5@; then
awk '
BEGIN{}
{
 FS="\\\134putbib" 
 fnum=2 
 while (fnum <=NF)
     {print $fnum 
     fnum = fnum + 1
      }
 }
END{}' klinetemp5@ | \
chitran7 -v inputpath=$inputpath
fi

fi

chitranno=1
export chitranno

if test -s klinetemp3@; then
chitran6 -v inputpath=$inputpath klinetemp3@
fi

     if test ! -s replacetemp@
     then 
       null="1"
     else
       chmod +x replacetemp@
       ./replacetemp@
     fi


     if test ! -s replacetemp2@
     then 
       null="1"
     else
       chmod +x replacetemp2@
       ./replacetemp2@
     fi



     if test ! -s replacetemp3@
     then 
       null="1"
     else
       chmod +x replacetemp3@
       ./replacetemp3@
     fi


     if test ! -s replacetemp5@
     then 
       null="1"
     else
       chmod +x replacetemp5@
       ./replacetemp5@
     fi


test2="y"
if test "$test2" = "y"; then
# To draw back to relative filename in \include to compatible to clyx

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

  if test "$stexf" != ""; then
    if test "$inputpath" != ""; then
     gawk '
     BEGIN{}
     {outline=$0
     includepos=index($0,"\\include{")
     includeposa=index($0,"\\include {")
     if (includeposa>0){includepos=includeposa}
     if (includepos>0)
       {
        include=substr($0,includepos,length($0)-includepos+1)
        lbr=index(include,"{")
        rbr=index(include,"}")
        include=substr(include,lbr+1,rbr-lbr-1)
        ch=substr(include,length(include),1)
        if (ch=="@")
           {include=substr(include,1,length(include)-1)
            rline=substr(include,rbr+1,length(include)-rbr)
            lline=substr($0,1,includepos-1)
            if (index(include,inputpath)==1)
             {replstr=inputpath
              gsub(replstr,"",include)
#              gsub(".tmp00/","",include)
              outline=lline "\\include{" include "}" rline
             }
           }
       }
       print outline > "maintempincl@"
     }
     END{}' inputpath=$inputpath maintemp@ 
    fi

    if test -s maintempincl@; then
       cp maintempincl@ maintemp@
    fi
  fi
fi


  if test "$stexf" != ""; then
     sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
     chicode -u |chicodelistc > $filea.tmp00.tmp@ 
     sed 's/\\makeatother/\\relax/g' $filea.tmp00.tmp@ > $filea.tmp00
     stexf=""
  else
     sed 's/\\@@!#?@enput/\\input/g' maintemp@| \
     chicode -u |chicodelistc > $outfilename.tmp@
     sed 's/\\makeatother/\\relax/g' $outfilename.tmp@ > $outfilename.tmp2@
     sed 's/\\@@?!lineend@@chinese@@!@\$\*?char//g' $outfilename.tmp2@ > $outfilename
  fi



  if test ! -s nexttemp@
   then null="1"
  else 
    sed 's#.@add#.tex#g' nexttemp@ > nexttemp3@
    mv -f nexttemp3@ nexttemp@
    chmod +x nexttemp@
    ./nexttemp@
#    /bin/rm -f nexttemp@ 
  fi


  if test ! -s nexttemp2@
   then null="1"
  else 
    sed 's#.@add#.tex#g' nexttemp2@ > nexttemp3@
    mv -f nexttemp3@ nexttemp2@
    chmod +x nexttemp2@
    ./nexttemp2@
#    /bin/rm -f nexttemp2@ 
  fi

fi


#rm -f *.@@@

rm -f tmp@

chitranno=1
export chitranno

exit
     
