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

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

export $gawkbin

LATEX2HTML=`which latex2html`

#sed 's/$LATEX /chilatexhh -e /g' $LATEX2HTML > chilatexhtmltmp
#sed 's/$LATEX /chitex /g' $LATEX2HTML > chilatexhtmltmp

#chmod +x chilatexhtmltmp

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

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

if test ! -s $TEXMF/tex/chinese/l2hconfpm; then
 FindConfpm(){
   clear
   echo " "; echo " "
   echo "   It will take time to setup chilatex2html environment"
   echo "   at first use of chilatex2html. Wait! "

  find /usr -name 'l2hconf.pm' > pmdir@

  if test -s pmdir@; then
   $gawkbin ' 
   BEGIN{}
   {i=i+1
    {if (i==1){print $0 > "pmdir1@"}}
    {if (i==2){print $0 > "pmdir2@"}}
    {if (i==3){print $0 > "pmdir3@"}}
   }
   END{} ' pmdir@
  fi

 
  if test -s "pmdir1@"; then
    pmdir1=`cat pmdir1@`
  fi
  if test -s "pmdir2@"; then
    pmdir2=`cat pmdir2@`
  fi

  if test -s "pmdir3@"; then
    pmdir3=`cat pmdir3@`
  fi

 j=0
 while [ $j -le 2 ];
 do
  j=`expr $j + 1`
   
   if test "$j" = "1"; then pmdir=$pmdir1; fi
   if test "$j" = "2"; then pmdir=$pmdir2; fi
   if test "$j" = "3"; then pmdir=$pmdir3; fi

  if test "$pmdir" != ""; then
   if test -s $pmdir; then
    $gawkbin -v j=$j '
    BEGIN{}
    {inlineorg=$0
     inline=$0
     mark=index(inline,"#")
     if (mark>0){inline=substr(inline,1,mark-1)}
     if (index(inline,"\$LATEX")>0)
        {if (index(inline,"chilatex")==0 && index(inline,"chilatexhh")==0)
             {gsub(/latex/,"chilatexhh",inlineorg)}
         if (index(inline,"chilatex")>0 && index(inline,"chilatexhh")==0)
             {gsub(/chilatex/,"chilatexhh",inlineorg)}
        }
     
     if (index(inline,"@IMAGE_TYPES = qw(png")>0)
        {inlineorg="@IMAGE_TYPES = qw(gif png);"
        }

     if (index(inline,"\$HTML_VERSION")>0)
        {inlineorg="\$HTML_VERSION = \0473.2,math\047;"
        }
      
     print inlineorg > "l2hconf" j ".pm@"
    }
    END{}' $pmdir
    if test -s l2hconf$j.pm@; then
       if test ! -s $pmdir".bak"; then
         cp $pmdir $pmdir".bak"
       fi
       mv -f l2hconf$j".pm@" $pmdir
    fi  
  fi
 fi 
 done
}


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@ `

    FindConfpm
    echo "y" > $TEXMF/tex/chinese/l2hconfpm

fi







#exit

deletemenu="1"

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

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 $echomode "      Enter '1' or '2' to choose one the the above: "$echocrread
read inkey

  if test "$inkey" = "1" -o "$inkey" = "2"; then
     deletemenu="0" 
     if test "$inkey" = "1"; then
        /bin/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=`
$gawkbin ' 
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 
   chilatexh $1 > $1-html.tex
#   mv $1.tmp00 $1-html.tex
else
#   chifiltpre $1.tex > $1-html.tex
   chifiltpre $1 > $1-html.tex
fi


if test ! -d $1-html; then
   mkdir $1-html
fi

#cp -f *.eps $1-html


#./chilatexhtmltmp $1-html

  latex2html $1-html


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

ls -1 *.html > htmldir@


$gawkbin '
BEGIN{}
{
#print "chifilt -u " $0 " > tmp/" $0  >> "chifiltall@"
print "chifilt -u " $0 " > " $0".tmp"  >> "chifiltall@"
print "chifiltpost "  $0 ".tmp > tmp/" $0 >> "chifiltpostall@"
}
END{}' htmldir@



chmod +x chifiltall@
./chifiltall@

chmod +x chifiltpostall@
./chifiltpostall@



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

rm -f chifiltall@
rm -f chifiltpostall@
rm -f htmldir@

cd ..


/bin/rm -rf $1/
rm -f chilatexhtmltmp 
rm -f texfile@
rm -f *@

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


exit
