#!/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 revised on July 17, 1999
# 
#                All rights preserved   
#

gawkloc=`which gawk|grep ":"`
if test "$gawkloc" = ""; then
   gawkloc=`which gawk|grep "no gawk"`
fi
if test "$gawkloc" != ""; then
   gawkbin="awk"
else
   gawkbin="gawk"
   if test -s /bin/gawk-3.1.4; then
      gawkbin="gawk-3.1.4"
   else    
      gawkbin="gawk"
   fi
   export gawkbin
fi


$gawkbin '
{
 
   inline=$0
   inlinenew=$0
   inlineorg=$0
   subinline=inline
   percent=index(inline,"%")
   perpos=percent

   if (percent > 0)
       {
        gsub("\\\\%","##",subinline)
        percent=index(subinline,"%")
        if (percent > 1)
           {inline=substr(inline,1,percent-1)}
        else
           if (percent == 1)
              {inline=""}
       }


# Find \listing
  
   listingpos=index(inline,"\\begin{listing")
   endlistingpos=index(inline,"\\end{listing")
   verbtabpos=index(inline,"\\begin{verbatimtab")
   endlverbtabpos=index(inline,"\\end{verbatimtab")

   if (listingpos > 0||verbtabpos > 0)
      {listing=1}
   if (endlistingpos > 0||endlverbtabpos > 0)
      {listing=0}


 if (listing > 0)
  {str=""
   kl=0
   len=length(inlinenew)
   sec=0
   kk=1
   k=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 >= "\200" && chpost <= "\232" || chpost == "\234")
            {
            if (chpost == "\234")                          
             chpost="|"
            else
            {if (chpost == "\200")                          
              chpost="~"
             else
            {if (chpost == "\201") 
               chpost="a"
             else
            {if (chpost == "\202") 
                    chpost="b"
             else
            {if (chpost == "\203") 
                   chpost="c"
             else
             {if (chpost == "\204") 
                  chpost="d"
              else
             {if (chpost == "\205") 
                  chpost="e"
              else
             {if (chpost == "\206") 
                  chpost="f"
              else
             {if (chpost == "\207") 
                  chpost="g"
              else
             {if (chpost == "\210") 
                  chpost="h"
              else
             {if (chpost == "\211") 
                  chpost="i"
              else
             {if (chpost == "\212") 
                 chpost="j"
              else
             {if (chpost == "\213") 
                chpost="k"
              else
             {if (chpost == "\214") 
                 chpost="l"
              else
             {if (chpost == "\215") 
                 chpost="m"
              else
             {if (chpost == "\216") 
                 chpost="n"
              else
             {if (chpost == "\217") 
                 chpost="o"
              else
             {if (chpost == "\220") 
                  chpost="p"
              else
             {if (chpost == "\211") 
                 chpost="q"
              else
             {if (chpost == "\222") 
                  chpost="r"
              else
             {if (chpost == "\223") 
                 chpost="s"
              else
             {if (chpost == "\224") 
                 chpost="t"
              else
             {if (chpost == "\225") 
                  chpost="u"
              else
             {if (chpost == "\226") 
                  chpost="v"
              else
             {if (chpost == "\227") 
                  chpost="w"
              else
             {if (chpost == "\230") 
                 chpost="x"
              else
             {if (chpost == "\231") 
                  chpost="y"
              else
             {if (chpost == "\232") 
                 chpost="z"
             }}}}}}}}}} }}}}}}}}}} }}}}}}}  
            }
             kk=kk+1
             str=str ch chpost
         }
         else
            {str=str ch}   
      }
    else
      {sec=0}
   }
     outline=str
  }
 else
  {outline=$0}

  print outline

} ' $1
