#!/bin/sh

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

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


  bname=`basename $filename`                    

  dirname=`
  ls $filename| \
  gawk -v bname=$bname '
  BEGIN{}
  {basenamepos=index($0,bname)
   dirname=substr($0,1,basenamepos-1)
   periopos=index(dirname,".")
   print dirname
  }
  END{}' `                                       

  filea=`echo $bname|cut -d. -f 1` 
  fileb=`echo $bname |cut -d. -f 2`
  file=$dirname$filea


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


filebb=`echo $bname |sed -n '/\./p'`

  if test "$filebb" = ""
  then
      if test ! -s $file.tex
        then null="1"
      else fileyes="2" 
      fi
  else
      if test $fileb != "tex"
      then
          fileyes="3"
      else
         fileyes="2"
      fi
  fi



includetemp=`sed -n '/\\include/p' $filename`

if test "$includetemp" != ""; then
   chitranu $1
fi


#alias gawk='awk'

rm -f *temp@ 
rm -f maintemp*@ maintmpb@

echo -n "."

   if test -s  $filename.tmp@; then 
      mv  $filename.tmp@  maintemp@
   else
      cp $filename maintemp@
   fi

#   addredefine=`expr $addredefine + 1`
#   export addredefine

#    labelcount=`expr $labelcount + 1`
#    export labelcount

   if [ -s ch@export ]; then
       source ch@export
   fi
   if [ -s ch@exportb ]; then
       source ch@exportb
   fi

#echo "labelcount=" "$labelcount"
#echo "bitemcount=" "$bitemcount"


gawk '
BEGIN{
oldinline=""
inbracelbrno=0
oldfpos=0
verbatim=0
}
{
   inline=$0
   inlineorg=$0
   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   subinline=inline
   percent=index(inline,"%")
   perpos=percent


   perfound=0
   if (percent > 0)
       {
        if (substr(subinline,percent-1,1) == "\\")
           {per=1; subinline=substr(inline,percent+1,length(inline)-percent)
            perfound=0
            while (per !=0 && subinline !="" && perfound ==0)  
            {per=index(subinline,"%")
             if (per > 0)
                { perpos=perpos+per
                  if (substr(subinline,percent-1,1) == "\\")
                     {subinline=substr(subinline,per+1,length(subinline)-per)
                      percent=per
                     }
                  else
                     {perfound=1; break}
                }
             else
                {perfound=0}
            }
           }
        else
           {perfound=1}
       }


   if (perfound == 1)
      {inline=substr(inline,1,perpos-1)}

    if (inline == "")
       {oldfpos=0}


# Find \verbatim
  
   verbatimpos=index(inline,"\\begin{verbatim}")
   endverbatimpos=index(inline,"\\end{verbatim}")
   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")


  endd=0
  fpos=0

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}


 if (verbatimpos > 0)
    {verbatim=1}
 if (endverbatimpos > 0)
    {verbatim=0}

 if (verbatim == 0)
 { 
  j=1
  for (j; j<=2; j++)
  {
   chappos=index(inline,"\\chapter{"); chapposb=index(inline,"\\chapter {")
   secpos=index(inline,"\\section{"); secposb=index(inline,"\\section {")

   subsecpos=index(inline,"\\subsection{");subsecposb= \
      index(inline,"\\subsection {")
   subsubsecpos=index(inline,"\\subsubsection{");subsubsecposb= \
         index(inline,"\\subsubsection {")

   paragraphpos=index(inline,"\\paragraph{")
   paragraphposb=index(inline,"\\paragraph {")
   subparagraphpos=index(inline,"\\subparagraph{")
   subparagraphposb=index(inline,"\\subparagraph {")

   fpos=0
   if (chappos > 0)
     {lbr[1]=chappos+8; fpos=1}
   else
     {if (chapposb > 0)
        {lbr[1]=chappos+9; fpos=1}
      else
        {if (secpos > 0)
          {lbr[1]=secpos+8; fpos=2}
         else
          {if (secposb > 0)
            {lbr[1]=secpos+9; fpos=2}
           else
             {if (subsecpos > 0)
               {lbr[1]=subsecpos+11; fpos=3}
              else
              {if (subsecposb > 0)
                {lbr[1]=subsecposb+12; fpos=3}
               else
                {if (subsubsecpos > 0)
                  {lbr[1]=subsubsecpos+14; fpos=4}
                 else
                   {if (subsubsecposb > 0)
                     {lbr[1]=subsubsecposb+15; fpos=4}
                    else                                                 #
                     {if (paragraphpos > 0)                              #
                         {lbr[1]=paragraphpos+10; fpos=5}                #
                      else                                               #
                         {if (paragraphposb > 0)                         #
                             {lbr[1]=paragraphposb+11; fpos=5}           #
                          else                                           #
                             {if (subparagraphpos > 0)                   #
                                {lbr[1]=subparagraphpos+13; fpos=6}      #
                              else                                       #
                                {if (subparagraphposb > 0)               #
                                    {lbr[1]=subparagraphposb+14; fpos=6} #
                                }  #11                                   #
                             }   #10                                     #
                         }   #9                                          #
                     }   #8                                              #
                   }
                }
              }
             }
          }
        }
     }

   
     if (j == 1)
      {if (oldinline !="")
        {if ((oldfpos != fpos && fpos !=0) || inline == "")
            {print oldinline; break}
         else
            {inline=oldinline inline}
        }

      }



  }

    oldinline=""
    lbrpos=lbr[1]; lbrno=1

    rbrno=0
    if (fpos > 0)
       {linline=substr(inline,1,lbr[1])
        rinline=substr(inline,lbr[1]+1,length(inline)-lbr[1])

        if (rinline !="")   # if rinline="" let it unchanged
        {
         sec=0
         rbrexist=0
         lenn=length(rinline)
         i=1

         for (i; i<=lenn; i++)
             {ch[i]=substr(rinline,i,1)}

         rbrexist=0; rbrno=0 
         i=1
         for (i; i<=lenn; i++) 
         {if (ch[i] > "\240" && ch[i] < "\372")
              {sec=1
               i=i+1
              }             
          else
              {sec=0
               if (ch[i] == "}" && ch[i-1] != "\\")
                  {
                   rbrexist=1
                   rbrpos=i
                   rbrno=1
                   break
                  }
             }
         }
         

         if (rbrexist == 1)    # if exist rbr find lbrno
          {lblock=substr(rinline,1,rbrpos-1)
           if (lenn == rbrpos)
             {rblock="";endd=1}
           else
             {rblock=substr(rinline,rbrpos+1,lenn-rbrpos)}


           if (lblock != "")   # if lblock !=""
           {lenlblock=length(lblock)
            i=1
            for (i; i<=lenlblock; i++) 
            {ch[i]=substr(lblock,i,1)
             if (ch[i] > "\240" && ch[i] < "\372")
                {i=i+1}             
             else
                {if (ch[i] == "{" && ch[i-1] != "\\")
                    {++lbrno}
                }
            }
           }
           else
           {endd=1}             #if lblock="" lbrno=1 never increased


           if (lbrno > rbrno)        
              {   #find rbr "}" in rblock if rblock !=""
               lastrbrpos=0
               if (rblock != "")
                {lenn=length(rblock)
                 i=1
                 for (i; i<=lenn; i++)
                   {ch[i]=substr(rblock,i,1)}

                 bgrbrpos=1; i=1
                 for (i; i<=lenn; i++) 
                 {lbrnewfound=1
                   {if (ch[i] > "\240" && ch[i] < "\372")
                     {i++}             
                    else
                     {#secc=0;
                      newblock=""; lbrnewfound=1
                      if (ch[i] == "}" && ch[i-1] != "\\")
                         {newblock=substr(rblock,bgrbrpos,i-1)
                          lennew=length(newblock)
                          lbrnewfound=0; bgrbrpos=i
                          j=1
                          for (j; j<=lennew; j++)
                          {
                           chnew[j]=substr(newblock,j,1)
                            {if (chnew[j] > "\240" && chnew[j] < "\372")
                                {j++}
                             else
                                {if (chnew[j] == "{" && chnew[j-1] != "\\")
                                  {lbrnewfound=1; break}
                                }
                            }
                          }
                          if (lbrnewfound == 0)
                             {++rbrno;
                              if (lbrno == rbrno)
                                 {lastrbrpos=i;break}
                             }
                         }
                     }
                   }
                 }




                }
              }        #if resulted lbrno=rbrno lastrbrpos>0
           else                     #where lbrno=rbrno=1
              {}

           if (lbrno <= rbrno)
              {if (rblock != "")
                {lenn=length(rblock)
                 righttorbr=substr(rblock,lastrbrpos,lenn-lastrbrpos+1)
                  inbrace=substr(lblock,1,rbrpos-1) "}" 
                 inbrace=inbrace substr(rblock,1,lastrbrpos-1)
                }
               else   #where rblock="" must lbrno=rbrno=1
                {inbrace=substr(lblock,1,rbrpos-1) "}"; righttorbr=""}

               len=length(inbrace)
               inbracestr=""
               i=1
               for (i; i<=len; i++)
                   {ch[i]=substr(inbrace,i,1)}
               i=1
               for (i; i<=len; i++)
               {if (ch[i] > "\240" && ch[i] < "\372")
                   {inbracestr=inbracestr ch[i] ch[i+1]; i++}
                else
                   {if (ch[i] == "\\")
                       {inbracestr=inbracestr "\\protect" ch[i]} 
#                       {inbracestr=inbracestr ch[i]} 
                    else 
                       {inbracestr=inbracestr ch[i]}
                   }
               }

               inline=linline inbracestr righttorbr
               endd=1
              }
           else
              {if (percent == 0)
                  {oldinline=inline " "}
               else 
                  {oldinline=inline}
               endd=1
              }
          }
         else
          {
           {if (percent == 0)         #where rbr not exist, lbrno=1 rbrno=0
               {oldinline=inline " "} #move inline to oldinline, to
            else                      #append to inline of next record
               {oldinline=inline}
            endd=1                    
           }                            
          }

        }
       else                       #where rinline=""
        {
         {if (percent == 0)
             {oldinline=inline " "}
          else 
             {oldinline=inline}
          endd=1
         }
        }
       }
    else
       {endd=1}   # where fpos=0

  }
    if (fpos == 0)
       {
        print inlineorg
        }
    else
       {if (oldinline == "")
           {print inline}
       }
  
}
END{} ' maintemp@|\
gawk -v bitemcount=$bitemcount '
BEGIN{
linecount=bitemcount
count=bitemcount 
bgbitemcount=bitemcount+1
++bitemcount
}
{
   inline=$0
   inlineorg=$0
   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        if (substr(subinline,percent-1,1) == "\\")
           {per=1; subinline=substr(inline,percent+1,length(inline)-percent)
            perfound=0
            while (per !=0 && subinline !="" && perfound ==0)  
            {per=index(subinline,"%")
             if (per > 0)
                { perpos=perpos+per
                  if (substr(subinline,percent-1,1) == "\\")
                     {subinline=substr(subinline,per+1,length(subinline)-per)
                      percent=per
                     }
                  else
                     {perfound=1; break}
                }
             else
                {perfound=0}
            }
           }
        else
           {perfound=1}
       }


   if (perfound == 1)
      {inline=substr(inline,1,perpos-1)}

   verbatimpos=index(inline,"\\begin{verbatim}")
   endverbatimpos=index(inline,"\\end{verbatim}")
   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

 if (verbatimpos > 0)
    {verbatim=1}
 if (endverbatimpos > 0)
    {verbatim=0}

 if (verbatim == 0)
 { 
   i=1
   j=1
   kk=0
   len=length(inline)

  inlineupdate=0
  inl=inline
  inlinenew=""
  bitempos=1
  while (inl != "" && bitempos != 0)
  {
   len=length(inl)
   bitemposa=0
   bitemposb=0
   bitempos=0
   bitemrbpos=0

   bitemposa=index(inl,"\\bibitem{") 
   bitemposb=index(inl,"\\bibitem {")
   bitemposc=index(inl,"\\bibitem[") 

   if (bitemposc > 0)
      {#ltbitem=substr(inl,1,bitemposc-1)
       fsubbitem=substr(inl,bitemposc+9,length(inl)-bitemposc-8)
       roptlbr=index(fsubbitem,"]{")
       if (roptlbr > 0)
          {
           bitemposc=bitemposc+8+roptlbr+1
          }
       else
          {bitemposc=0}
      }

   if (bitemposc > 0)
      {bitempos=bitemposc                                                                           
       bitembgpos=bitemposc+1
      }       
   else
      {if (bitemposa > 0 )                                                       
        {bitempos=bitemposa                                                                           
         bitembgpos=bitemposa+9
        }                                                                      
       else                                                               
        {if (bitemposb > 0 )                                               
         {bitempos=bitemposb
          bitembgpos=bitemposb+10
         }
        }
      }


   if (bitempos > 0)                                                                                
      {
       subbitem=substr(inl,bitembgpos,len-bitembgpos+1)                                          
       ltbitem=substr(inl,1,bitembgpos-1)
       bitemrbpos=0
       subbitemstr=subbitem
       rbrexist=0

          while (subbitemstr !="" && rbrexist == 0)
           {
            bitemrbtmp=index(subbitemstr,"}")
            if (bitemrbtmp > 0)
              {
               prerbr=substr(subbitemstr,1,bitemrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;bitemrbpos=bitemrbpos+bitemrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }

                }
                 if (big5 == 0)
                    {rbrexist=1;bitemrbpos=bitemrbpos+bitemrbtmp}
                 else
                    {subbitemstr= \
             substr(subbitemstr,bitemrbtmp+1,length(subbitemstr)-bitemrbtmp)
                     bitemrbpos=bitemrbpos+bitemrbtmp
                    }
              }
            else
              {
               rbrexist=0; subbitemstr=""
              }              
           }
          

       if (bitemrbpos > 0)
          {linecount=linecount + "1"
           bitemstr=substr(subbitem,1,bitemrbpos-1)                                                 
           lenn=length(bitemstr)
           i=1; chinese=0
  
           for (i; i<=lenn; i++)
               {
                ch=substr(bitemstr,i,1)
                if (ch > "\240" && ch < "\372")
                   {chinese=1;break}
               }
           if (chinese == 1)
             {
               bitemcount=bitemcount + 1 
               i=1; havebitemstr=0
               if (count == 0)
                  {havebitemstr=0}
               else
                  {
                   for (i; i<=count; i++)
                       {
                        if (bitemstrorg[i] == bitemstr)
                           {havebitemstr=1;matchcount=i;break}
                       }
                  } 
               if (havebitemstr == 0)
                  {count=count+1
                   matchcount=count
                   bitemstrorg[count]=bitemstr
                   lencount=length(linecount)
                   bitemstrtmp[count]=bitemcount
                  }

               inlineextra[count]="ch@bib@" bitemstrtmp[matchcount] "%" bitemstr
               inlinenew=inlinenew ltbitem "ch@bib@" \
                  bitemstrtmp[matchcount] "}"
               inlineupdate=1
             }
            
           rhbitem=substr(subbitem,bitemrbpos+1,length(subbitem)-bitemrbpos)                        #               
           inl=rhbitem
          }                                                                                        #
      }                                                                                             #
  }
  
  if (inlineupdate == 1 && chinese == 1)
     {
      inl=inlinenew rhbitem}
  else
     {inl=inline}

 k=1
 for (k; k<=2; k++)
 {if (k == 2)
     {inl=inline}
  inlinenewb=""
  citepos=1
  while (inl != "" && citepos != 0)
  {len=length(inl)
   citeposa=0
   citeposb=0
   citeposc=0
   citepos=0
   citerbpos=0
   
   if (k == 1)
    {citeposa=index(inl,"\\cite{")                                                                   #
     citeposb=index(inl,"\\cite {")                                                                  #
     citeposc=index(inl,"\\cite[")
     addno=0
    }
   else
    {citeposa=index(inl,"\\nocite{")                                                                   #
     citeposb=index(inl,"\\nocite {")
     addno=2
    }
    

   if (citeposc > 0)
      {
       fsubcite=substr(inl,citeposc+6,length(inl)-citeposc-5)
       roptlbr=index(fsubcite,"]{")
       if (roptlbr > 0)
          {
           citeposc=citeposc+5+roptlbr+1
          }
       else
          {citeposc=0}
      }

   if (citeposc > 0)
      {citepos=citeposc                                                                           
       citebgpos=citeposc+1
      }       
   else
      {
       if (citeposa > 0 )                                                                              #
          {citepos=citeposa                                                                           #
           citebgpos=citeposa+6+addno
          }                                                                      #
       else                                                                                             #
          {if (citeposb > 0 )                                                                          #
           {citepos=citeposb
            citebgpos=citeposb+7+addno}                                                                     #
          }                                                                                             #
      }
 
   if (citepos > 0)                                                                                #
      {if (citepos > 1)                                                                            #
          {ltcite=substr(inl,1,citepos-1)}                                                     #
       else                                                                                         #
          {ltcite=""}                                                                              #

       subcite=substr(inl,citebgpos,len-citebgpos+1)                                          #
       ltcite=substr(inl,1,citebgpos-1)

       citerbpos=0
       subcitestr=subcite
       rbrexist=0

          while (subcitestr !="" && rbrexist == 0)
           {
            citerbtmp=index(subcitestr,"}")
            if (citerbtmp > 0)
              {
               prerbr=substr(subcitestr,1,citerbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;citerbpos=citerbpos+citerbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }
                }
                 if (big5 == 0)
                    {rbrexist=1;citerbpos=citerbpos+citerbtmp}
                 else
                    {subcitestr= \
             substr(subcitestr,citerbtmp+1,length(subcitestr)-citerbtmp)
                     citerbpos=citerbpos+citerbtmp
                    }
              }
            else
              {
               rbrexist=0; subcitestr=""
              }              
           }
          
          
       if (citerbpos > 0)                                                                          #
          {citestr=substr(subcite,1,citerbpos-1)                                                 #
           rhcite=substr(subcite,citerbpos+1,length(subcite)-citerbpos)
           comma=1; subbcitestr=citestr
           jj=0
           while (subbcitestr != "" && comma != 0)
           {comma=index(subbcitestr,",")
            ++jj
            if (comma == 0)
               {citecomp[jj]=subbcitestr}
            else
               {citecomp[jj]=substr(subbcitestr,1,comma-1)
                subbcitestr=\
                  substr(subbcitestr,comma+1,length(subbcitestr)-comma)
               }
           }

           j=1
           for (j; j<=jj; j++)
           {i=1
            for (i; i<=count; i++)
               {chinesematch=0
                if (citecomp[j] == bitemstrorg[i])
                   {
                    citestrorg=bitemstrtmp[i]
                    chinesematch=1
                    break 
                    }
               }
           if (j == 1)
              {commastr=""}
           else
              {commastr=","}
           
           if (chinesematch == 1)
              {#inlinenewb=inlinenewb ltcite "\\cite{ch@bib@" citestrorg "}"
               #inlinenewb=inlinenewb ltcite "ch@bib@" citestrorg "}"
               inlinenewb=inlinenewb commastr "ch@bib@" citestrorg
               inlineupdate=3+addno
              }
           else
              {
               lenn=length(citecomp[j])
               i=1; chinese=0
  
               for (i; i<=lenn; i++)
                   {
                    ch=substr(citecomp[j],i,1)
                    if (ch > "\240" && ch < "\372")
                       {chinese=1;break}
                   }
              if (chinese == 1)
               {count=count+1
                linecount=linecount + "1"
                bitemcount=bitemcount+1
                lencount=length(linecount)
                bitemstrtmp[count]=bitemcount
                bitemstrorg[count]=citecomp[j]
                inlineextra[count]="ch@bib@" bitemcount "%" citecomp[j]
                inlinenewb=inlinenewb commastr "ch@bib@" bitemcount

                inlineupdate=3+addno
               }
              else
               {inlinenewb=inlinenewb commastr citecomp[j]}
              }
            inl=rhcite
            if (j == jj)
               {inlinenewb=ltcite inlinenewb "}"}
           } #new for (j; j<=jj; j++) 
         }                                                                                        #
      }                                                                                             #
  }

  if (k == 1)
  {if (inlineupdate == 1)
      {inline=inlinenew rhbitem}
   else
      {if (inlineupdate == 3)
          {inline=inlinenewb rhcite}
      }
  }
  else 
      {if (inlineupdate == 5 )
          {inline=inlinenewb rhcite}
      }
 }  # End of for (k; k<=2; k++)

################################################

       if (inlineupdate == 0)
          {print inlineorg}
       else
          {print inline}
 }
 else
  {print inlineorg}
}
END{  
      i=bgbitemcount
      for (i; i<=count; i++)
      {print inlineextra[i] >> "ch@label"
      }
      print "#!/bin/sh" > "ch@exportb"
      print "export bitemcount=" bitemcount  > "ch@exportb"
}' |\
gawk -v labelcount=$labelcount '
BEGIN{
linecount=labelcount
count=labelcount 
bglabelcount=labelcount+1
++labelcount
}
{
   inline=$0
   sectionline=0
   newinline=""
   newline=""
   bkslash=0
   percent=index(inline,"%")
   subinline=inline
   perpos=percent
   perfound=0

   if (percent > 0)
       {
        if (substr(subinline,percent-1,1) == "\\")
           {per=1; subinline=substr(inline,percent+1,length(inline)-percent)
            perfound=0
            while (per !=0 && subinline !="" && perfound ==0)  
            {per=index(subinline,"%")
             if (per > 0)
                { perpos=perpos+per
                  if (substr(subinline,percent-1,1) == "\\")
                     {subinline=substr(subinline,per+1,length(subinline)-per)
                      percent=per
                     }
                  else
                     {perfound=1; break}
                }
             else
                {perfound=0}
            }
           }
        else
           {perfound=1}
       }


   if (perfound == 1)
      {inline=substr(inline,1,perpos-1)}

   verbatimpos=index(inline,"\\begin{verbatim}")
   endverbatimpos=index(inline,"\\end{verbatim}")
   chprotect=index(inline,"\\chprotect")
   unchprotect=index(inline,"\\unchprotect")

 if (chprotect > 0)
    {verbatim=0}
 if (unchprotect > 0)
    {verbatim=1}

 if (verbatimpos > 0)
    {verbatim=1}
 if (endverbatimpos > 0)
    {verbatim=0}

 if (verbatim == 0)
 { 
   i=1
   j=1
   kk=0
   len=length(inline)

  inlineupdate=0
  inl=inline
  inlinenew=""
  labelpos=1
  while (inl != "" && labelpos != 0)
  {
   len=length(inl)
   labelposa=0
   labelposb=0
   labelpos=0
   labelrbpos=0

   labelposa=index(inl,"\\label{")                                                               
   labelposb=index(inl,"\\label {")                                                              
   
   if (labelposa > 0 )                                                       
      {labelpos=labelposa                                                                           
       labelbgpos=labelposa+7
      }                                                                      
   else                                                               
      {if (labelposb > 0 )                                               
       {labelpos=labelposb
        labelbgpos=labelposb+8}                                                                     
      }                                                                                             

   if (labelpos > 0)                                                                                
      {
       if (labelpos > 1)                                                                            
          {ltlabel=substr(inl,1,labelpos-1)}                                                     
       else                                                                                         
          {ltlabel=""}                                                                              
       sublabel=substr(inl,labelbgpos,len-labelbgpos+1)                                          
       ltlabel=substr(inl,1,labelpos-1)
#       labelrbpos=index(sublabel,"}")
       labelrbpos=0
       sublabelstr=sublabel
       rbrexist=0

          while (sublabelstr !="" && rbrexist == 0)
           {
            labelrbtmp=index(sublabelstr,"}")
            if (labelrbtmp > 0)
              {
               prerbr=substr(sublabelstr,1,labelrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;labelrbpos=labelrbpos+labelrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }

                }
                 if (big5 == 0)
                    {rbrexist=1;labelrbpos=labelrbpos+labelrbtmp}
                 else
                    {sublabelstr= \
             substr(sublabelstr,labelrbtmp+1,length(sublabelstr)-labelrbtmp)
                     labelrbpos=labelrbpos+labelrbtmp
                    }
              }
            else
              {
               rbrexist=0; sublabelstr=""
              }              
           }
          

       if (labelrbpos > 0)
          {linecount=linecount + "1"
           labelstr=substr(sublabel,1,labelrbpos-1)                                                 
           lenn=length(labelstr)
           i=1; chinese=0
  
           for (i; i<=lenn; i++)
               {
                ch=substr(labelstr,i,1)
                if (ch > "\240" && ch < "\372")
                   {chinese=1;break}
               }
           if (chinese == 1)
             {
               labelcount=labelcount + 1 
               i=1; havelabelstr=0
               if (count == 0)
                  {havelabelstr=0}
               else
                  {
                   for (i; i<=count; i++)
                       {
                        if (labelstrorg[i] == labelstr)
                           {havelabelstr=1;matchcount=i;break}
                       }
                  } 
               if (havelabelstr == 0)
                  {count=count+1
                   matchcount=count
                   labelstrorg[count]=labelstr
                   lencount=length(linecount)
                   labelstrtmp[count]=labelcount
                  }

               inlineextra[count]="ch@lb@" labelstrtmp[matchcount] "%" labelstr
#               inlinenew=inlinenew ltlabel "\\protect\\label{ch@lb@" \
               inlinenew=inlinenew ltlabel "\\label{ch@lb@" \
                  labelstrtmp[matchcount] "}"

               inlineupdate=1
             }
           rhlabel=substr(sublabel,labelrbpos+1,length(sublabel)-labelrbpos)                        #               
           inl=rhlabel
          }                                                                                        #
      }                                                                                             #
  }
  
  if (inlineupdate == 1 && chinese == 1)
     {
      inl=inlinenew rhlabel}

 k=1
 for (k; k<=2; k++)
 {if (k ==2)
     {inl=inline}
  inlinenewb=""
  refpos=1
  while (inl != "" && refpos != 0)
  {len=length(inl)
   refposa=0
   refposb=0
   refpos=0
   refrbpos=0

   if (k == 1)
    {refposa=index(inl,"\\ref{");preaddstr="\\ref{"
     refposb=index(inl,"\\ref {");addno=0}
   else
    {refposa=index(inl,"\\pageref{");preaddstr="\\pageref{"                                                     #
     refposb=index(inl,"\\pageref {");addno=4}

   if (refposa > 0 )                                                                              #
      {refpos=refposa
       refbgpos=refposa+5+addno
      }                                                                      #
   else                                                                                             #
      {if (refposb > 0 )                                                                          #
       {refpos=refposb
        refbgpos=refposb+6+addno}
      }                                                                                             #


   if (refpos > 0)                                                                                #
      {if (refpos > 1)                                                                            #
          {ltref=substr(inl,1,refpos-1)}                                                     #
       else                                                                                         #
          {ltref=""}                                                                              #

       subref=substr(inl,refbgpos,len-refbgpos+1)                                          #
       ltref=substr(inl,1,refpos-1)                                                          #

       refrbpos=0
       subrefstr=subref
       rbrexist=0

          while (subrefstr !="" && rbrexist == 0)
           {
            refrbtmp=index(subrefstr,"}")
            if (refrbtmp > 0)
              {
               prerbr=substr(subrefstr,1,refrbtmp)
               len=length(prerbr)
               if (len == 1)
                {
                 rbrexist=1;big5=0 #;refrbpos=refrbpos+refrbtmp
                }
               else
                {i=1; sec=0
                 for (i; i<=len; i++)
                 {labch[i]=substr(prerbr,i,1)
                  if (sec == 1)
                     {sec=0; big5=1}
                  else
                     {if (sec == 0)
                       {if (labch[i] > "\240" && labch[i] < "\372")
                           {sec=1; big5=0}
                        else
                           {big5=0}
                       }
                     }
                 }
                }
                 if (big5 == 0)
                    {rbrexist=1;refrbpos=refrbpos+refrbtmp}
                 else
                    {subrefstr= \
             substr(subrefstr,refrbtmp+1,length(subrefstr)-refrbtmp)
                     refrbpos=refrbpos+refrbtmp
                    }
              }
            else
              {
               rbrexist=0; subrefstr=""
              }              
           }
          
          
#       refrbpos=index(subref,"}")

       if (refrbpos > 0)                                                                          #
          {refstr=substr(subref,1,refrbpos-1)                                                 #
           rhref=substr(subref,refrbpos+1,length(subref)-refrbpos)                        #
           i=1
           chinesematch=0
           for (i; i<=count; i++)
               {
                if (refstr == labelstrorg[i])
                   {
                    refstrorg=labelstrtmp[i]
                    chinesematch=1
                    break 
                    }
               }
           if (chinesematch == 1)
#              {inlinenewb=inlinenewb ltref "\\ref{ch@lb@" refstrorg "}"
              {inlinenewb=inlinenewb ltref preaddstr "ch@lb@" refstrorg "}"
               inlineupdate=3+addno
              }
           else
              {
               lenn=length(refstr)
               i=1; chinese=0
  
               for (i; i<=lenn; i++)
                   {
                    ch=substr(refstr,i,1)
                    if (ch > "\240" && ch < "\372")
                       {chinese=1;break}
                   }
              if (chinese == 1)
               {count=count+1
                linecount=linecount + "1"
                labelcount=labelcount+1
                lencount=length(linecount)
                labelstrtmp[count]=labelcount
                labelstrorg[count]=refstr
                inlineextra[count]="ch@lb@" labelcount "%" refstr
#                inlinenewb=inlinenewb ltref "\\ref{ch@lb@" \
                inlinenewb=inlinenewb ltref preaddstr "ch@lb@" \
                     labelcount "}"
                inlineupdate=3+addno
               }
              }
           inl=rhref
          }                                                                                        #
      }                                                                                             #
  }

 if (k ==1)
  {if (inlineupdate == 1)
    {inline=inlinenew rhlabel} 
   else
    {if (inlineupdate == 3)
        {inline=inlinenewb rhref}
    }
  }
 else
  {if (inlineupdate == 7)
    {inline=inlinenewb rhref}
  }

 }

################################################

   item=index(inline,"\\item[")
   if (item > 0)
      {if (item > 1)
        {lsubline= substr(inline,1,item-1)}
       else
        {lsubline=""}
       subline=substr(inline,item+6,length(inline)-item-5)
       sec=0
       found=0
       sublen=length(subline)
       i=1; big5=0
       for (i; i<=sublen; i++) 
        {fch=substr(subline,i,1)
         if (fch > "\240" && fch < "\372")
            {sec=1; big5=1
             i=i+1
            }             
         else
            {sec=0
             if (fch == "]")
                {found=1
                 content=substr(subline,1,i-1)
                 rsubline=substr(subline,i+1,sublen-i)
                 break
                }
            }
        }
      if (found == 1)
         {print "\\def\\chtempstr{" content "}" # > "maintemppp@"
          print lsubline "\\item[\\chtempstr]" rsubline # > "maintemppp@"
         }
      }
     else
      {
       if (sectionline == 0 && inlineupdate == 0)
          {print $0}
       else
          {
           print inline}
      }
 }
 else
  {print $0}
}
END{  
      i=bglabelcount
      for (i; i<=count; i++)
      {print inlineextra[i] >> "ch@label"
      }
      print "#!/bin/sh" > "ch@export"
      print "export labelcount=" labelcount  > "ch@export"
} ' |\
gawk '
BEGIN{
print "##??!!firstline"
addperc=0
latin=0
outlinelast=""
}
{sww=0
 outline=""  
  {#llen=length($0)
#   crloc=index($0,"
")
    crloc=index($0,"\r")
   if (crloc > 0)
      {inline=substr($0,1,crloc-1) substr($0,crloc+1,length($0)-crloc)}
#      {if ($0 == "
")
#          {inline=" "}
#       else
#          {inline=substr($0,1,crloc-1)
#           if (inline == "")
#              {inline=" "}
#           }
#      }
   else 
      inline=$0
   fi
  }
  leftinlinenew=""
  rightinlinenew=""
  inlinenew=inline
  percent=index(inline,"%")
  {if (percent > 0)
     inline=substr(inline,1,percent-1)
  fi}       
#
  {if (addperc == 0)
     { beginperc=index(inline,"\\addpercent")
       if (beginperc > 0)
          {beginpercprev=1}
     }      
   fi
  }
  {if (latin == 0) 
     {
       beginlatin=index(inline,"\\latin")
      if (beginlatinprev >0)
         {latin=1}
      if (beginlatin > 0)
         {beginlatinprev=1
          latin=1
          endlatinb=index(inlinenew,"\\endlatin")
          leftinlinenew=substr(inlinenew,1,beginlatin-1)
          {if (endlatinb > 0)   
             {rightinlinenew=substr(inlinenew,endlatinb,12) "}"
              rightinlinenew=rightinlinenew substr(inlinenew,endlatinb+12,length(inlinenew)-endlatinb+11)
              inlinenew="{" substr(inlinenew,beginlatin,endlatinb-beginlatin)
              latinendlatin=1
             }
           else
              inlinenew="{" substr(inlinenew,beginlatin,length(inlinenew)-beginlatin+1)
          }
         }
     }
   fi
  }
  {if (beginpercprev > 0)
      {unaddperc=index(inline,"\\unaddpercent")
       if (unaddperc > 0)
          {beginpercprev=0}
       fi
      }
  }

  {if (beginlatinprev > 0)
      {endlatin=index(inline,"\\endlatin")
       if (endlatin > 0)
          {beginlatinprev=0
#           latin=0
          {if (latinendlatin != 1)
              {
         rightinlinenew= substr(inlinenew,endlatin,12) "}"
 rightinlinenew=rightinlinenew substr(inlinenew,endlatin+12,length(inlinenew)-endlatin-11)
              leftinlinenew=""
              inlinenew=substr(inlinenew,1,endlatin-1)
              }              
          }
              
          }
       fi
      }
  }
  {if (beginpercprev > 0 || beginlatinprev > 0)
      {
       if (begintab == 0)
          {begintab=index(inline,"\\chtabverbatim")}
       if (endtab == 0)
          {endtab=index(inline,"\\endchtabverbatim")}
       if (beginverb == 0)
         {beginverb=index(inline,"\\begin{verbatim}")}
       if (endverb == 0)       
          {endverb=index(inline,"\\end{verbatim}")}
       
       if (beginchverv > 0 || begintab > 0 || beginverb > 0)
         {
           if (begintab > 0) 
             {if (endtab > 0)
                 {begintab=0
                  endtab=0
                  if (beginpercprev > 0)
                     addperc=1
                  fi
                  if (beginlatinprev > 0)
                     latin=1
                  fi
                 }
              else
                {if (beginpercprev > 0)
                    addperc=0
                 fi
                 if (beginlatinprev > 0)
                     latin=0
                 fi
                }
             }
          if (beginverb > 0) 
             {
              if (endverb > 0)
                 {beginverb=0
                  endtaverb=0
                  if (beginpercprev > 0)
                      addperc=1
                  fi
                  if (beginlatinprev > 0)                    
                     latin=1
                  fi                
                 }
              else
                {if (beginpercprev > 0)
                    addperc=0
                 fi
                 if (beginlatinprev > 0)                    
                    latin=0
                 fi                
                }
             }
         }
       else
             {if (beginpercprev > 0)
                  {addperc=1}
              if (beginlatinprev > 0)  
                  {latin=1
              fi}                
             }
      }
   else 
    {addperc=0
     latin=0
    }
  }
  {if (latin > 0)
   {str=""
   kl=0
   len=length(inlinenew)
   sec=0
   kk=1
   for (k=1; k<=len;k++)
   {if (sec == 0)
        {
         ch=substr(inlinenew,kk,1)
         kk=k+1
         if (ch > "\240" && ch < "\372")
            {sec=1
             chpost=substr(inlinenew,kk,1)
        if (chpost >= "a" && chpost <= "}" && chpost != "|")
        {
        {if (chpost == "{")                          
          chpost="\233"
        else
        {if (chpost == "}")
           chpost="\235"
         else
         {if (chpost == "\134")
            chpost="\200"
          else
          {if (chpost == "a") 
             chpost="\201"
           else
           {if (chpost == "b") 
              chpost="\202"
            else
            {if (chpost == "c") 
               chpost="\203"
             else
             {if (chpost == "d") 
                chpost="\204"
              else
              { if (chpost == "e") 
                  chpost="\205"
               else
               { if (chpost == "f") 
                  chpost="\206"
                else
                { if (chpost == "g") 
                   chpost="\207"
                 else
                 { if (chpost == "h") 
                    chpost="\210"
                  else
                  { if (chpost == "i") 
                      chpost="\211"
                   else
                   { if (chpost == "j") 
                      chpost="\212"
                    else
                    { if (chpost == "k") 
                       chpost="\213"
                    else
                    { if (chpost == "l") 
                        chpost="\214"
                     else
                     { if (chpost == "m") 
                        chpost="\215"
                     else
                     { if (chpost == "n") 
                         chpost="\216"
                      else
                      { if (chpost == "o") 
                         chpost="\217"
                       else
                       { if (chpost == "p") 
                          chpost="\220"
                        else
                        { if (chpost == "q") 
                           chpost="\221"
                         else
                         { if (chpost == "r") 
                            chpost="\222"
                          else
                          { if (chpost == "s") 
                             chpost="\223"
                           else
                           { if (chpost == "t") 
                              chpost="\224"
                            else
                            { if (chpost == "u") 
                               chpost="\225"
                             else
                             { if (chpost="v") 
                                chpost="\226"
                              else
                              { if (chpost == "w") 
                                 chpost="\227"
                               else
                               {  if (chpost == "x") 
                                   chpost="\230"
                                 else
                                 { if (chpost == "y") 
                                    chpost="\231"
                                  else
                                  { if (chpost == "z") 
                                     chpost="\232"
        }}}}}}}}}}}}}}}}}}}}}}}}}}}}}                          
   }
             kk=kk+1
             str=str ch chpost}
         else
            {str=str ch}   
        }
    else
       {sec=0}
    }
     outline=str
   }
  }   

  {if (addperc > 0)
    {if (latin == 0)
      {outline=inlinenew "
"}
     else
      {rightinlinenew=rightinlinenew "
"}
    }
   else
    {if (latin == 0)
       {outline=inlinenew}
    }
  }

  {if (outlinelast != "")
   {lastchar=substr(outlinelast,length(outlinelast),1)
    if (lastchar != "
")
      {print outlinelast}
    else
       {firstchar=substr(outline,1,1)
        if (firstchar > "\240" && firstchar < "\372")
           {lenoutline=length(outlinelast) 
            kk=lenoutline-1
            i=1
            sec=0
            while (kk > i-1)
                {ch=substr(outlinelast,i,1)
                 i=i+1
                 if (ch > "\240" && ch < "\372")
                    {sec=1
                     i=i+1
                     }
                 else
                     {sec=0}
                 }

            if (sec == 1 && lenoutline >2)
               {lchar=substr(outlinelast,lenoutline-2,2)
                print substr(outlinelast,1,lenoutline-1) "%"
                }
            else
               {print substr(outlinelast,1,lenoutline-1)}
           }
        else
           {outlinelast=substr(outlinelast,1,length(outlinelast)-1)
            print outlinelast}
       }
   }
   else
   {print outlinelast}
   outlinelast=leftinlinenew outline rightinlinenew  
  }

  if (beginlatin > 0) 
     {beginlatin=0
      latinendlatin=0
      }
  if (endlatin > 0)
     {endlatin=0
      latin=0
     }
  
}
END{
print outlinelast
}' | \
gawk '
BEGIN{
 fnum=0
 sww=0 
}
#
{
  {
   print $0
   if (sww == 0)
   {
    l1=index($1,"\\\documentclass")
    if (l1 > 0) 
    {
     if (l1 == 1) 
      {sww=1
       l11=index($1,"%")
       if (l11 > 0) 
          inline=substr($1,1,l11-1)
       else
          inline=$1
       fi
       l12=index(inline,"}")
       inline=substr(inline,1,l12)
       l122=index(inline,"{")      
       inlinefname=substr(inline,l122+1,l12-l122-1)
       if (inlinefname == "article")
          print "\\\usepackage{carticle}"
       else
        if (inlinefname == "report")
          print "\\\usepackage{creport}"
        else
          if (inlinefname == "book")
           print "\\\usepackage{cbook}"
          else
           if (inlinefname == "letter")
            print "\\\usepackage{cletter}"
           else
            if (inlinefname == "slides")
             print "\\\usepackage{cslides}"
            else
              if (inlinefname == "amsart")
               print "\\\usepackage{camsart}"
              else
               if (inlinefname == "amsbook")
                print "\\\usepackage{camsbook}"
               else
                  if (inlinefname == "amsmath")
                    print "\\\usepackage{camsart}"
                  fi
               fi
              fi
            fi
           fi
          fi
        fi
       fi
      }
     fi
    }
    fi
   }
   else
   {
   l3=index($1,"\\\begin{document}")
   if (l3 == 1)
   {
    l2=index($1,"\\\usepackage")
    if (l2 > 0) 
    {
     if (l2 == 1) 
      {
       l21=index($1,"%")
       if (l21 > 0) 
          inline=substr($1,1,l21-1)
       else
          inline=$1
       fi
       l22=index(inline,"}")
       inline=substr(inline,1,l22)
       l222=index(inline,"{")      
       inlinefname=substr(inline,l222+1,l22-l222-1)
       if (inlinefname == "amstex")
          print "\\\usepackage{camstex}"
       fi
      }
     fi
    }
   } 
   fi
   }
   fi 
  }     
}
END{}' | \
gawk '
BEGIN{}
{
 FS="%" 
 fnum=1
 inperc=0
 wholeline=""
 {if (index($0,"\134input") != 0)
   while (fnum <=NF)
   {#print "fnum=" fnum " NF="  NF
   if (substr($fnum,length($fnum),1) != "\134"||inperc ==1)
   {     inperc=1
         fnumsec=fnum+1
         inputpos = index($fnumsec,"\134input")
         if (inputpos != 0)
            {if (inputpos == 1) 
               substrsec = ""
            else
	       substrsec = substr($fnumsec,1,inputpos-1)
            fi
            if (fnum == 1)
                {wholeline=wholeline $fnum "%" substrsec "\134@@!#?@enput" \
                substr($fnumsec,inputpos+6,length($fnumsec)-inputpos-5)}
            else
                {wholeline=wholeline "%" substrsec "\134@@!#?@enput" \
                substr($fnumsec,inputpos+6,length($fnumsec)-inputpos-5)}
            }
         else
            {if (NF == 1)
                wholeline=$0
            else
               {if (fnum == 1)
                  wholeline=wholeline $fnum "%" $fnumsec
               else
                  if (fnum <NF)
                    wholeline=wholeline "%" $fnumsec
                  fi
    	       fi
               }
            fi
            }
	 fi
      }
   else
   wholeline=$0
   fi
    fnum=fnum+1
   }
  else
   wholeline=$0
  fi
  }
  if ($0 != "##??!!firstline") 
     print wholeline > "maintempp@"        
  fi
  }
END{}'

# maintempnext@

#fi

mv  maintempp@ maintemp@

  sed -n '/\\input/p' maintemp@  > klinetemp@



/bin/rm -f trancode@

#/bin/rm -f stexf@

#gawk -v filename=$filename '
#BEGIN{}
#{if (index($0,filename) > 0)
#  {print "yes" > "stexf@"}
#}
#END{}' texfile@




if test ! -s klinetemp@
then 
#  if test -s stexf@; then
  if test "$stexf" != ""; then
     chicode -u maintemp@ > $filea.tmp00
  else
     chicode -u maintemp@ > $outfilename
  fi
  exit

else



#tr "%" "\235" < klinetemp@|sed "s/^/%/" |tr "%" "\012"|tr "\235" "%" > klinebtemp@

tr "%" "\235" < klinetemp@|sed "s/^/%/" |tr "%" "\012"|tr "\235" "%"| \
gawk '
BEGIN{}
{
 FS="\\\134input" 
 fnum=2 
 while (fnum <=NF)
     {print $fnum 
     fnum = fnum + 1
      }
 }
END{}' | \
gawk '
BEGIN {}
{ 
la=index($0,"\{")
firstch = substr($0,1,1)

if (firstch != " " && firstch != "\{")
       {null = 1}
fi

 if (la == 0)
   {sefldch = substr($1,1,1)
    if (sefldfch == "\\" || sefldfch == "%")
        {null = 1}
    else
     { secach = index($1,"\\")
       secbch = index($1,"%")
       if (secach + secbch >0)
        {if (secach + secbch ==2)       
           {if (secach >= secbch)
               {secach = secbch}
            fi
           }
         else 
            {if (secach == 0 )
               {secach = secbch}
             fi
            }
         fi
         srepl = substr($0,1,index($0,$1)+secach-2)
         sfirst = substr($1,1,secach-1)
        }
       else 
          {srepl = substr($0,1,index($0,$1)+length($1)-1)
           sfirst = $1
           }
       fi
     }
    fi
   }

 else

  {   
     sixch = substr($0,1,1)
     if (sixch == "\{")
         {null = 1}
     else 
       { if (sixch == " ")
           {if (substr($1,1,1) == "\{")
               {null = 1}
            else
               {null = 1}
            fi
           }
         else 
            {null = 1}
         fi
       }
      lb=index($0,"\}")
      sfirst=substr($0,la+1,lb-la-1)
      srepl=substr($0,1,lb)
  }
 fi

    slash=index(sfirst,"/")
sfilename=sfirst
#
 if (sfirst == "chitex" || sfirst == "vanilla.sty" \
     || sfirst == "amstex" || sfirst == "chitexl" || sfirst == ""\
     || sfirst == "amstex.tex" || sfirst == "tcilatex.tex"\
     || sfirst == "tcilatex" || sfirst == "ctciltx" || sfirst == "pictex"\
     || sfirst == "ctciltx.tex" || sfirst == "colordvi"\
     || sfirst == "colordvi.tex" \
     || slash > 0 && length(sfirst) == slash)
    {null = 1}
 else
 {
      slash=1
	 lslash=0
      ssfirst=sfirst
      while (slash>0)
      {slash=index(sfirst,"/")
       if (slash > 0)
         {sfirst=substr(sfirst,slash+1,length(sfirst))
          lslash=lsalsh+slash}
       fi
      }

      lc=index(sfirst,".")
      {if (lc > 0)
         {sprev=substr(ssfirst,1,lslash+lc-1)
          ssprev=substr(sfirst,1,lc-1) 
          spost=substr(sfirst,lc+1,length(sfirst)-lc)    
         }
      fi
      }

   outfilename=sfilename
   slashnum=1
   while (slashnum > 0)
   {slashnum=index(outfilename,"/")
    if (slashnum > 0)
     {outfilename= \
     substr(outfilename,1,slashnum-1)"c"substr(outfilename,slashnum+1,length(outfilename)) 
     }
   }
  outfilename=".tmp00/" outfilename
   if (lc == 0)
      {
      {print "#!/bin/sh" > "replacetemp@"} 
      {print "if test ! -s " ssfirst".tex"  > "replacetemp@"}
     {print "then " > "replacetemp@"}
     {print "     null=1 " > "replacetemp@"}
     {print "else" > "replacetemp@"}

     {print "  sed  \"s#\\\\\134input" srepl "#\\\\\134input " \
      outfilename"#g\"  maintemp@ > maintemp2@" > "replacetemp@"}
     {print "   mv -f maintemp2@ maintemp@" > "replacetemp@"}

     {print "fi" > "replacetemp@"}
     {print "if test ! -s " ssfirst".tex"  > "nexttemp@"}
     {print "then " > "nexttemp@"}
     {print "     null=1 " > "nexttemp@"}
     {print "else" > "nexttemp@"}
     {print "     chitran " ssfirst >"nexttemp@"}
     {print "fi" > "nexttemp@"}
 
     }
   else
     {{print "if test ! -s " ssfirst  > "replacetemp@"}
      {print "then " > "replacetemp@"}
      {print "     null=1 " > "replacetemp@"}
      {print "else" > "replacetemp@"}

      {if (spost == "tex") 
       {

        {print "sed  \"s#\\\\\134input" srepl "#\\\\\134input "\
         outfilename"#g\"  maintemp@ > maintemp2@" >"replacetemp@"}
        {print "   mv -f maintemp2@ maintemp@" > "replacetemp@"}
        }
       else
        {

        {print "  sed -n \"/" ssprev".tmp00/p\" maintemp@ >" sfirst".@@@" > "replacetemp@"}
        {print "sed -e \"s#\\\\\134input" srepl "#\\\\\134input "\
         outfilename"#g\"  maintemp@ > maintemp2@" >"replacetemp@"}

        {print "   mv -f maintemp2@ maintemp@" > "replacetemp@"}
        }
       }
     {print "fi" > "replacetemp@"}

     {print "if test ! -s " ssfirst > "nexttemp@"}
     {print "then " > "nexttemp@"}
     {print "     null=1 " > "nexttemp@"}
     {print "else" > "nexttemp@"}
     {print "     chitran " ssfirst >"nexttemp@"}
     {print "fi" > "nexttemp@"}


     }
   fi
 }
}

END{}' 

#ksegtemp@

#   fi

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



sed 's/\\@@!#?@enput/\\input/g' maintemp@ > maintempnext@

mv maintempnext@ maintemp@


#  if test -s stexf@; then
  if test "$stexf" != ""; then
     chicode -u maintemp@ > $filea.tmp00
  else
     chicode -u maintemp@ > $outfilename
  fi


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


  rm -f fileb@
  rm -f *.@@@
fi


exit
     

    
if test -s chseries.tex; then
   if test -s /usr/local/TeX/texmf/tex/chinese/chitex.fdf; then
      cp chseries.tex /usr/local/TeX/texmf/tex/chinese/.chseries.tex
   else
      if test -s /usr/local/lib/texmf/tex/chinese/chitex.fdf; then
        cp chseries.tex /usr/local/lib/texmf/tex/chinese/.chseries.tex
      else
        if test -s /usr/lib/texmf/tex/chinese/chitex.fdf; then
          cp chseries.tex /usr/lib/texmf/tex/chinese/.chseries.tex
        fi
      fi
   fi   
   rm chseries.tex
fi

