#!/usr/bin/perl
use LWP::Simple;
use HTML::Parse;
use HTML::Entities;
use HTML::FormatPS;
use Getopt::Long;

use strict;
require 5.0;

=pod

=head 1 hp -- HTML postscript printer

Online documentation will go here

=cut

# initialize variables local to main loop 

my ($target) = "";   # name of current file to process
my ($help)   = "";   # help flag
my ($property) = ""; # scratch loop item

if (!defined($ARGV[0])) {
   $ARGV[0] = "";
}

# set up default values for printing

%MAIN::options = (
    PaperSize        => "A4",
    PaperWidth       => 0,
    PaperHeight      => 0,
    LeftMargin       => 0,
    RightMargin      => 0,
    HorizontalMargin => 0,
    TopMargin        => 0,
    BottomMargin     => 0,
    VerticalMargin   => 0,
    FontFamily       => "Times",
    FontScale        => 0,
    Leading          => 0,
    PageNo           => 0,
);
# set up command line options

GetOptions(
           "s=s"              => \$MAIN::options{PaperSize},
           "w=i"              => \$MAIN::options{PaperWidth},
           "H=i"              => \$MAIN::options{PaperHeight},
           "l=i"              => \$MAIN::options{LeftMargin},
           "r=i"              => \$MAIN::options{RightMargin},
           "h=i"              => \$MAIN::options{HorizontalMargin},
           "t=i"              => \$MAIN::options{TopMargin},
           "b=i"              => \$MAIN::options{BottomMargin},
           "v=i"              => \$MAIN::options{VerticalMargin},
           "f=s"              => \$MAIN::options{FontFamily},
           "S=i"              => \$MAIN::options{FontScale},
           "L=i"              => \$MAIN::options{Leading},
           "p!"               => \$MAIN::options{PageNo},
           "size=s"           => \$MAIN::options{PaperSize},
           "width=i"          => \$MAIN::options{PaperWidth},
           "height=i"         => \$MAIN::options{PaperHeight},
           "left=i"           => \$MAIN::options{LeftMargin},
           "right=i"          => \$MAIN::options{RightMargin},
           "horizontal=i"     => \$MAIN::options{HorizontalMargin},
           "top=i"            => \$MAIN::options{TopMargin},
           "bottom=i"         => \$MAIN::options{BottomMargin},
           "vertical=i"       => \$MAIN::options{VerticalMargin},
           "font=s"           => \$MAIN::options{FontFamily},
           "scale=i"          => \$MAIN::options{FontScale},
           "leading=i"        => \$MAIN::options{Leading},
           "pageno"           => \$MAIN::options{PageNo},
           "h"                => \$help,
           "help"             => \$help);

# issue help message, if needed

if (($help) || ($ARGV[0] eq "")) {
   print "\nhpps -- HTML to Postscript filter\n",
         "Format an HTML document into PostScript.\n",
         "\nOptions:\n",
         "-s <val> --size=<val>      Paper size (A3, A4, A5, B4, B5, \n",
         "                                       Letter, Legal, Executive,\n", 
         "                                       Tabloid, Statement, Folio,\n",
         "                                       10x14, Quarto)\n",
         "-w <n>, --width=<n>        Paper width (points)\n",
         "-H <n>, --height=<n>       Paper height (points)\n",
         "-l <n>, --left=<n>         Left margin (points)\n",
         "-r <n>, --right=<n>        Right margin (points)\n",
         "-h <n>, --horizontal=<n>   Horizontal margin \n",
         "-t <n>, --top=<n>          Top margin (points)\n",
         "-b <n>, --bottom=<n>       Bottom margin (points)\n",
         "-v <n>, --vertical=<n>     Vertical margin \n",
         "-f <n>, --font=<n>         Font to use (Times, Courier, Helvetica)\n",
         "-S <n>, --scale=<n>        Font scaling factor\n",
         "-L <n>, --leading=<n>      Gap between lines (factor of fontsize)\n",
         "-p,     --pageno,          Switch page numbers off\n",
         "-h,     --help             Print this help text\n",
         "\nTypical usage:\n",
         "hpps --size=A4 --font=Times index.html|lpr\n\n";
   exit 0;  
}

# twiddle pageno

if ($MAIN::options{PageNo} == 1) {
    $MAIN::options{PageNo} = 0;
} else {
    $MAIN::options{PageNo} = 1;
}

# discard the unused options in %options

foreach (keys %MAIN::options) {
    if (!$MAIN::options{$_}) {
        delete $MAIN::options{$_};
    }
}
# foreach specified file, prettyprint it!

foreach $target (@ARGV) {
    my ($p) = HTML::Parser->new;
    $p = parse_htmlfile($target);
    my ($formatter) = new HTML::FormatPS(%MAIN::options);
    print $formatter->format($p);
}

exit 0;

