#!/space/imail/perl/bin/perl

=head1 NAME
  htmlFmt - format HTML files

=head1 SYNOPSIS
  htmlFmt [file]..

=head1 DESCRIPTION

=head1 OPTIONS
  There are two options for setting the margins:

  -lm5 
	 sets the left margin to skip 5 chars.
	 -lm without a number means -lm3.
	 The default is -lm0.  

  -rm75 
	 sets the right margin to 75 chars.
	 -rm without a number means -rm72.
	 The default is -rm70.

=head1 FILES

=head1 BUGS

=head1 AUTHOR
  John Chambers <jc@trillian.mit.edu>

=cut

$| = 1;
($me = $0) =~ s".*/"";
$V = 1;
use HTML::Parse;
use HTML::FormatText;

$files = 0;
$lm = 0;
$rm = 70;

for $a (@ARGV) {
	print "$me: Arg: \"$a\"\n" if $V>1;
	if (($flg,$opts) = ($a =~ /^([-+])(.*)$/)) {
		print "$me: Opts \"$flg\" \"$opts\"\n" if $V>1;
		while (($opt,$rest) = ($opts =~ /^(.)(.*)$/)) {
			print "$me: Opt: \"$flg\" \"$opt\"\n" if $V>1;
			$opt  = lc($opt);
			if (($opt eq 'l') && ($rest =~ s/^m//i)) {
				if ($rest =~ s/^(\d+)//) {$lm = $1} else {$lm = 3}
			} elsif (($opt eq 'r') && ($rest =~ s/^m//i)) {
				if ($rest =~ s/^(\d+)//) {$rm = $1} else {$rm = 72}
			} else {
			}
			$opts = $rest;
		}
	} else {
		print "$me: File \"$a\"\n" if $V>1;
		$html = parse_htmlfile($a);
		$formatter = HTML::FormatText->new(leftmargin => $lm, rightmargin => $rm);
		print $formatter->format($html);
		++$files;
	}
}
print "$me: $files files read.\n" if $V>1;

if ($files < 1) {
	$html = parse_html(join("\n",<STDIN>));
	$formatter = HTML::FormatText->new(leftmargin => $lm, rightmargin => $rm);
	print $formatter->format($html);
}
