#!/usr/bin/perl
#
#   tcb name [files]...
#
# Tar+Compress+Btoa
#
# This runs tar on the files, compresses the archive, runs the result through
# btoa  and finally chops the result up into 50K chunks.  Their names will be
# the "name" args with "01", "02", ... appended. The output is a set of files
# that  should be emailable anywhere undamaged.  To unpack it, you need to do
# the inverse operations: Use sh to reconstruct the btoa-encoded archive, run
# atob  on  the  result  to  build  the .Z file, uncompress it to get the tar
# archive, and feed the result tar to extract the files. The files have names
# that end with 01, 02, 03, ...
#
# See the aut program for automatic unpacking at the receiving end.   There's
# also a tce program that uses uuencode rather than btoa; it is probably more
# widely usable, but can't be  unpacked  by  a  single  program  due  to  the
# difficulty  in stripping off email junk, and also the near impossibility of
# determining where uudecode puts its output.
#
$name = shift || &usage;
$count = $limit = 50000;
#open(bout,">bout");
open(data, "tar cf - @ARGV | compress | btoa |")
	|| die "Can't run tar|compress|btoa";
#
while (<data>) {
	&newfile if (($count += length) > $limit);
#	print bout;
	print outfile;
}
exit $?;
#
sub newfile {
	print outfile "---end beef\n"
		if ($filnum > 0);
	$suffix = sprintf("%02d",++$filnum);
	open(outfile,">$name.$suffix")
		|| die "$$: Can't write to \"$name.$suffix\"";
	$count = 0;
	print outfile "Subject: $name.$suffix [tar+compress+btoa]\n\n";
	print outfile "---start beef\n"
		if ($filnum > 1);
}
#
sub usage {
	die "Usage: $0 name [file]...";
}
