#!/usr/bin/perl -w

use MIME::Entity;
use strict;

$::outfile='';
$::preambel=undef;
$::epilogue=undef;

@::parts=();
%::head=();
%::phead=();

sub help(;$)
{
	my $indent = shift || 8;
	for my $opt (sort(keys(%::opts))){
		my ($needopt,$fun,$parm,$desc)=@{$::opts{$opt}};
		my $l=$indent-length($opt);
		if($l<1){$l=1};
		print $opt.' ' x $l.$parm;
		$l+=length($opt)+length($parm);
		
		if($l>2*$indent-2){print("\n".' ' x (2*$indent-1))}
		else{print ' ' x (2*$indent-$l-1)}
		
		$l=78-2*$indent;
		while($desc ne ""){
			my ($a,$r);
			if(length($desc)>$l){
				if($desc=~/^(.{1,$l}) (.*)$/){
					($a,$r)=($1,$2);
				}else{
					$desc=~/^(.{1,$l})(.*)$/;
					($a,$r)=($1,$2);
				}
				$desc=$r;
			}else{
				$a=$desc;
				$desc="";
			}
			print "$a\n";
			if($desc){print ' ' x (2*$indent)}
		}
	}
	exit 0;
}


%::opts=(
'b' => [0, sub{1},
"","this option is accepted for compatibility"],

'c' => [1, sub{$::head{"Cc"}=shift},
"cc","specifies the CC address."],

'F' => [1, sub{$::head{"From"}=shift},
"from","specifies the From address."],

"i" => [1, sub{$::head{"Id"}=shift},
"<content-id>","specifies the content-id for the MIME entity. Must be a".
" legal content-id value, enclosed in angle brackets."],

'm' => [1, sub{$::head{"Type"}=shift},
"MIME-type","specifies the MIME content-type."],

's' => [1, sub{$::head{"Subject"}=shift},
"subject","specifies the Subject field."],

't' => [1, sub{$::head{"To"}=shift},
"to","specifies the To address."],

'S' => [1, sub{1},
"splitsize","accepted for compatibility"],

'o' => [1, sub{$::outfile=shift},
"outputfile","specifies that the output from metasend.pl should go to a".
" named file rather than be delivered as mail."],

'P' => [1, sub{$::preambel=shift},
"preamblefile",'specifies a file containing alternative text to be put in'.
' the "preamble" area of a MIME multipart message.'],

'L' => [1, sub{$::epilogue=shift},
"epiloguefile",'specifies a file containing alternative text to be put in'.
' the "epilogue" area of a MIME multipart message.'],

'/' => [1, sub{$::head{"Type"}="multipart/".shift},
"subtype",'specifies the use of a MIME multipart subtype other than "mixed".'],


'a' => [0, sub{$::phead{"Disposition"}="attachment"},
"","this mime part ist to be displayed as an attachement, not inline."],

'D' => [1, sub{$::phead{"Description"}=shift},
"description","specifies a string to be used as the Content-description".
" value."],

'e' => [1, sub{$::phead{"Encoding"}=shift},
"encoding",'specifies the encoding type. Must be either "base64",'.
' "quoted-printable", "7bit", or "x-uue". "7bit" means no encoding is'.
' performed.'],

'f' => [1, sub{my $f=shift; if($f eq '-'){$::phead{"Data"}=[<STDIN>];}
                            else{$::phead{"Path"}=$f}},
"filename","specifies the file containing the data, '-' for stdin."],

"I" => [1, sub{$::head{"Id"}=shift},
"<content-id>","specifies the content-id for the multipart entity being".
" created by metasend, if any. Must be a legal content-id value, enclosed".
" in angle brackets."],

'n' => [0, sub{push(@::parts,MIME::Entity->build(%::phead, Top=>0));
               %::phead=();},
"","specifies that an additional file is to be included. Before each use of".
" the -n option on the command line, the options -m and -f, at a minimum,".
" must have appeared, and must appear separately for each included file."],

'z' => [0, sub{1},
"","accepted for compatibility."],

'h' => [0, sub{help},
"","prints help"],

'E' => [0, sub{print STDERR "The option '-E' is not supported"},
"","unsupported, specifies that the file being included is already a full".
" MIME entity, and does not need to have any Content-* or other header".
" fields added."]

);

while(defined(my $p=shift(@ARGV))){
	$p=~/^-(.)(.*)/;
	my ($o,$v)=($1,$2);
	if(my $t=$::opts{$o}){
		my ($preq,$fun)=@{$t};
		if($preq && ($v eq "")){
			if(!defined($v=shift(@ARGV))){
				print STDERR "Option $o requires an argument\n";
				exit(2);
			}
		}
		&{$fun}($v);
	}
}


if(@::parts){
	if(!$::head{"Type"}){$::head{"Type"}="multipart/mixed"}
	if(%::phead){push(@::parts,MIME::Entity->build(%::phead, Top=>0))}
}else{
	%::head=(%::head,%::phead);
}

{
	if($::outfile){
		if($::outfile ne "-")
		{open(STDOUT,'>',$::outfile)||die("open($::outfile): $!")}
	}elsif($::head{"To"}||$::head{"Bcc"}){
		open(STDOUT,'|-','/usr/sbin/sendmail','-t');
	}
	
	my $ent=MIME::Entity->build(%::head);

	for(@::parts){$ent->add_part($_)}

	for my $t ([$::preambel,sub{$ent->preamble(@_)}],
	           [$::epilogue,sub{$ent->epilogue(@_)}]){
		my ($f,$fun)=@{$t};
		if(!defined($f)){next}
		if(!open(F,$f)){
			print STDERR "open($f): $!";
			exit 1;
		}
		&{$fun}([<F>]);
		close(F);
	}
	$ent->sync_headers(Length=>'COMPUTE');

	$ent->print(\*STDOUT);
	print("\n");
}

exit 0;
