#!/usr/bin/perl
# !/usr/bin/perl -w		# use for debug
#-------------------------------------------------------------------------
# File:		htmlmail.pl
# Description:	Translate a HTML file to a ASCII text-file.
# Author:	Cees van de Griend <cvdg@pobox.com>
# Copyright:	GPL - GNU General Public License
# Date:		19990301
# Version:	1.11
#
# Note:		
# If you only want to display HTML-mail, add to your ~/.mailcap:
#       text/html; lynx -dump %s; copiousoutput; nametemplate=%s.html
# else, add to your ~/.mailcap:
#       text/html; htmlmail; copiousoutput; print=htmlmail -p
# 
# Usage:
# htmlmail.pl [-d] [-f <file>] [-o <file>] [-p]
# 	-d:	 	debug, default is no debug
# 	-f <file>:	read from <file>, default is read from STDIN
# 	-o <file>:	write to <file>, default is write to STDOUT
#       -p:		print file in PostScript to 'lpr'
#-------------------------------------------------------------------------

# use strict;			# use for debug
# use diagnostics;		# use for debug


use Getopt::Std;
use HTML::FormatPS;
use HTML::FormatText;
use HTML::TreeBuilder;


#-------------------------------------------------------------------------
# prototypes
#-------------------------------------------------------------------------

sub read_html();
sub html2text();
sub write_text();
sub print_html();


#-------------------------------------------------------------------------
# constants
#-------------------------------------------------------------------------

my $html = "/tmp/htmlmail.$$.html";	# filename: html
my $text = "/tmp/htmlmail.$$.txt";	# filename: text
my $LPR  = "/usr/bin/lpr";		# program:  lpr


#-------------------------------------------------------------------------
# variables
#-------------------------------------------------------------------------

my $option_usage = 0;
my $option_debug = 0;
my $option_file  = 0;
my $option_out   = 0;
my $option_print = 0;


#-------------------------------------------------------------------------
# Sub:		MAIN
#-------------------------------------------------------------------------

{
    my %opts;

    if (not getopts("df:o:p", \%opts)) {
	$option_usage++;
    } 
    $option_debug = $opts{"d"} if defined($opts{"d"});
    $option_file  = $opts{"f"} if defined($opts{"f"});
    $option_out   = $opts{"o"} if defined($opts{"o"});
    $option_print = $opts{"p"} if defined($opts{"p"});
    
    if ($option_debug) {
	print("Usage: $option_usage\n");
	print("Debug: $option_debug\n");
	print("File:  $option_file\n");
	print("Out:   $option_out\n");
	print("Print: $option_print\n");
    }

    if ($option_usage) {
	print("Usage:\n");
	print("htmlmail.pl [-d] [-f <file>] [-o <file>]\n");
	print("	-d:        debug, default is no debug\n");
	print(" -f <file>: read from <file>, default is read from STDIN\n");
	print(" -o <file>: write to <file>, default is write to STDOUT\n");
	print(" -p:        write to 'lpr' in postscript\n");
	exit(2);
    } elsif ($option_print) {
	read_html();
	print_html();

	if (not $option_debug) {
 	    if (-e $html) {
	    	unlink($html) or die("Can't unlink $html: $!");
	    }
	}
	exit(0);
    } else {
	 read_html();
	 html2text();
	 write_text();

	if (not $option_debug) {
 	    if (-e $html) {
	    	unlink($html) or die("Can't unlink $html: $!");
	    }
	    if (-e $text) {
	    	unlink($text) or die("Can't unlink $text: $!");
	    }
	}
        exit(0);
    }
}


#-------------------------------------------------------------------------
# Sub:		read_html
# Description:	Read <file> or STDIN and write the input to $html.
# Date:		19990301
#-------------------------------------------------------------------------

sub read_html() {
    my $FILE;

    if ($option_file) {
    	open(FILE, "<$option_file") or die("Can't open $option_file: $!");
	$FILE = *FILE;
    } else {
	$FILE = *STDIN;
    }

    open(HTML, ">$html") or die("Can't open $html: $!");
    while (<$FILE>) {			# read  $_ from STDIN
	print(HTML);			# write $_ to   HTML
    }
    close(HTML) or die("Can't close $html: $!");

    if ($option_file) {
    	close($FILE) or die("Can't close $option_file: $!");
    }
}


#-------------------------------------------------------------------------
# Sub:		html2text
# Description:	Translate $html to $text.
# Date:		19990301
#-------------------------------------------------------------------------

sub html2text() {
    my $parser = HTML::TreeBuilder->new();
    my $format = HTML::FormatText->new();

    open(TEXT, ">$text") or die("Can't open $text: $!");
    $parser->parse_file($html);
    print(TEXT $format->format($parser));
    close(TEXT) or die("Can't close $text: $!");
}


#-------------------------------------------------------------------------
# Sub:		write_text
# Description:	Read $text and write to STDOUT or <file>.
# Date:		19990301
#-------------------------------------------------------------------------

sub write_text() {
    my $OUT;

    if ($option_out) {
	open(OUT, ">$option_out") or die("Can't open $option_out: $!");
	$OUT = *OUT;
    } else {
	$OUT = *STDOUT;
    }

    open(TEXT, "<$text") or die("Can't open $text: $!");
    while (<TEXT>) {			# read  $_ from TEXT
	print($OUT $_);			# write $_ to   STDOUT
    }
    close(TEXT) or die("Can't close $text: $!");

    if ($option_out) {
	close($OUT) or die("Can't close $option_out: $!");
    }
}


#-------------------------------------------------------------------------
# Sub:		print_html
# Description:	Translate $html to PostScript and write to $LPR
# Date:		19990301
#-------------------------------------------------------------------------

sub print_html() {
    my $parser = HTML::TreeBuilder->new();
    my $format = new HTML::FormatPS;

    open(LPR, "|$LPR") or die("Can't open $LPR: $!");
    $parser->parse_file($html);
    print(LPR $format->format($parser));
    close(LPR) or die("Can't close $LPR: $!");
}


#-------------------------------------------------------------------------
# Log:
# 19990225	First version.
# 19990301	Renamed: 'html2txt.pl' to 'htmlmail.pl'
# 		Added:   options -d, -f <file> and -o <file>
#		Changed: don't use 'lynx' but perl-modules
#		Added:   option -p
#-------------------------------------------------------------------------
