#/usr/local/bin/perl
#
#	pearmail - use perl to send and extract uuencoded, t'archived mail
#
#	Daniel Smith, daniel@island.com, July, August 1991

#	This script was automatically generated by dansmith
#	running this command from /bin/tcsh:
#	/usr/local/bin/pearmail -helpthem middle@cse.uta.edu SoftList-2.0 SoftList-2.0
#
#	if this was mailed to you, and you are wondering what to do, follow
#	these simple steps:
#
#	1) save this message without any headers (or trim them off later).  The
#	very first line should say "#! /usr/bin/perl".  Save this to the
#	filename "pearmail".
#
#	2) make this an executable script by entering "chmod +x pearmail"
#	from a shell.
#
#	3) if someone sent you some files along with this script via mail,
#	save the messages out to separate files, and run the command
#	"pearmail -extract <the filenames>" on them.
#
#
#	History:
#
#	1.2 - Wed Aug 21 21:25:20 PDT 1991, daniel
#	One coup over the kremlin's nest fails
#	added -nice option for spreading the load out to sendmail
#       
#       
#       1.2.1  Thu May 21 23:03:27 CDT 1992  Bill Middleton
#       Lawrence Welk gone this week.  Granny is very upset.
#       Added system call to chmod 444 pearmail.tar.Z in the extraction
&usage unless @ARGV;
@command_line_args = @ARGV;	# for -helpthem

#	things to customize
$tmp_dir = "/tmp";
$how_we_mail = "mail -s";
$how_we_package = "tar cvf -";
$how_we_compress = "compress";
$how_we_encode = "uuencode pearmail.tar.Z";
$how_we_decode = "uudecode";
$how_we_split = "split -700 - $tmp_dir/pearmail.";

# more portable...
#$how_we_extract = "cat pearmail.tar.Z | uncompress | tar -xvf - ";
$how_we_extract = "zcat pearmail.tar.Z | tar xvf - ";

#	set up the way we'll process our options
%options = (
	'-helpthem', 	'&set_help',
	'-extract',	'&set_extract',
	'-x',		'&set_extract',
	'-e',		'&set_extract',
	'-nice',	'&set_nice',
	'-to_encode',	'&set_encode_method',
	'-to_package',	'&set_package_method',
	'-to_decode',	'&set_decode_method',
	'-to_extract',	'&set_extraction_method',
	'-h',		'&usage',
	'-help',	'&usage',
	'-usage',	'&usage',
);

#	*anything* to avoid "if elsif elsif" ad infinitum :-)
while ($_ = shift @ARGV) {
	if (/^-/) {
		eval ($options{$_}) || print "no such option: $_\n";
	} else {
		push (@our_args, $_);
	}
}

if ($we_are_extracting) {
	&arrange_filenames;
	&do_extract;
	exit;
}

#	otherwise, we're sending something...

if (($#our_args + 1) < 3) {
	print "not enough arguments, run \"pearmail -usage\" for help...\n";
	exit 0;
}

$recipient = shift @our_args;
$subject = shift @our_args;

if ($send_them_help) {
	&send_myself;
}

#	now all the rest of @our_args are files that we are sending

$how_to_send = join ('|', $how_we_package." @our_args", $how_we_compress,
			$how_we_encode);

open (SENDPIPE, "$how_to_send | split -700 - $tmp_dir/pearmail.$$.|")  ||
		die "can't set up pipe!: $!\n";
while (<SENDPIPE>) {
	print;		# output of pipe (tar) commands...
}
wait;

@files_we_send = <$tmp_dir/pearmail.$$.*>;
$cur_part = 1;
$total = ($#files_we_send + 1);
@fname_exts =  'xaa'..'xdz';
($fname_subject = $subject) =~ tr/ \t":;`'/_/s;
while ( <$tmp_dir/pearmail.$$.*> ) {
	$full_subject = "$subject part $cur_part of $total";
	open (MAILPIPE, "|$how_we_mail \"$full_subject\" $recipient")
			|| die "can't set up pipe!: $!\n";
	open (STUFF_TO_MAIL, $_);
	print MAILPIPE <<"PREAMBLE";

PEARMAIL_HEADER_START
	This is part $cur_part of $total

	This file was created with pearmail, and contains an archive
	of:
	@our_args

	Save this part, and any other parts, to separate files (it won't
	matter what names you give the files), and run:

	pearmail -extract <filenames>

	on them.  You will then have the decoded contents of the archive.

	SUBJECT: $subject
	FILENAME: $fname_subject.$fname_exts[$cur_part - 1]
	DECODE_METHOD: $how_we_decode
	EXTRACTION_METHOD: $how_we_extract

	The steps involved with creating the entire archive were:
	$how_to_send
PEARMAIL_HEADER_END
PREAMBLE

	print MAILPIPE "PEARMAIL_DATA_START\n";
	print MAILPIPE <STUFF_TO_MAIL>;
	print MAILPIPE "PEARMAIL_DATA_END\n";
	close (STUFF_TO_MAIL);
	unlink $_ || die "can not rm tmp files: !$\n";
	if ($we_are_nice && $cur_part < $total) {
		sleep 60; # don't overwhelm sendmail, which drives the load up
	}
	$cur_part++;
}


sub send_myself {
	open (MYSELF, $0) || die "can't open myself!: $!\n";
	open (MAILME,
	"|$how_we_mail \"pearmail script - sent with: $subject\" $recipient")
					|| die "can't set up pipe!: $!\n";

	while (<MYSELF>) {
		if ($. == 6) {
			print MAILME "
#	This script was automatically generated by $ENV{'USER'}
#	running this command from $ENV{'SHELL'}:
#	$0 @command_line_args
#
#	if this was mailed to you, and you are wondering what to do, follow
#	these simple steps:
#
#	1) save this message without any headers (or trim them off later).  The
#	very first line should say \"#! /usr/bin/perl\".  Save this to the
#	filename \"pearmail\".
#
#	2) make this an executable script by entering \"chmod +x pearmail\"
#	from a shell.
#
#	3) if someone sent you some files along with this script via mail,
#	save the messages out to separate files, and run the command
#	\"pearmail -extract <the filenames>\" on them.
#
";
		}
		print MAILME;
	}
}

sub set_help {
	$send_them_help = "yep";
}

sub set_extract {
	$we_are_extracting = "yep";
}

sub set_nice {
	$we_are_nice = "yep";
}

sub arrange_filenames {

	$need_decode_method = 0;
	$need_extraction_method = 0;


	if ($#our_args < 0) {
		die "you haven't told me which files to extract from...\n";
	}
	foreach $raw_file (@our_args) {
		open (RAW, $raw_file) || warn "cannot open $raw_file: $!\n";
		while (<RAW>) {
			next unless /PEARMAIL_HEADER_START/;
			last;
		}
		if (eof (RAW)) {
			print "can't find pearmail header in $raw_file\n";
		} else {
			while (<RAW>) {
				if (/PEARMAIL_HEADER_END/) {
					last;
				}
				if (/FILENAME: /o) {
					chop ($_);
					($skip, $all_raw_files{$raw_file}) =
							split (/ /, $_);
				}
				if (/DECODE_METHOD: /o &&
					$need_decode_method == 0) {
					chop ($_);
					($skip, @how_we_decode) =
							split (/ /, $_);
					$need_decode_method == 1;
				}
				if (/EXTRACTION_METHOD: /o &&
					$need_extraction_method == 0) {
					chop ($_);
					($skip, @how_we_extract) =
							split (/ /, $_);
					$need_extraction_method == 1;
					last;
				}
			}
		}
	}
	print "decode method: \"$how_we_decode\"\n";
	print "extraction method: \"$how_we_extract\"\n";

	foreach $old_name (keys %all_raw_files) {
		print "$old_name becomes $all_raw_files{$old_name} ....\n";
		close RAW;
		rename ($old_name, $all_raw_files{$old_name});
	}
}

sub do_extract {
	$| = 1;
	#open (DEBUG_FILE, "> debug") || die "can't set up debug file!: $!\n";
	open (DECODE_PIPE, "|$how_we_decode") ||
				die "can't set up pipe!: $!\n";

	foreach $workfile (sort (values %all_raw_files)) {
		print "now working with $workfile\n";

		open (CUR_WORK_FILE, $workfile) ||
				die "can't open $workfile!: $!\n";
		while (<CUR_WORK_FILE>) {
			next unless /PEARMAIL_DATA_START/o;
			last;	# ok, now we want the next line...
		}
		while (<CUR_WORK_FILE>) {
			last if /PEARMAIL_DATA_END/;
			print DECODE_PIPE;
			# print DEBUG_FILE;
		}
		close (CUR_WORK_FILE);
	}

	# very important to have this...flush out the end of the tar,,,
	close (DECODE_PIPE);
        system("chmod a+r pearmail.tar.Z");
        open (EXTRACT_PIPE, "$how_we_extract|") ||
				die "can't set up pipe!: $!\n";
	while (<EXTRACT_PIPE>) {
		print;
	}
}

sub set_encode_method {

}

sub set_package_method {

}

sub set_decode_method {

}

sub set_extraction_method {
	&opt_arg;
	$how_we_extract = $method;
}

sub opt_arg {
	($method = shift @ARGV) ||  die "need a method for this...";
}

sub usage {
	print "Usage: pearmail [options] person@somewhere \"Subject\" files.

pearmail is used to send groups of files to someone.  By default,
a tar file is created, uuencoded, and sent in parts through mail.
The recipient then saves those parts to separate files (with any names
they want), and runs pearmail -extract on them.

options:
	-helpthem		forward a copy of this script
	-extract		unarchive the files
	-nice			huge archives sent one message per minute
examples:
	pearmail -helpthem daniel@island.com \"diffs to pearmail\" diffs
	pearmail gumby@claytown.com \"blockhead jokes\" bjokes
	pearmail -nice lucky_user@some_site \"big_package.tar\" big.tar

	The first example sends the files to someone, and also sends this
script, giving you an easy way to give this to them.
	The second example is everyday usage; sending to some one who already
has pearmail.
	The third example is used when you are going to send a huge amount
of mail, and want to spread the load out (one message per minute).
	When someone gets their mail, they may save the parts to any file
names they wish.  They then run \"pearmail -extract files\" to get the archive
extracted.
";
	exit 0;
}
__END__