#!/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 Aug. 17, 1999
# 
#                All rights preserved   
#

gawkloc=`which gawk|grep ":"`
if test "$gawkloc" != ""; then
   gawkbin="awk"
else
   gawkbin="gawk"
fi

export gawkbin

echo "yes" > tmp@
rm -f tmplog@

chlabtr="1"

$gawkbin  '
BEGIN{FS="\\^\\^"
#   chtab["80"]="5c";chtab["81"]="61";chtab["82"]="62";chtab["83"]="63";
   chtab["9f"]="5c";chtab["81"]="61";chtab["82"]="62";chtab["83"]="63";
   chtab["9e"]="5b";chtab["a0"]="5d";chtab["80"]="7e";
   chtab["84"]="64";chtab["85"]="65";chtab["86"]="66";chtab["87"]="67";
   chtab["88"]="68";chtab["89"]="69"
   chtab["8a"]="6a";chtab["8b"]="6b";chtab["8c"]="6c";chtab["8d"]="6d";
   chtab["8e"]="6e";chtab["8f"]="6f";

   chtab["90"]="70";chtab["91"]="71";chtab["92"]="72";chtab["93"]="73";
   chtab["94"]="74";chtab["95"]="75";chtab["96"]="76";chtab["97"]="77";
   chtab["98"]="78";chtab["99"]="79"
   chtab["9a"]="7a";chtab["9b"]="7b";chtab["9c"]="7c";chtab["9d"]="7d"

   ctab["0"]="\\x30";ctab["1"]="\\x31";ctab["2"]="\\x32";ctab["3"]="\\x33";
   ctab["4"]="\\x34";ctab["5"]="\\x35";ctab["6"]="\\x36";ctab["7"]="\\x37";
   ctab["8"]="\\x38";ctab["9"]="\\x39"

   ctab["@"]="\\x40";ctab["A"]="\\x41";ctab["B"]="\\x42";ctab["C"]="\\x43";
   ctab["D"]="\\x44";ctab["E"]="\\x45";ctab["F"]="\\x46";ctab["G"]="\\x47";
   ctab["H"]="\\x48";ctab["I"]="\\x49";ctab["J"]="\\x4a";ctab["K"]="\\x4b";
   ctab["L"]="\\x4c";ctab["M"]="\\x4d";ctab["N"]="\\x4e";ctab["O"]="\\x4f";
   ctab["P"]="\\x50";ctab["Q"]="\\x51";ctab["R"]="\\x52";ctab["S"]="\\x53";
   ctab["T"]="\\x54"
   ctab["U"]="\\x55";ctab["V"]="\\x56";ctab["W"]="\\x57";ctab["X"]="\\x58";
   ctab["Y"]="\\x59";ctab["Z"]="\\x5a";ctab["["]="\\x5b";ctab["\\"]="\\x5c";
   ctab["]"]="\\x5d";ctab["^"]="\\x5e";ctab["_"]="\\x5f";ctab["`"]="\\x60"

# if (chlabtr == 1)
# {
#  i=0
#  while (getline inl < "ch@label" > 0)
#        {++i
#         per=index(inl,"%")
#         enlabel[i]=substr(inl,1,per-1)
#         chlabel[i]=substr(inl,per+1,length(inl)-per)
#        }
#  fileno=i
# }

}
{

if (NR==1)
{
 if (chlabtr == 1)
 {
  i=0
  while (getline inl < "ch@label" > 0)
        {++i
         per=index(inl,"%")
         enlabel[i]=substr(inl,1,per-1)
         chlabel[i]=substr(inl,per+1,length(inl)-per)
        }
  fileno=i
  close("ch@label")
  i=0
  while (getline inl < ".tmp00/texf.tab" > 0)
        {++i
         per=index(inl," ")
         chfname[i]=substr(inl,1,per-1)
         dot=index(chfname[i],".tex")
         if (dot>0)
           {chfname[i]=substr(chfname[i],1,dot-1)}
         enfname[i]=substr(inl,per+1,length(inl)-per)
         dot=index(enfname[i],".tex")
         if (dot>0)
           {enfname[i]=substr(enfname[i],1,dot-1)}
        }
  texffileno=i
 }
} # end of if (NR==1) 

     inlinetmp=inline; newline=""
     CMDSTR="\47"; NEWCMDSTR="\\47"
     while (inlinetmp != "")  
       {cmdpos=index(inlinetmp,CMDSTR)
        if (cmdpos>0)
          {newline=newline substr(inlinetmp,1,cmdpos-1) NEWCMDSTR
           inlinetmp=substr(inlinetmp,cmdpos+length(CMDSTR))
          }
        else
          {newline=newline inlinetmp; inlinetmp=""}
       }
     inline=newline

# gsub("\47","\\47",inline)

 str=""
 fnum=2
 if (NF > 1)
 {while (fnum <= NF)
       {#print "$" fnum "=" $fnum
        thr=substr($fnum,3,1)
        if (length($fnum)>3)
           {fou=substr($fnum,4,1)}
        else
           {fou=""}
        ftwo=substr($fnum,1,2)
        chmore=""
#        fno=fnum % 2
#        if (fno == 1)
            {fremain=substr($fnum,4,length($fnum)-3)}

#          if (ftwo < "80" || ftwo > "9d")
          if (ftwo < "80" || ftwo > "a0")
             { }
          else
             {ftwo=chtab[ftwo]}

            {
             if (ftwo != "^.")
               {str=str "\\x" ftwo}
            }

          thrst=""
          if (thr != "" && thr !=" " && thr != "^")
          {  
#            {if (thr < "@" || thr > "`")
            {if (thr < "0" || thr > "`"|| thr>"9" && thr<"@")
                {}
             else
                {thr=ctab[thr]}
            }
            {if (fou < "0" || fou > "`"|| fou>"9" && fou<"@")
                {}
             else
                {fou=ctab[fou]}
            }
          fremain=substr($fnum,5,length($fnum)-4)
          str=str thr fou fremain
          }

        fnum=fnum+1
       }

    outline=$1 str
 }
  else
 {#print $0 > "temp@"
  outline=$0}

     inlinetmp=outline; newline=""
     CMDSTR="\47"; NEWCMDSTR="\\47"
     while (inlinetmp != "")  
       {cmdpos=index(inlinetmp,CMDSTR)
        if (cmdpos>0)
          {newline=newline substr(inlinetmp,1,cmdpos-1) NEWCMDSTR
           inlinetmp=substr(inlinetmp,cmdpos+length(CMDSTR))
          }
        else
          {newline=newline inlinetmp; inlinetmp=""}
       }
     outline=newline

#  gsub("\47","\\47",outline)

{ inlinetmp=outline
if (chlabtr == 1)
{ j = 1
  for (j; j<=fileno; j++)
      {ll1=index(inlinetmp,enlabel[j])
       if (ll1 > 0)
          {
           gsub(enlabel[j],chlabel[j],inlinetmp)
#           meet=1
          }
      }
}
#   outline=inlinetmp

   
  j = 1
  for (j; j<=texffileno; j++)
      {
       ll1=index(inlinetmp,enfname[j])
       if (ll1 > 0)
          {
           gsub(enfname[j],chfname[j],inlinetmp)
#           meeta=1

          }
      }

   outline=inlinetmp

  tmppos=index(outline,".tmp00")
  if (tmppos > 0)
    {
     lenn=length(oldfilename)
     dot=substr(outline,tmppos+6,1)
     if (dot != "." && dot != "/")
     {  
      i=1; extname=""
      for (i;i<=lenn;i++)
        {ch=substr(oldfilename,lenn-i+1,1)
         if (ch==".")
           {extname=substr(oldfilename,lenn-i+1);break}
        }
      if (extname!="")
        {gsub(".tmp00",extname,outline)}
     }
    }
   
    if (index(outline,".tmp00/")>0)
       {gsub(".tmp00/","",outline)}

}
#

   if (outlinelast == "cr")
      {if (substr(outline,1,2) == "? ")
          {outline=substr(outline,3,length(outline)-2)
           outlinelast="" 
          }
      }    

   if (outline != "" || nulline == "n")    
      {
#       system("$gawkbin \47 " "{print" "\"" outline "\"" "} \47 tmp@ 1>&1")
       system("$gawkbin \47 {print \42" outline "\42 } \47 tmp@ 1>&1")
#       system("$gawkbin \47 {print \42" outline "\42 } \47 tmp@ 2>&1")
#       print outline|"$gawkbin \47 " "{print" "\"" outline "\"" "} \47" 
#      close("$gawkbin \47 " "{print" "\"" outline "\"" "} \47")
#       system("$gawkbin \47 " "{print" "\"" outline "\"" " >> \"tmplog@\"} \47 tmp@ 1>&1")
       }

   llen=length(outline)

   nulline="y"
   for (i=1; i<=llen; i++)
       {ch=substr(outline,i,1)
        if (ch != " " && i < 5 )
           {nulline="n"
            break}
        }


    if (nulline == "y")
    {
          {ch=substr(loutline,1,2)}
       if (ch != "l.")
          {nulline="n"}
     }      

   if (nulline == "y") 
      { 
       #printf "? "
          {system("$gawkbin \47 " "{printf" "\"?"  "\"" "} \47 tmp@")}
#           print "?" >> "tmplog@"
       outlinelast="cr"
       }

    
   loutline=outline   



}
END{
#system("mv tmplog@ " filea".log")
#system("ctranbb " filea".log;mv tmplog@@ " filea".log")
}' filea=$filea chlabtr=$chlabtr oldfilename=$oldfilename $1


exit