#!/usr/bin/perl -w
#
# $Id: emailprint.pl,v 1.2 2001/08/16 15:12:05 davee Exp $

$open_char  = '<';
$close_char = '>';
$homedir    = $ENV{HOME};

# This is 1 for headers, 2 for body and 3 for sig
$current_section = 1;

open(OUTPUT, ">tmpprint.tex");

print OUTPUT "\\documentclass[a4paper,12pt]{article}\n";
print OUTPUT "\\pagestyle{plain}\n";
print OUTPUT "\\usepackage{charter}\n";
print OUTPUT "\\begin{document}\n";
print OUTPUT "\\thispagestyle{empty}\n";
print OUTPUT "\\noindent\n";
print OUTPUT "\\begin{tabular}{ll}\n";
print OUTPUT "\\hline";
while (<STDIN>) {
    $rec = $_;
    chop($rec);
    if ($current_section == 1) {
        $rec =~ s/$open_char/(\\emph{/g;
        $rec =~ s/$close_char/})/g;
        if ($rec =~ /^From: / || 
            $rec =~ /^Date: / || 
            $rec =~ /^Subject: / || 
            $rec =~ /^To: / || 
            $rec =~ /^cc: /i)
        {
            print OUTPUT "{\\bf $&} & $'\\\\\n";
        }
        elsif ($rec =~ /^$/) {
            $current_section = 2;
            print OUTPUT "\\hline\n\\end{tabular}\n";
            print OUTPUT "\\begin{verbatim}\n";
        }
    }
    elsif ($current_section == 2) {
        print OUTPUT "$rec\n";
        if ($rec =~ /^-- $/) {
            print OUTPUT "\\end{verbatim}\n";
            $current_section = 3;
            print OUTPUT "\\emph{";
        }

    }
    else {
        print OUTPUT "$rec\\\\\n";
    }
}
if ($current_section == 3) {
    print OUTPUT "}";
}
else {
    print OUTPUT "\\end{verbatim}\n";
}
print OUTPUT "\\end{document}\n";
close(OUTPUT);
system("latex -interaction=batchmode tmpprint.tex");
system("dvips tmpprint.dvi");
system("rm $homedir/tmpprint.*");
system("rm $homedir/texput.log");
