#!/usr/bin/perl -w 
$file= $ARGV[0];
open(DAT, $file) || die("Could not open file $file!");
$file =~ s/\.tex//g;
@raw_data=<DAT>;
$i=0;
foreach $_ (@raw_data)
{
  s{@([^@\n]+)@}|<tt>$1</tt>|g;
  s/\\begin{document}/\/\*\*/g;
  s/\\end{document}/\*\*\//g;
  s/\\item/\\li /g;
  s/\\begin{verbatim}/\\code/g;
  s/\\end{verbatim}/\\endcode/g;
  s/\\tmaction{\$\\boxdot\$}/\\example/g;
  s/Example:[\\]* \\tmtexttt{/\\example{/g;
  if(m/\\example{([^}]*)}/g) { $i=$i+1; }
  s/\\example{([^}]*)}/\\htmlonly<div class=\"code\"><div style=\"display: block;\"><p><img src=\"ftv2pnode.png\" alt=\"o\" width=16 height=22 onclick=\"toggleFolder(\'folder$i\', this)\"\/><img src=\"ftv2folderclosed.png\" alt=\"+\" width=24 height=22 onclick=\"toggleFolder(\'folder$i\', this)\"\/><\/p><div  id=\"folder$i\">\\endhtmlonly\n\\include $1\n\\htmlonly<\/div><\/div><\/div>\\endhtmlonly/g;
  s/File:[\\ ]*\\tmtexttt{/\\file{/g;
  if(m/\\file{([^}]*)}/g) { $i=$i+1; }
  s/\\file{([^}]*)}/\\htmlonly<div class=\"code\"><div style=\"display: block;\"><p><img src=\"ftv2pnode.png\" alt=\"o\" width=16 height=22 onclick=\"toggleFolder(\'folder$i\', this)\"\/><img src=\"ftv2folderclosed.png\" alt=\"+\" width=24 height=22 onclick=\"toggleFolder(\'folder$i\', this)\"\/><\/p><div  id=\"folder$i\">\\endhtmlonly\n\\include $1\n\\htmlonly<\/div><\/div><\/div>\\endhtmlonly/g;
  s/{\\cite{([\w\d\. ]*)}}/\[$1\]/g;
  s/{\\index{[\w\d\. ]*}}//g;
  s/\\label{([\w\d\. ]*)}/\\anchor $file$1 /g;
  s/\\ref{([\w\d\. ]*)}/\\ref $file$1 \"\[\*\]\" /g;
  s/\\begin{eqnarray([\*])}/\\f{eqnarray$1}/g;
  s/\\end{eqnarray([\*])}/\\f}/g;
  s/\\begin{equation}/\\f{equation}/g;
  s/\\end{equation}/\\f}/g;
  s/\\begin{quote}//g;
  s/\\end{quote}//g;
  s/\\begin{itemize}//g;
  s/\\end{itemize}//g;
  s/\\begin{center}//g;
  s/\\end{center}//g;
  s/\\begin{enumerate}//g;
  s/\\end{enumerate}//g;
  s/{\\noindent}//g;
  s/{\\hspace\*{\\fill}}//g;
  s/\\begin{flushright}/\\htmlonly <right>\\endhtmlonly /g;
  s/\\end{flushright}/\\htmlonly <\/right>\\endhtmlonly /g;
  s/\\begin{definition}/\\htmlonly <table width=\"100%\" cellpadding=\"5\" bgcolor=\"\#aaaaaa\"><tr><td bgcolor=\"\#ffffcc\">\n<b>Definition:<\/b>\\endhtmlonly /g;
  s/\\end{definition}/\\htmlonly <\/td><\/tr><\/table>\\endhtmlonly/g;
  s/\\begin{proposition}/\\htmlonly <table width=\"100%\" cellpadding=\"5\" bgcolor=\"\#aaaaaa\"><tr><td bgcolor=\"\#ffffcc\">\n<b>Proposition:<\/b>\\endhtmlonly /g;
  s/\\end{proposition}/\\htmlonly <\/td><\/tr><\/table>\\endhtmlonly/g;
  s/\\begin{theorem}/\\htmlonly <table width=\"100%\" cellpadding=\"5\" bgcolor=\"\#aaaaaa\"><tr><td bgcolor=\"\#ffffcc\">\n<b>Theorem:<\/b>\\endhtmlonly /g;
  s/\\end{theorem}/\\htmlonly <\/td><\/tr><\/table>\\endhtmlonly/g;
  s/\\begin{corollary}/\\htmlonly <table width=\"100%\" cellpadding=\"5\" bgcolor=\"\#aaaaaa\"><tr><td bgcolor=\"\#ffffcc\">\n<b>Corollary:<\/b>\\endhtmlonly /g;
  s/\\end{corollary}/\\htmlonly <\/td><\/tr><\/table>\\endhtmlonly/g;
  s/\\begin{algorithm}/\\htmlonly <table width=\"100%\" cellpadding=\"5\" bgcolor=\"\#aaaaaa\"><tr><td bgcolor=\"\#dfdfff\">\n<b>Algorithm:<\/b>\\endhtmlonly /g;
  s/\\end{algorithm}/\\htmlonly <\/td><\/tr><\/table>\\endhtmlonly/g;
  s/\\begin{exercise}/\\htmlonly <table width=\"100%\" cellpadding=\"5\" bgcolor=\"\#ffaa00\"><tr><td bgcolor=\"\#ffffaa\">\n<b>Exercise:<\/b>\\endhtmlonly /g;
  s/\\end{exercise}/\\htmlonly <\/td><\/tr><\/table>\\endhtmlonly/g;
  s/See:/\\sa /g;
  s/\\_/_/g;
  s/\\ //g;
  s/\\section{([^}]*)}/\\page $file $1/g;
  s/\\subsection{([\w\d_]*)([, ]*)([\w\d,;]*)([ ]*)([\w\d,;]*)([ ]*)([\w\d,;]*)([\w\d,; ]*)}/\\section $file$1_$3_$5 $1$2$3$4$5$6$7$8/g;
  s/\\subsubsection{([\w\d_]*)([, ]*)([\w\d,;]*)([ ]*)([\w\d,;]*)([ ]*)([a-zA-Z]*)}/\\subsection $file$1_$3_$5_$7 $1 $3 $5 $7/g;
  s/\\end{itemize}//g;
  s/\\tmmathbf{/\\mathbf{/g;
  s/\\tmtextbf{/\\textbf{/g;
  s/{\\tmsamp{([^}]*)}}/<em>$1<\/em>/g;
  s/{\\tmname{([^}]*)}}/<em>$1<\/em>/g;
  s/{\\tmem{([^}]*)}}/<em>$1<\/em>/g;
  s/\\tmem{([^}]*)}/<em>$1<\/em>/g;
  s/{\\tmtexttt{([^}]*)}}/<tt>$1<\/tt>/g;
  s/\\tmtexttt{([^}]*)}/<tt>$1<\/tt>/g;
  s/{\\tmname{([^}]*)}}/<b>$1<\/b>/g;
  s/\\tmname{([^}]*)}/<b>$1<\/b>/g;
  s/{\\tmtexbf{([^}]*)}}/<b>$1<\/b>/g;
  s/ }}{\\color/}\\htmlonly<\/font>\\endhtmlonly {\\color/g;
  s/{\\ttfamily{([^}]*)}}/\\htmlonly<font color=\"brown\">\\endhtmlonly$1\\htmlonly<\/font>\\endhtmlonly/g;
  s/\\ttfamily{([^}]*)}/<tt>$1<\/tt>/g;
  s/\\verb\|([^\|]*)\|}/<tt>$1<\/tt>\\htmlonly<\/font>\\endhtmlonly/g;
  s/{\\color{blue}/\\htmlonly<font color=\"blue\">\\endhtmlonly/g;
  s/{\\color{red}/\\htmlonly<font color=\"orange\">\\endhtmlonly/g;
  s/{\\tmhlink<tt>([\w\d\/\$\.: ]*)<\/tt>{([\w\d\/\.:\-\+ ]*)}}/ <a href=\"$2\">$1<\/a>/g;
  s/{\\tmhlink{([\w\d\/\$\\\.: ]*)}{([\w\d\/\.:\-\+ ]*)}}/ <a href=\"$2\">$1<\/a>/g;
  s/\\\[/\\f\[/g;
  s/\\\]/\\f\]/g;
  s/\\includegraphics\[([\w\d= ]*)\]{([\w\d\-_]*.)([\w]*)}/\\image html $2jpg \n\\image latex $2eps \"figure\" $1/g;
#  s/\$\$([\w\d:\-\+\^\{\}\(\) \t\r\n]*)\$\$/\\f\[$1\\f\]/g;
  if((m/\$\$/g))
    {
      if($bg==0)
	{
	  s/\$\$/\\f\[/g;
      $bg=1;
	}
      else
	{
	  s/\$\$/\\f\]/g;
	  $bg=0;
	}
    }
  s/\$/\\f\$/g;
  print $_;

}
close(DAT);

