#!/bin/sh

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

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


#rm -f maintemp@

chitranu $1




alias gawk='awk'
rm -f *temp@ 
rm -f maintemp*@ maintmpb@


echo -n "."


   if test -s  $filename.tmp@; then 
      cp  $filename.tmp@  maintemp@
      /bin/rm -f $filename.tmp@
   else
      cp $filename maintemp@
   fi


gawk '
BEGIN{
addperc=0
latin=0
}
{sww=0
 outline=""  
  {crloc=index($0,"
")
   if (crloc > 0)
      inline=substr($0,1,crloc-1)
   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}
    }
  }

  {print leftinlinenew outline rightinlinenew > "maintempp@"}

  if (beginlatin > 0) 
     {beginlatin=0
      latinendlatin=0
      }
  if (endlatin > 0)
     {endlatin=0
      latin=0
     }
  
}
END{}' maintemp@

 addperc < maintempp@ > maintemp@


if test ! -s maintmpb@; then
gawk '
BEGIN{
 fnum=0
 sww=0 
}
#
{
  {
   print $0 > "maintmpb@"
   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}" > "maintmpb@"
       else
        if (inlinefname == "report")
          print "\\\usepackage{creport}" > "maintmpb@"
        else
          if (inlinefname == "book")
           print "\\\usepackage{cbook}" > "maintmpb@"
          else
           if (inlinefname == "letter")
            print "\\\usepackage{cletter}" > "maintmpb@"
           else
            if (inlinefname == "slides")
             print "\\\usepackage{cslides}" > "maintmpb@"
            else
              if (inlinefname == "amsart")
               print "\\\usepackage{camsart}" > "maintmpb@"
              else
               if (inlinefname == "amsbook")
                print "\\\usepackage{camsbook}" > "maintmpb@"
               else
                  if (inlinefname == "amsmath")
                    print "\\\usepackage{camsart}" > "maintmpb@"
                  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}" > "maintmpb@"
       fi
      }
     fi
    }
   } 
   fi
   }
   fi 
  }     
}
END{}' maintemp@
cp  maintmpb@ maintemp@
fi

sed -n '/\\input/p' $filename  > klinetemp@


  bname=`basename $filename`                    
  ls $filename > tmp@

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


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

  echo $bname |sed -n '/\./p' > fileb@

  if test ! -s fileb@
  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


gawk '
BEGIN{print "##??!!firstline" > "maintempnext@"}
{
 print $0  > "maintempnext@"
 }
END{}' maintemp@

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 > "maintemp@"        
  fi
  }
END{}' maintempnext@


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


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

/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
     chicode -u maintemp@ > $filea.tmp00
  else
     chicode -u maintemp@ > $outfilename
  fi
  exit

else



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


gawk '
BEGIN{}
{
 FS="\\\134input" 
 fnum=2 
 while (fnum <=NF)
     {print $fnum  > "ksegtemp@"
     fnum = fnum + 1
      }
 }
END{}' klinebtemp@


   if test ! -s ksegtemp@
      then null="1"
   else


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 "     inputno=1" >"nexttemp@"}
     {print "     export inputno"  >"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 "     inputno=1" >"nexttemp@"}
     {print "     export inputno" >"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
     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
     
