 #!/bin/sh

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

 

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


  cp $filename maintemp@

gawk '
BEGIN{
postverbatim=0
}
{sww=0
 verendbatim=0
 scanchapter=0
 inline=$0
 outline=""  
 while (sww == 0)  
 {
  percent=index(inline,"%")
  if (percent > 0)
     inline=substr(inline,1,percent-1)
  fi       
#
  verbatim=index(inline,"\134begin{verbatim}")
  endverbatim=index(inline,"\134end{verbatim}")
  if (endverbatim > 0)
     {postverbatim=0}
  if (postverbatim == 0)
     {l1=index(inline,"\134chapter {")
      l2=index(inline,"\134chapter{")
      l3=index(inline,"\134chapter[")}
  else
     {l1=0
      l2=0
      l3=0}

 if (verbatim > 0 || endverbatim > 0)
    {verendbatim=1} 
 else
    {verendbatim=0}  

 if (verendbatim == 1)
    {if (l1 < verbatim ||l2 < verbatim ||l3 < verbatim|| \
        l1 > endverbatim ||l2 > endverbatim ||l3 > endverbatim)
       {scanchapter=1}
    }

  if (verendbatim == 0 || scanchapter ==1 || postverbatim == 0)
 {  
  {if (l3 > 0)
      l5=index(inline,"{")
  fi}

  if (l1 == 0 && l2 == 0 && l3 ==0)
     outline=$0
     sww=1
  fi
  if (l1 > 0 && l2 > 0)
    {if (l1 > l2)
        {ll=l2
         addnext=9}
     else
        {ll=l1
         addnext=10}
     fi}
  else
    {
     if (l1 > 0)
       {ll=l1
        addnext=10}
     else
       {ll=l2
        addnext=9
       }
     fi}
  fi

  if (l3 > 0 && ll >0)
     {{if (ll > l3)
        ll=l3
      fi}
      addnext=l5-l3+1
     }
  else
     if (l3 > 0)
        {ll=l3
         addnext=l5-l3+1
        }
     fi
  fi
        
  if (l1 > 0 || l2 > 0 || l3 >0)
    {lfirst=ll+addnext
     inlinelen=length(inline)
     chapter=substr(inline,lfirst,inlinelen)
     llast=index(chapter,"}")
     booka=0
     if (llast > 0)
      {booka=booka+1
       chapter=substr(chapter,l,llast-1)}
       kkey=1
       booka=0
       llast=llast-1
      }
     fi
     inline=substr(inline,lfirst+llast,inlinelen-lfirst-llast+1)
   }
  else
   {sww=1}
    }
  if (l1 > 0 || l2 > 0|| l3 > 0)
     {print $0 > "addmacros@"}
  if (verbatim > 0) 
     {postverbatim=1}
 }
END{}' maintemp@

if test -s addmacros@; then
   chbigdef < addmacros@ > addmacdef@
   chbigpt < addmacros@ > addmacpt@

gawk '
BEGIN{
postverbatim=0
}
{sww=0
 verendbatim=0
 scanchapter=0
 inline=$0
 outline=""  
 replace=0
 while (sww == 0)  
 {
  percent=index(inline,"%")
  if (percent > 0)
     {inline=substr(inline,1,percent-1)}
#  fi       
#
  verbatim=index(inline,"\134begin{verbatim}")
  endverbatim=index(inline,"\134end{verbatim}")
#  print inline
#  print "verbatim,endverbatim = " verbatim endverbatim
  if (endverbatim > 0)
     {postverbatim=0}
  if (postverbatim == 0)
     {l1=index(inline,"\134chapter {")
      l2=index(inline,"\134chapter{")
      l3=index(inline,"\134chapter[")}
  else
     {l1=0
      l2=0
      l3=0}

 if (verbatim > 0 || endverbatim > 0)
    {verendbatim=1} 
 else
    {verendbatim=0}  

 if (verendbatim == 1)
    {if (l1 < verbatim ||l2 < verbatim ||l3 < verbatim|| \
        l1 > endverbatim ||l2 > endverbatim ||l3 > endverbatim)
       {scanchapter=1}
    }
  

 if (verendbatim == 0 || scanchapter == 1 || postverbatim == 0)
 {  
  {if (l3 > 0)
      l5=index(inline,"{")
  fi}

  if (l1 == 0 && l2 == 0)
     outline=$0
     sww=1
#     break
  fi
  if (l1 > 0 && l2 > 0)
    {if (l1 > l2)
        {ll=l2
         addnext=9}
     else
        {ll=l1
         addnext=10}
     fi}
  else
    {
     if (l1 > 0)
       {ll=l1
        addnext=10}
     else
       {ll=l2
        addnext=9
       }
     fi}
  fi

  if (l3 > 0 && ll >0)
     {{if (ll > l3)
        ll=l3
      fi}
      addnext=l5-l3+1
#print "addnext="  addnext
     }
  else
     if (l3 > 0)
        {ll=l3
         addnext=l5-l3+1
        }
     fi
  fi
        
  if (l1 > 0 || l2 > 0 || l3 > 0)
    replace=1
  fi
  inline=substr(inline,lfirst+llast,inlinelen-lfirst-llast+1)
 }
 else
   {sww=1}
    }

  if (replace == 0 || postverbatim == 1)
     {print $0 > "maintempbc@"}
  else
     {getline newrec < "addmacpt@"
      print newrec > "maintempbc@"}
 
  if (verbatim > 0) 
     {postverbatim=1}

 }
END{}' maintemp@
fi

exit
