#!/bin/sh

#alias gawk='awk'
rm -f *temp@ 
rm -f maintemp*@
 echo -n "."
if test ! -s $1
then
 if test ! -s $1.tex; then 
   filename=`find /usr/share/texmf/tex -name $1 -print0`
   if test "$filename" = ""; then
      filename=`find /usr/share/texmf/tex -name $1.tex -print0`
      if test "$filename" = ""; then
         exit
      fi
   fi
 else
   filename=$1.tex
 fi
else 
 filename=$1
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




gawk '
BEGIN{
print "##??!!firstline"
addperc=0
outlinelast=""
}
{sww=0
 outline=""  
  {
   #crloc=index($0,"")
   crloc=index($0,"\r")
   if (crloc > 0)
      {inline=substr($0,1,crloc-1) substr($0,crloc+1,length($0)-crloc)}
#   if (crloc > 0)
#      {if ($0 == "")
#         {inline=" %%%%% "}
#       else
#         {inline=substr($0,1,crloc-1)}
#      }
   else 
      inline=$0
   fi
  }
  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 (beginpercprev > 0)
      {unaddperc=index(inline,"\\unaddpercent")
       if (unaddperc > 0)
          {beginpercprev=0}
       fi
      }
  }
  {if (beginpercprev > 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
                  addperc=1}
              else
                 addperc=0
             }
          if (beginverb > 0) 
             {
              if (endverb > 0)
                 {beginverb=0
                  endtaverb=0
                  addperc=1}
              else
                 addperc=0
             }
         }
       else
          {addperc=1}
      }
   else 
     addperc=0
  }
  {if (addperc > 0)
    {
     outline=inlinenew ""
    }
  else
     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=outline
   }
}

END{
print outlinelast 
}' $filename| \
gawk '
BEGIN{}
{
 FS="%" 
 fnum=1
 inperc=0
 wholeline=""
 {if (index($0,"\134input") != 0)
   while (fnum <=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{}'

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


if test ! -s klinetemp@; 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" "%" | \
gawk '
BEGIN{kk=0}
{kk=kk+1; 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 "     chitranp " 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 "     chitranp " ssfirst >"nexttemp@"}
     {print "fi" > "nexttemp@"}


     }
   fi
 }
}
END{}'

#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
     
