#!/bin/sh
#
# textohtml 'fichier.tex' produit 'fichier.html dans le même chemin

in_file="$1"

cp $1 $1.save
buf_init="${TMPDIR:=/tmp}/textohtml$$1"

if [ ! "$2" = "" ]; then
    out_file=$2
else
    tmml_dir=`dirname $in_file`
    tmml_file=`basename $in_file .tex`
    out_file=$tmml_file
fi

rm -f $out_file

trap 'rm -f "$buf_init" >/dev/null 2>&1' 0
trap "exit 2" 1 2 3 13 15

cat > "$buf_init" <<-EOF
\documentclass{article}
\usepackage{html,graphics,amssymb,color}

%%%%%%%%%% Start TeXmacs macros
\newcommand{\tmsamp}[1]{\texttt{#1}}
\newcommand{\tmem}[1]{{\em #1\/}}
\newcommand{\tmname}[1]{{\sc{#1}}}
\newcommand{\tmstrong}[1]{{\bf{#1}}}
\def\tmdummy{}

\newcommand{\tmhlink}[2]{ \htmladdnormallink{\color{blue} #1}{#2}}
\newcommand{\tmaction}[2]{\htmladdnormallink{#1}{../html/#2_8cc-example.html}}

\newcounter{exercisenr}
\setcounter{exercisenr}{0}
\newenvironment{exercise}{\medskip
  \refstepcounter{exercisenr}\small
  {\bf\noindent Exercise~\arabic{exercisenr}.\ }}{\normalsize}
\newcommand{\assign}{:=}
%%%%%%%%%% End TeXmacs macros
\newenvironment{theorem}{
\begin{rawhtml}
<table width="100%" cellpadding="5" bgcolor="#aaaaaa"><tr><td bgcolor="#ffffcc">
\end{rawhtml} {\bf Theorem:} }{\begin{rawhtml}</td></tr></table>\end{rawhtml}}

\newenvironment{proposition}{
\begin{rawhtml}
<table width="100%" cellpadding="5" bgcolor="#aaaaaa"><tr><td bgcolor="#ffffcc">
\end{rawhtml}{\bf Proposition:} }{\begin{rawhtml}</td></tr></table>\end{rawhtml}}

\newenvironment{corollary}{
\begin{rawhtml}
<table width="100%" cellpadding="5" bgcolor="#aaaaaa"><tr><td bgcolor="#ffffcc">
\end{rawhtml} {\bf Corollary:} }{\begin{rawhtml}</td></tr></table>\end{rawhtml}}

\newenvironment{definition}{
\begin{rawhtml}
<table width="100%" cellpadding="5" bgcolor="#aaaaaa"><tr><td bgcolor="#ffffcc">
\end{rawhtml} {\bf Definition:} }{\begin{rawhtml}</td></tr></table>\end{rawhtml}}

\newenvironment{algorithm}{
\begin{rawhtml}
<table width="100%" cellpadding="5" bgcolor="#aaaaaa"><tr><td bgcolor="#dfdfff">
\end{rawhtml}{\bf Algorithm:} }{\begin{rawhtml}</td></tr></table>\end{rawhtml}}

\renewcommand{\chapter}[1]{
\begin{rawhtml}
<hr/><table width="100%" align="center" cellpadding="10" border="0"><tr><td
bgcolor="SteelBlue"><b><font size="6" color="white" face="helvetica">
\end{rawhtml}
{#1}
\begin{rawhtml}</font></b></td></tr></table><hr/></dt>\end{rawhtml}
}

\renewcommand{\section}[1]{
\begin{rawhtml}
<table width="100%" align="center" cellpadding="10" border="0"><tr><td bgcolor="#00ffff"><b><font size="6" color="black" face="helvetica">
\end{rawhtml}
{#1}
\begin{rawhtml}</font></b></td></tr></table><hr></hr>\end{rawhtml}}

\renewcommand{\subsection}[1]{
\begin{rawhtml}
<h2><font color="#ff7700" face="helvetica">
\end{rawhtml}
{#1}
\begin{rawhtml}</font></h2>\end{rawhtml}}

\begin{document}
EOF
cat $in_file>>$buf_init
echo "\\end{document}" >>$buf_init

perl -pi -e "s/\.tex/\.html/g" $buf_init
perl -pi -e "s/DOC/\.\.\/html/g" $buf_init

cp $buf_init $out_file.tex
cp $out_file.tex tmp.tex

latex2html -no_navigation -split 0 -no_subdir -info '0' -prefix $out_file- $out_file.tex 

rm $out_file-images.tex $out_file-images.aux $out_file-images.css tmp.tex

mv $1.save $1
perl -pi -e "s/#!/<a href=\".\/bibliography.html#/g" $out_file.html
perl -pi -e "s/!#/\">=<\/a>/g" $out_file.html
perl -pi -e "s/<PRE>/<table width=\"100\%\" cellpadding=\"5\" bgcolor=\"\#CCCCCC\"><tr><td bgcolor=\"\#f1f1f1\"><PRE>/g" $out_file.html
perl -pi -e "s/<\/PRE>/<\/PRE><\/td><\/tr><\/table>/g" $out_file.html
