#!/usr/local/bin/perl

# wfsend
#
# Author: andy powell <a.powell@ukoln.ac.uk>
#
# $Id: wfsend,v 1.8 1997/04/25 20:51:11 lisap Exp $

=head1 NAME

wfsend - send a MIME encoded Warwick Framework container

=head1 USAGE

    wfsend [-d] -t to -s subject [ [-c|-a|-r] file ... ] ...

    -d - Debug mode
    -t - E-mail To: field
    -s - E-mail Subject: field
    -c - Start of container
    -a - Start of container holding alternative packages of metadata
    -r - Start of container holding an object (resource) and it's metadata

For example...

  wfsend -t a.powell@ukoln.ac.uk \
		-s "Three packages of metadata about a single object" \
		-c file1.sgml file1.pics file1.ukmarc
  wfsend -t a.powell@ukoln.ac.uk \
		-s "Object and 2 alternative packages of metadata" \
		-r file2.html -a file2.sgml file2.pics
  wfsend -t a.powell@ukoln.ac.uk -s "Two unrelated packages of metadata" \
		-c \
		-c file3.pics file3.dc \
		-c file4.sgml file4.pics

=head1 AUTHOR

andy powell, UKOLN, Feb 1997 (based on mimesend by Eryq)

=cut

use MIME::Entity;

$mailer = "Warwick Framework (MIME-tools) 1.1";
$boundary = "----------=_".scalar(time)."-$$-"."container-";
$count = 1; # start at 1, container 0 is the outer-most one
@mimetypefiles = ('/etc/mime.types', '/usr/local/lib/mime.types',
			'~/.mime.types', './mime.types');

foreach (@mimetypefiles) {
    read_mime_types($_);
}

# Get args:
while (@ARGV) {
    $_ = shift @ARGV;
    if (/^-t$/) {
	$to = shift @ARGV;
    }
    elsif (/^-s$/) {
	$subj = shift @ARGV;
    }
    elsif (/^-d$/) {
	$debug++;
    }
    elsif (/^-c$/) {
	&new_container('mixed');
    }
    elsif (/^-a$/) {
	&new_container('alternate');
    }
    elsif (/^-r$/) {
	&new_container('related');
    }
    else {
	my $path = $_;
	die "$path not found" unless (-f $path);
	my $type = file_to_type($path);
	die "No container for $path" unless $current;
	attach $current Type=>$type, 
		        Path=>$path,
		        Encoding=>(($type =~ m{^(text|message)/}i) 
				? '7bit' : 'base64');
    }
}

$to or die "missing [-t address]\n";
$top->head->add('To',$to);

$subj or die "missing [-s subject]\n";
$top->head->add('Subject',$subj);

if ($debug) {
    $top->print;
}
else {
    # Launch mailer and send message:
    open SENDMAIL, "|/usr/lib/sendmail -t -oi -oem" or die "open sendmail: $!";
    $top->print(\*SENDMAIL);
    close SENDMAIL;
    die "sendmail failed" if ($? >> 255);
}

exit 0;

sub read_mime_types {
    my ($file) = @_;

    open(IN, "<$file") || return;
    while (<IN>) {
	next if (/^#/ || /^\s*$/);
	chomp;
	my @w = split(/[\t ]+/);
	my $type = shift @w;
	foreach (@w) {
	    $Type{$_} = $type;
	}
    }
    close(IN);
}

sub file_to_type {
    my ($path) = @_;

    $_ = $path;
    s/^.*\.//;
    die "Unknown MIME type for $path" unless $Type{$_};
    return($Type{$_});
}

sub new_container {
    my ($subtype) = @_;

    my $t = $top ? 0 : 1;
    my $new = build MIME::Entity	Type=>"multipart/$subtype",
					Boundary=>$boundary.$count++,
					Top=>$t;
    if ($top) {
	$current = add_part $top $new;
    }
    else {
	$top = $new;
	$current = $new;
    }
}
