#!/bin/sh

  filea=`echo $1|cut -d. -f 1` 

gawk ' 
BEGIN{
print "#!/bin/sh" > "tmp@.ixx"
print "echo \" yes \" > tmp@" > "tmp@.ixx"
print "gawk \47" > "tmp@.ixx"
print "BEGIN{" > "tmp@.ixx"


}
{ 
   inline=$0
   strhead=1
   outline=""
   kk=0
   while (strhead >0)
    {strhead=index(inline,"\\bc@BG \\ch@WP \\ch@PP{")
     if (strhead > 0)
        {firstbyte=substr(inline,strhead+21,2)
         fchar=substr(firstbyte,1,1)
         fchar=tolower(fchar)
         lchar=substr(firstbyte,2,1)
         kk=kk+1
         if (lchar == "a")
            lchar="1"
         else
         if (lchar == "b")           
            lchar="2"
         else
         if (lchar == "c")
            lchar="3"
         else
         if (lchar == "d")           
            lchar="4"
         else
         if (lchar == "e")
            lchar="5"
         else
         if (lchar == "f")           
            lchar="6"
         else
         if (lchar == "g")
            lchar="7"
         else
         if (lchar == "h")           
            lchar="8"
         else
         if (lchar == "i")
            lchar="9"
         else
            {lchar=tolower(lchar)}
         fi
         firstbyte=fchar lchar

         leftstr=substr(inline,1,strhead-1)
         rightstr=substr(inline,strhead+24,length(inline)-strhead-23)
         nextpr=index(rightstr,"}")
         secondbyte=substr(rightstr,2,nextpr-2)
         graph=index(secondbyte,"^^")
         if (graph > 0)
           {ffchar=tolower(substr(secondbyte,3,2))
            if (ffchar == "\200")
                rffchar="\\"
            else 
                {rffchar="\\x" ffchar}

            secondbyte=rffchar
           }
         else
           {if (secondbyte == "|")
                {secondbyte="\234"}
            else
                {if (secondbyte == "~")
                    {secondbyte="\236"}}
           }

      outline=outline "\"" leftstr "\"" " \"\\\x" firstbyte "\" " "\"" secondbyte "\" "

         inline=substr(rightstr,nextpr+1,length(rightstr)-nextpr)
        }         
       else
        {if (kk == 0)
            {outline="\"" inline "\""}
         else
            {outline=outline "\"" inline "\""}
         }

     }             
#print "outline=" outline
print "print " "\"\\\\\""  outline " > \"tmp@.iii\"" >  "tmp@.ixx"
}                 
END{
print "}" > "tmp@.ixx"
print "END{} \47 tmp@" > "tmp@.ixx"
} ' $1 >& nul


chmod +x tmp@.ixx


./tmp@.ixx

cp tmp@.iii $1

makeindex $1


#exit

inlinenew=$filea.ind

gawk ' 
BEGIN{
print "\\def\\addmv{\\hskip0pt\\unskip}" > "tmp@.ind"
}
{
  {str=""
   kl=0
   inlinenew=$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 == "\\")
                {chpost="\200"}
             else 
                {if (chpost == "\234")
                    {chpost="|"}
                 else  
                   {if (chpost == "\236")
                      {chpost="~"}}
                 }
             kk=kk+1
             str=str ch chpost
             }
         else
            {str=str ch}   
        }
    else
       {sec=0}
   }

     ll1=index(str,"\\item")
     if (ll1 > 0) 
     {str=substr(str,1,ll1+4) "\\addmv" substr(str,ll1+5,length(str)-ll1-4)}
     outline=str
     print outline > "tmp@.ind"
  }
}   
END{}' $filea.ind

mv tmp@.ind $filea.ind
#rm tmp@.ixx tmp@.iii

exit

