#!/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 Nov. 22, 2000
# 
#                All rights preserved   
#

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

filename=$1

if test ! -s $filename.tex; then
 if test ! -s $filename; then 
    :
 else
   filename=$filename
 fi
else 
 filename=$filename.tex
fi


echo "yes" > tmp@

newfilename=""
newfilename=`
gawk -v filename=$filename '
{
 llen=length(filename)
 i=1; chinese=0
 for (i;i<=llen;i++)
   {ch=substr(filename,i)
    if (ch > "\240" && ch < "\372")
       {chinese=1; break}
   } 
 if (chinese==1)
    {
     while (getline inline < ".tmp00/texf.tab">0)
      {
       sp=index(inline," ")
       chfilename=substr(inline,1,sp-1)
       if (filename==chfilename)
          {rinline=substr(inline,sp+1)
           newfname=rinline; found=1
          }
      }        
     print newfname
    }
}' tmp@ `  

filea=$filename

if test "$newfilename" != ""; then
   oldfilename=$filea
   bname=`basename $filea `                    
   oldfilea=`echo $bname|cut -d. -f 1` 
   filea=`echo $newfilename|cut -d. -f 1`
else 
   filea=`echo $filea|cut -d. -f 1`
fi


  if test "$filenameext" != "tex" -a "$filenameext" != ""; then
     ext="."$filenameext
  else
     ext=""
  fi

gawkbin="gawk"  
export $gawkbin

$gawkbin ' 
BEGIN{
#print "#!/bin/sh" > "tmp@.ixx"
#print "echo \" yes \" > tmp@" > "tmp@.ixx"
#print gawkbin " \47" > "tmp@.ixx"
#print "BEGIN{" > "tmp@.ixx"
}
{ 
if (NR==1)
{
print "#!/bin/sh" > "tmp@.ixx"
print "echo \" yes \" > tmp@" > "tmp@.ixx"
print gawkbin " \47" > "tmp@.ixx"
print "BEGIN{" > "tmp@.ixx"
} # end of if (NR==1)

   inline=$0
   strhead=1
   outline=""
   kk=0
#   llbrmark=0
   lbr=index(inline,"{")
   llen=length(inline)
   for (i; i<=llen;i++)
       {ch=substr(inline,llen-i+1,1)
        if (ch=="\{")
           {llbrmark="1"; lrbr=llen-i; break}
       }

   indexcontent=substr(inline,lbr+1,lrbr-lbr-1) 
   rline=substr(inline,lrbr+1,llen-lrbr)
   gsub(/!/,"\\\!",indexcontent)
   gsub(/\47/,"\\47",indexcontent)
   gsub(/\42/,"\\42",indexcontent)
   inline="\\indexentry{" indexcontent "}" rline

   while (strhead >0)
    {strhead=index(inline,"\\bc@BG \\ch@WP \\ch@PP{")
     strheada=index(inline,"\\bc@cBG \\ch@WP \\ch@PP{")

     if (strhead==0){strhead=100000}
     if (strheada==0){strheada=100000}
     if (strhead==100000 && strheada==100000 ){strhead=0}

     if (strheada < strhead) 
        {strhead=strheada
         addno=22  
         }
     else
         {addno=21}

     if (strhead > 0)
        {
#         firstbyte=substr(inline,strhead+21,2)
         firstbyte=substr(inline,strhead+addno,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
         if (lchar == "j")
            lchar="0"
         else
            {lchar=tolower(lchar)}

         firstbyte=fchar lchar

         leftstr=substr(inline,1,strhead-1)

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

            secondbyte=rffchar
           }
         else
           {if (secondbyte == "|")
                {secondbyte="\234"}
            else
                {if (secondbyte == "~")
                    {secondbyte="\200"}}   
#                    {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 "print " "\"\\\\\""  outline " > \"tmp@.iii\"" >  "tmp@.ixx"
}                 
END{
print "}" > "tmp@.ixx"
print "END{} \47 tmp@" > "tmp@.ixx"
} ' gawkbin=$gawkbin $filea.idx > /dev/null 2>&1 


chmod +x tmp@.ixx


./tmp@.ixx

cp tmp@.iii $filea.idx


makeindex $filea.idx


inlinenew=$filea.ind

$gawkbin ' 
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"}
                {chpost="\237"}
             else 
                {if (chpost == "\234")
                    {chpost="|"}
                 else  
#                   {if (chpost == "\236")
                   {if (chpost == "\200")
                      {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
/bin/rm -f tmp@.* tmp@

exit

