#!/usr/bin/perl -w

=head1 NAME

mimeprint - parse a MIME stream, and print the parsed entity

=head1 SYNOPSIS

    mimeprint
        Give usage.
     
    mimeprint [options] infile
        Parse a MIME stream from infile (- means stdin), and print 
        entity to stdout.

=head1 DESCRIPTION

Parse a MIME stream, and output the resulting entity.  This is
a nice way of eyeballing whether or not MIME-tools "understood"
your MIME message.

Due to nonuniqueness of MIME encodings, there is a very good chance
that your output will not I<exactly> resemble your input.    

=head1 AUTHOR

Eryq, eryq@zeegee.com

=cut

use lib "./lib";
use strict;
use Getopt::Std;

use vars qw($opt_e $opt_h $opt_n);
use MIME::Parser;

# Usage?
$ARGV[0] or die <<EOF;

Usage:
    mimeprint [-options] infile
       Parse a MIME stream from infile, and print entity to stdout:
          -h           Decode headers
          -e ENCODING  Re-encode top-level entity (if not multipart)
          -n HOW       Parse nested messages [NEST or REPLACE]

EOF

# Args:
getopts("he:n:");

# Set up parser:
my $parser = new MIME::Parser;
$parser->output_to_core('ALL');       # 512 bytes or less goes to core
$parser->parse_nested_messages($opt_n) if ($opt_n);
$parser->decode_headers($opt_h);

# Parse:
my $ent = $parser->parse_in($ARGV[0]);
$ent or die "MIME parsing failed!\n";
if ($opt_e and !$ent->is_multipart) {
    supported MIME::Decoder $opt_e or die "unsupported encoding: $opt_e\n";
    $ent->head->mime_attr("Content-transfer-encoding" => $opt_e);
}
$ent->print(\*STDOUT);

1;


