#!/usr/pkg/bin/perl

=begin metadata

Name: mimedecode
Description: extract MIME attachments in uudecode-like manner
Author: Nick Ing-Simmons, nick@ni-s.u-net.com
License:

=end metadata

=cut

use v5.12.0;
use Getopt::Std;

SANITY: {
	my $external_module = 'MIME::Parser';
	my $rc = eval "require $external_module; $external_module->import; 1";
	die "This program needs the $external_module module.\n" unless $rc;
	}

{package PerlPowerTools::MIME::Parser;
push @INC, 'MIME::Parser'; # this is old school so -c doesn't complain

sub new_body_for
 {
  my ($parser,$head) = @_;
  my $outname = $head->recommended_filename;
  if (defined $outname)
   {
    return MIME::Body::File->new($parser->output_path($head));
   }
  else
   {
    return MIME::Body::Scalar->new;
   }
 }

}

my %opt = ( 'd' => '.' );

getopts('d:',\%opt);

my $parser = MIME::Parser->new;
$parser->output_dir($opt{'d'});

if (@ARGV)
 {
  foreach my $file (@ARGV)
   {
    my $entity = $parser->parse_in($file);
   }
 }
else
 {
  my $entity = $parser->read(\*STDIN);
 }

__END__

=head1 NAME

mimedecode - extract MIME attachments in uudecode-like manner

=head1 SYNOPSIS

  mimedecode [-d directory] < file

  mimedecode [-d directory] file...

=head1 DESCRIPTION

C<mimedecode> processes mail files using B<MIME::Parser> module.
It handles "multipart" messages and saves "attached" files
(i.e. parts with suggested file names) to their suggested names
in the directory specified after C<-d>, or the current directory if
C<-d> is not given.

=head1 BUGS

It needs the module installed.

Should probably consider more of headers in deciding if it really
is a useful attachment.

=head1 AUTHOR

Slapped together by Nick Ing-Simmons <nick@ni-s.u-net.com>

=cut


