#!/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 updated  Jan. 20, 2000
# 
#                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"
fi

searchtexmf="y"
if test "$searchtexmf" = "y"; then
echo "yes" > tmp@
texmfcnffile=`kpsexpand -w cnf texmf.cnf`
TEXMFCNF=`
$gawkbin  '
{texmfcnfloc=index(texmfcnffile,"/texmf.cnf")
 print substr(texmfcnffile,1,texmfcnfloc-1)
}' texmfcnffile=$texmfcnffile tmp@ `

echo $TEXMFCNF > tmp@
TEXMF=`
$gawkbin '
{
  inline=$0
  gsub(/web2c/,"",inline)
  inline=substr(inline,1,length(inline)-1)
  print inline
}' tmp@ `
export TEXMF
fi

export fileno
filenoo=`expr $fileno + 1`
#alias gawk='awk'
/bin/rm -f *temp@ maintemp*@ maintmp*@ fileb@ *.@@@ 

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

/bin/cp $filename maintemp@

#$gawkbin -v infile=$filename -v fileno=$fileno '

if test ! -s maintmpb@; then
$gawkbin  '
BEGIN{
firstno=0
 altno=0
}
{
 sww=0
 inline=$0
 while (sww == 0)  
 {
  percent=index(inline,"%")
  if (percent > 0)
     inline=substr(inline,1,percent-1)
  fi       
#
  l1=index(inline,"\\\include {")
  l2=index(inline,"\\\include{")
  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 (l1 > 0 || l2 > 0)
    {lfirst=ll+addnext
     inlinelen=length(inline)
     include=substr(inline,lfirst,inlinelen-lfirst+1)
     llast=index(include,"}")
     if (llast > 0)
      {include=substr(include,l,llast-1)
       spacesw=1
       while (spacesw == 1)
        {llen=length(include)
         if (substr(include,llen,1) == " ")
            include=substr(include,1,llen-1)
         else
            spacesw=0
         fi
        }


   outfilename=include
   slashnum=1
   while (slashnum > 0)
   {slashnum=index(outfilename,"/")
    if (slashnum > 0)
     {outfilename= \
     substr(outfilename,1,slashnum-1)"c"substr(outfilename,slashnum+1,length(outfilename)) 
     }
   }
  replstr=".tmp00/" outfilename

       if (altno == 1)
          {altno=0}
       else
          {altno=1}


        print "if test -s " include ".tex; then" > "transl@." fileno 
        print "if test -s " include ".tex; then" >> "replfileu@" 

       if (firstno == 0)
          {print "   sed \"s#\\\\\134include\{" include "#\\\\\134include\{" \
           replstr"#g\" " infile " > " infile".tmp@ " >> "replfileu@"
           firstno=1
          }
       else
          {if (altno == 0)
           {print "   sed \"s#\\\\\134include\{" include "#\\\\\134include\{" \
            replstr"#g\" " infile".tmp@ > " infile".tmp@@ " >> "replfileu@"}
           else
            {print "   sed \"s#\\\\\134include\{" include "#\\\\\134include\{" \
             replstr"#g\" " infile".tmp@@ > " infile".tmp@ " >> "replfileu@"}
           }

        print "fi" > "replfileu@" 
        print "   inputno=1" > "transl@." fileno
        print "   export inputno" > "transl@." fileno
        print "   chitran " include ".tex" > "transl@." fileno
        print "fi"   > "transl@." fileno      
       fi
      }
     fi
    }
  fi
  inline=substr(inline,lfirst+llast,inlinelen-lfirst-llast+1)
  if (l1 == 0 && l2 == 0)
     sww=1
  fi
 }
}
END{fileno=fileno + 1
if (altno == 0 && firstno ==1)
   {print "mv " infile".tmp@@ " infile".tmp@" >  "replfileu@"}
}' -v infile=$filename -v fileno=$fileno maintemp@

filenoold="$fileno"

fileno="$filenoo"



if test -s transl@.$filenoold; then
   chmod +x ./transl@.$filenoold
   ./transl@.$filenoold
fi

if test -s  replfileu@; then
   chmod +x  replfileu@
   ./replfileu@
fi



rm  -f *.ident@

fi

rm -f tmp@

exit 
