#!/bin/sh

# This a part of ChiTeX which cooperate with latex2html to
# enable "latex2html" for chinese
#
# This script is created by; Hung-yih Chen ()
# Some ideas of this script is from from Prof. shann ()
#

if test $# -lt 1 -o "$1" = "-help"; then
   echo " "
   echo "Usage: chilatex2html filename(without extension)"
   echo "where  filename.tex will be used"
   echo "Example:   "
   echo "      chilatex2html samp2 "
   echo "will convert samp2.tex into 'html' file"
   echo " "
   exit
fi

awkloc=`which awk|grep ":"`
if test "$awkloc" = ""; then
   awkloc=`which awk|grep "no awk"`
fi
if test "$awkloc" != ""; then
   awkbin="awk"
else
   awkbin="awk"
fi

export $awkbin

LATEX2HTML=`which latex2html`

sed 's/$LATEX /chilatex -e /g' $LATEX2HTML > chilatexhtmltmp

chmod +x chilatexhtmltmp

#chifilt $1.tex > $1-html.tex
#

deletemenu="1"

if test -d $1-html; then
while test "$deletemenu" = "1"
do
clear
echo "                                        " 
echo "      There has already a  subdir. $1-html"
echo "      You must delete this subdir. to enable having new html files"
echo "      Do you like to rmove this dir:"
echo "      "
echo "      1. Delete subdir. $1-html"
echo "      2. Keep   subdir. $1-html"
echo "      "
echo "      Enter '1' or '2' to choose one the the above: \c"

read inkey

  if test "$inkey" = "1" -o "$inkey" = "2"; then
     deletemenu="0" 
     if test "$inkey" = "1"; then
        rm -rf $1-html

#echo " "
#echo "      Wait! It will start to do the conversion "
#echo " "

     fi
  fi
done
fi

echo " "
echo "      Wait! It will start to do the conversion "
echo " "



#inputstr=`sed -n '/\\input/p' $1.tex`
#includestr=`sed -n '/\\include/p' $1.tex`

sed -n '/\\input/p' $1.tex|cut -d% -f 1 > inputstrf@
sed -n '/\\include/p' $1.tex|cut -d% -f 1 >> inputstrf@ 

inputstr=`
$awkbin ' 
BEGIN{
intab[1]="colordvi"
intab[2]="colordvi.tex"
intab[3]="pictex"
intab[4]="pictex.tex"
intab[5]="table"
intab[6]="table.tex"
}
{ strintab=""
  for (i; i<=6; i++)
      {if (index($0,intab[i]) > 0)
           {strintab="y";break}
      }
  if (strintab == "")
     {print $0}

}
END{} ' inputstrf@ `

rm -f inputstrf@


if test "$inputstr" != ""; then
   inputstr="y"
fi 

if test "$inputstr" = "y"; then
   chilatexh $1.tex 
   mv $1.tmp00 $1-html.tex
else
   chifiltpre $1.tex > $1-html.tex
#   chifilt $1.tex > $1-html.tex
fi

chilatexhtmltmp $1-html


cd $1-html; mkdir tmp
if test -s chifiltall@; then
   rm -f chifiltall@
fi

ls -1 *.html > htmldir@


$awkbin '
BEGIN{}
{
print "chifilt -u " $0 " > tmp/" $0  >> "chifiltall@"
}
END{}' htmldir@


chmod +x chifiltall@
chifiltall@


mv -f tmp/*.html .
cp -f $1-html.html $1.html
rm -rf tmp
rm -f ../$1-html.tex

rm -f chifiltall@
rm -f htmldir@

cd ..


/bin/rm -rf $1/
rm chilatexhtmltmp 

echo "   ********************************************************"
echo "   *                                                      "
echo "   *  The generated html file '$1.html' is on subdir:     "
echo "   *          $1-html                                     "
echo "   *  of this dir.                                        "
echo "   *                                                      "
echo "   ********************************************************"
echo "   "


exit
