#! /s/perl/bin/perl

$use_warn = 0;
$opts_done = 0;

@files = ();

foreach $item (@ARGV) {

    if ($opts_done || (substr($item,0,1) ne '-')) {
        push(@files,$item);
    } elsif ($item eq '--') {
        $opts_done = 1;
    } elsif (($item eq '-w') || ($item eq '-warn')){
        $use_warn = 1;
    } elsif (($item eq '-h') || ($item eq '-help')){
        &show_usage;
    } else {
        print "$item: Unrecognized option\n";
        &show_usage;
    }
}

&show_usage if (! @files);

foreach $file (@files) {

    $in_verbatim = 0;

    if (open(IN,$file)) {
      $num = 0;

      $htmlfile = &htmlize($file);

      open(OUT,"> $htmlfile") || die "Could not open $htmlfile for writing.\n";

      print "Writing output to $htmlfile\n";

      while (<IN>) {

        $num++;

        if ( /^\s*$/ ) {
            if ($in_verbatim) {
                print OUT "\n";
            } else {
                print OUT "<P>\n";
            }
            next;
        }

        # Do the simple replacements first:
        s/\\_/_/g;
        s/\\#/#/g;
        s/\\&/&amp/g;

        s/\$<\$/&lt/g;
        s/\$>\$/&gt/g;

        s/\$\\sim\$/~/g;        # Common
        s/\\emdash / -- /g;
        s/\$\\langle\$/&lt/g;
        s/\$\\rangle\$/&gt/g;

        # Need to watch these so we don't add <P> in verbatim sections
        $in_verbatim = 1 if ( /\\begin\s*{verbatim}/ );
        $in_verbatim = 0 if ( /\\end\s*{verbatim}/ );

        s/\\begin\s*{verbatim}/<PRE>/g;
        s/\\end\s*{verbatim}/<\/PRE>/g;

        s/\\begin\s*{itemize}/<UL>/g;
        s/\\end\s*{itemize}/<\/UL>/g;
 
        s/\\begin\s*{enumerate}/<OL>/g;
        s/\\end\s*{enumerate}/<\/OL>/g;

        s/\\item/<LI>/g;

        s/\\begin\s*{quote}/<BLOCKQUOTE>/g;
        s/\\end\s*{quote}/<\/BLOCKQUOTE>/g;


        # Now do the more complicated brace-matching ones

        $_ = &unlatex($_, '{\\\\em ', 'I');
        $_ = &unlatex($_, '{\\\\it ', 'I');
        $_ = &unlatex($_, '{\\\\bf ', 'B');
        $_ = &unlatex($_, '{\\\\tt ', 'TT');

        $_ = &unlatex($_, '\\\\section{', 'H3');
        $_ = &unlatex($_, '\\\\subsection{', 'H4');
        $_ = &unlatex($_, '\\\\subsubsection{', 'H4');


        # And finally, warn about suspicious looking stuff
        # if they asked for warnings.
        if ($use_warn) {

            # This should catch quite a few
            if ( /\$/ || /{/ || /}/ || /\\/ ) {
                print STDERR "$htmlfile:$num $_";
            }
        }


        print OUT;
      }

      close IN;

      print OUT "\n</BODY>\n</HTML>\n";
      close OUT;

    } else {
      print "File $file not found.\n";
    }
}

exit 0;


##########################

sub unlatex {

  #  used as:
  #  $_ = &unlatex($_, '\\\\section{', 'H3');
  #
  #  to replace \section{...} with <H3>...</H3>

  my($ln, $eff, $rep) = @_;
  my($l,$r,$pos);

  # We assume this gives us the leftmost pattern that matches.
  if ( $ln =~ /$eff/ ) {
    $l = $`;
    $r = &unlatex($',$eff,$rep);

    $pos = &find_rbrace($r);

    if ($pos >= 0) {
        substr($r, $pos, 1) = "</$rep>"; 
        $ln = $l . "<$rep>" . $r;
    }
  }

  return $ln;
}

##########################

sub find_rbrace {

  my($ln) = @_;
  my($lcnt) = 0;
  my($j,$ch);

  for ($j=0; $j < length($ln); $j++) {
    $ch = substr($ln,$j,1);
    if ( $ch eq '{' ) {
        $lcnt++;
    } elsif ( $ch eq '}' ) {
        return $j if ($lcnt-- == 0);
    }
  } 

  return -1;
}

##########################

sub htmlize {

  my($file) = @_;
  my($html);

  if ( $file =~ /\.tex$/ ) {
    $html = $` . '.html';
  } else {
    $html = $file . '.html';
  }

  return $html;
}

##########################

sub show_usage {


  print "\nUsage: l2h [-options] [files...]\n\n";
  print
    "\tConvert simple latex to HTML, ignore stuff you don't understand.\n\n";
  print "Options:\n";

  print "-w:  Print out warnings for confusing stuff that looks like latex.\n";
  print "-h:  Show usage and exit.\n\n";

  die "\n";
}

##########################
