#!/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   
#


gawkbin="gawk"

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

rm -f *temp@ 
rm -f maintemp*@

  echoprompt=`echo -n "."`
  if test "$echoprompt" = "."; then
     echomode="-n ."
  else
     echoprompt=`echo  ".\c"`
     if test "$echoprompt" = "."; then
        echomode=" .\c"
     else 
        echomode="-e .\c"
     fi
  fi

echo $echomode

   
filename=$1

if test "$filename" = ".tex"; then 
   exit
fi

unadd=`echo $filename|fgrep "@unadd"`
if test "$unadd" != ""; then
   addperc=0
else
   add=`echo $filename|fgrep "@add"`
   if test "$add" != ""; then
      addperc=1
   fi
fi
export addperc

filename=`echo "$filename"|\
  sed -e 's#.@unadd##' -e 's#.@add##'`


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


outfilename=`echo "$filename"|sed 's#/#c#g'`
outfilename=".tmp00/$outfilename"

  bname=`basename $filename`                    


  filea=`echo $bname|cut -d. -f 1` 
#  fileb=`echo $bname |cut -d. -f 2`
#  file=$dirname$filea


stexf=`
$gawkbin -v filename=$filename '
BEGIN{}
{if (index($0,filename) > 0)
  {print "yes"}
 else
  {print ""}
}
END{}' texfile@`



#filebb=`echo $bname |sed -n '/\./p'`

if test -s typesize@; then
   /bin/rm -f typesize@
fi
if test -s havemag@; then
   /bin/rm -f havemag@
fi


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


chitran0 $filename | \
gawk -f chitran1.awk -v chitranno=$chitranno \
     -v nochprotect=$nochprotect | \
gawk -f chitran11.awk | \
#chitran1  -v nochprotect=$nochprotect | \
gawk -f chitranp1.awk -v chitranno=$chitranno \
   -v addperc=$addperc -v beginpercprev=$beginpercprev  | \
chitranp2 


#typesizea=`cat typesize@`
#havemaga=`cat havemag@`

if test ! -s typesize@ -o ! -s havemag@; then
   echo "%" > addmagfile@
else
#   echo "\134chtypesize" > addmagfile@
   echo "\\chtypesize" > addmagfile@
fi

if test ! -s maintempre@; then
   echo "%" > maintempre@
fi
if test ! -s  maintempost@; then
  echo "%" > maintempost@
fi

sed 's/\\134chtypesize/\\\chtypesize/g' addmagfile@ > addmagfilea@


cat maintempre@ addmagfilea@ maintempost@ >> maintemp@


if test -s typesize@; then
   /bin/rm -f typesize@
fi
if test -s havemag@; then
   /bin/rm -f havemag@
fi

sed -n '/\\input/p' maintemp@  > klinetemp@


if test ! -s klinetemp@; then
  if test "$stexf" != ""; then
     chicode -u maintemp@|chicodelistc > $outfilename.tmp2@
     sed 's/\\@@?!lineend@@chinese@@!@$*?char//g' $outfilename.tmp2@ > $filea.tmp00
  else
     chicode -u maintemp@|chicodelistc > $outfilename.tmp2@
     sed 's/\\@@?!lineend@@chinese@@!@\$\*?char//g' $outfilename.tmp2@ > $outfilename
  fi
  exit

else

tr "%" "\235" < klinetemp@ |sed "s/^/%/" |tr "%" "\012"|tr "\235" "%" | \
$gawkbin '
BEGIN{kk=0}
{kk=kk+1; FS="\\\134input"; fnum=2 
while (fnum <=NF)
 {
     {print $fnum}
     fnum = fnum + 1
      }
 }
END{}'| \
chitranp3 



     if test ! -s replacetemp@
     then 
       null="1"
     else
       chmod +x replacetemp@
       ./replacetemp@
     fi



sed 's/\\@@!#?@enput/\\input/g' maintemp@ > maintempnext@

mv maintempnext@ maintemp@


#  if test -s stexf@; then
  if test "$stexf" != ""; then
     chicode -u maintemp@|chicodelistc > $outfilename.tmp2@
     sed 's/\\@@?!lineend@@chinese@@!@$*?char//g' $outfilename.tmp2@ > $filea.tmp00
  else
     chicode -u maintemp@|chicodelistc > $outfilename.tmp2@
     sed 's/\\@@?!lineend@@chinese@@!@\$\*?char//g' $outfilename.tmp2@ > $outfilename
  fi

#fi


  if test ! -s nexttemp@
   then null="1"
  else 
    gawk '
    { inline=$0
      ss=index(inline,"-s ")
      if (ss > 0)
        {sep=index(inline,";")
         fname=substr(inline,ss+3,sep-ss-3)
        }
      else
        {if (index(inline,"chitranpc ")>0)
            {inline="chitranpc " fname}
        }    
      print inline > "nexttempp@"  
    } ' nexttemp@
    chmod +x nexttempp@
    ./nexttempp@
#    chmod +x nexttemp@
#    ./nexttemp@
  fi

  
  rm -f fileb@
  rm -f *.@@@
fi

rm -f tmp@

exit
     
