#!/usr/local/bin/perl

sub basename {
  $slash = rindex($_[0], "/");
  $suffix = rindex($_[0], $_[1]);
  if ($suffix == -1) {$suffix=length($_[0])};
  return substr($_[0], $slash+1, $suffix-$slash-1);
}

sub dirname {
  $slash = rindex($_[0], "/");
  if ($slash == -1) {return "."};

  return substr($_[0], 0, $slash);
}

sub DoHtml {

  $infile = $_[0];
  open(INP,$infile) || die "Can't open '$infile' for reading: $!";

  $breakLen=0;
  $didLF=1;
  $level = 0;
  while (<INP>) {
    $result = &DoLine($_);
  }
  &DoTab(0);
  close(INP);
  close(OUT);
  return;
}

sub GetItem {
  &DoFont($_);
  $len = length($_);
  if ($len == 0) {return $_};
 
  $start = index($_, "<");
  $end = index($_, ">");
  if ($end == -1 || $start == -1 || $start > $end) 
  {
     #
     # No more markers in the space
     # Set space to null and return the residual
     chop;
     $result = $_;
     $_ = "";
     return $result;
  }
  if ($start != 0) {
     #
     # Marker does not begin at start of line.
     # If only leading whitespace, return the marker
     # Otherwise return the leading string up to the marker
     #
     if (m#^[ \t]*<#) {
        s#^[ \t]*<#<#;
        $end = index($_, ">");
        $result = substr($_, 0, $end+1);
        $_ = substr($_,$end+1);
        return $result;
     }
    $result = substr($_, 0, $start);
    $_ = substr($_, $start);
    return $result;
  }

  #
  # Marker starts the current space
  # Return the marker and remove it from the space
  #
  $result = substr($_,0,$end+1);
  $_ = substr($_,$end+1);
  return $result;
}

sub DoLine {

  while (length$_) {
    $item = &GetItem($_);
    &SetLevel($item);
    while (length($item) > 0) {    
      $item = &DoPrint($item);
    }
  }
  return 0;
}

sub DoTab {
    $i = $_[0];
    if ($didLF == 1) {return};
    print "\n";
    $breakLen = 0;
    while ($i-- > 0) {
         print "    ";
         $breakLen += 4;
    }
    $didLF = 1;
}

sub DoPrint {
   $didLF = 0;
   if (! $doBreak) {
     $len=length($_[0]);
     if ($breakLen+$len > $lineLength) {
       # Line will be full. Break at whitespace
       # and return the residual
       #
       $pos = $lineLength-$breakLen;
       $end=index($_[0]," ",$pos);
       if ($end < 0) {
         # No whitespace; new line  and print as is
         &DoTab($level);
         print $_[0];
         return "";
       }
       print substr($_[0],0,$end);
       &DoTab($level);
       return substr($_[0],$end+1);
     };
     $breakLen += $len;
     print "$_[0] ";
     return "";
   }

   print "$_[0]";
   &DoTab($level);
   return "";
}

sub SetLevel {
  if ($_[0] eq "<HTML>" || $_[0] eq "<html>" ||
      $_[0] eq "<BODY>" ||  $_[0] eq "<body>" ||
      $_[0] eq "<HR>" ||  $_[0] eq "<hr>" ||
      $_[0] eq "<HEADER>" || $_[0] eq "<header>") 
    {
      $doBreak=1;$level=0;&DoTab($level);return;
    };
  if ($_[0] eq "<P>"  ||
      $_[0] eq "</P>" ||
      $_[0] eq "<p>"  ||
      $_[0] eq "</p>") {$doBreak=1; &DoTab(0);return};

  if ($_[0] eq "<BR>"  ||
      $_[0] eq "</BR>" ||
      $_[0] eq "<br>"  ||
      $_[0] eq "</br>") {$doBreak=1; &DoTab(0);return};

    if ($_[0] eq "<B>" || $_[0] eq "<b>"  ||
      $_[0] eq "</B>" ||  $_[0] eq "</b>" ||
      $_[0] eq "<I>"  ||  $_[0] eq "<i>" ||
      $_[0] eq "</I>" ||  $_[0] eq "</i>" ||
      $_[0] eq "<LI>" ||  $_[0] eq "<li>" ||
      substr($_[0],0,2) eq "<H" ||
      substr($_[0],0,2) eq "<h" ||
      substr($_[0],0,3) eq "</h" ||
      substr($_[0],0,3) eq "</H" ||
      $_[0] eq "<TITLE>" || $_[0] eq "<title>"  ||
      $_[0] eq "</TITLE>" ||
      $_[0] eq "</title>" ) {$doBreak=0;return};
  
   if ($_[0] eq "</DT>" ||
       $_[0] eq "</dt>") {$doBreak=0;$level++;return};

   if ($_[0] eq "<DT>" ||
       $_[0] eq "<dt>") {$level--;return};

  if (length($_[0]) < 2 ||
      substr($_[0],0,1) ne "<") {$doBreak=0;return};

  $doBreak=1;
  if (substr($_[0],1,1) eq "/") {
    if ($level > 0) {$level--};
    &DoTab($level);
    return;
  }

  &DoTab($level);
  $level++;
  return;
}

#================
# Consecutive font transitions: 
#
sub DoFont {
  s#</B><B>##ig;
  s#</B> <B># #ig;
  s# </B>_<B> #_#ig;
  s#</I><I>##ig;
  s# </I>#</I>#ig;
  s#<I> #<I>#ig;
  s#</I> <I># #ig;
  s#</I>_<I>#\_#ig;
  return 1;
}

#================
# Process all HTML file names
$lineLength = 70;
for $arg (@ARGV) {
  $isTitle=0;
  &DoHtml($arg);
}

exit
