[alt.sources] uumerge.perl - merge and uudecode split uuencoded files

csu@alembic.acs.com (Dave Mack) (06/10/90)

Enclosed is a perl script called uumerge, which takes a collection
of split uuencoded files, strips off the e-mail garbage, signatures
and other drivel, and pipes the resulting data stream through uudecode.
The files must be in the correct order on the command line, must be
uuencoded using the "M" default (61 characters per line), and must
form a complete set - the first file must contain the "begin" line,
the last file must contain the "end" line, and must form only one
uuencoded file. Please read the comments at the beginning of the
script before using.

Example: I saved the three-part uuencoded gif posting of glynn in
alt.sex.pictures as glynn.gif.uu.part[1-3]. The following command
produced a useable glynn.gif file:

		uumerge glynn.gif.uu.*

The main advantage of this script over the others that have been 
posted to alt.sex.pictures so far is that it doesn't care how the
poster delimited the start and end of the uuencoded data - it doesn't
look for specially formatted "cut" lines. It relies on the uuencoded
data being in a specific form, which every uuencoded posting to asp
has had so far.

I'd appreciate reports of corrections and improvements. I leave it
to someone else to write the sh/sed/awk version of this.

-- 
Dave Mack
csu@alembic.ACS.COM

-------------------------------- cut here --------------------------------
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  uumerge
# Wrapped by csu@alembic on Sat Jun  9 13:38:55 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f uumerge -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"uumerge\"
else
echo shar: Extracting \"uumerge\" \(3509 characters\)
sed "s/^X//" >uumerge <<'END_OF_uumerge'
X#! /usr/local/bin/perl
X#
X# Combine split uuencoded files into a single data stream with
X# e-mail garbage removed and pipe into uudecode. The uuencoded
X# files must be in the correct order on the command line - in
X# particular the first file must contain the "begin" line and
X# the last file must contain the "end" line.
X#
X# WARNING: this code relies on uuencode putting out all lines
X# of the form "M[61 ASCII characters]\n" for every line of the
X# file except the last few before the "end" line. If you come
X# across a uuencoded file that doesn't do this, you'll need to
X# modify the code to handle it.
X#
X# DISCLAIMER: You use this code at your own risk. Also, don't
X# take this is as a sterling example of Perl programming. Corrections
X# and improvements welcome. You may do whatever you like with this
X# code as long as you leave in some reminder of who the original
X# culprit^H^H^H^H^H^H^Hauthor was.
X#
X# Usage: uumerge filename [filename...]
X# Requires Perl 3.0 - my copy is at patchlevel 18
X#
X# Dave Mack csu@alembic.ACS.COM
X#
X# TODO: modify to allow more than one collection of files on
X#	command line.
X#
X# KNOWN BUGS: 
X#
X# If some bozo puts a line beginning with "M" in the body of one
X# of the intermediate/last chunks, uumerge will assume that uuencoded
X# part starts there.
X#
X# If the last chunk only contains the last two or three lines of
X# the uuencoded file (the ones that don't start with "M"), uumerge
X# will die.
X
Xif ($#ARGV < 0 ) {
X	print "Usage: uumerge filename [filename...]\n";
X	exit 1;
X}
X
X# if we only have one file, pump it straight into uudecode and die
Xif ( $#ARGV == 0 ) {
X	system("cat $ARGV[0] | uudecode");
X	exit 0;
X}
X$| = 1;
X# open a pipe into uudecode
Xopen(DECO,"|uudecode") || die "Can't pipe into uudecode\n";
X
X# process the first file - make sure we have a "begin" line
X
Xopen(FIRST,"<$ARGV[0]") || die "Can't open $ARGV[0] for input\n";
X
Xwhile ( <FIRST> ) {
X	# skip past everything before the "begin" line
X	next unless /^begin [0-9]/;
X	last;
X}
Xdie "First file on command line doesn't contain \"begin\"\n" if eof(FIRST);
X	
Xprint DECO $_; # the begin line
X
X# the remaining "real" uuencoded lines in this file should begin with "M"
Xwhile ( <FIRST> ) {
X	if ( /^M/ ) {
X		print DECO $_;
X	}
X	else {
X		last;
X	}
X}
X
X# done with the first file
Xclose(FIRST);
X
X# do all except the last file
X$maxindex = $#ARGV;
X$curr = 1;
X
Xwhile ( $curr < $maxindex ) {
X	open(CURR,"<$ARGV[$curr]") || die "Can't open $ARGV[$curr]\n";
X	# skip the header junk
X	while ( <CURR> ) {
X		next unless /^$/;
X		last;
X	}
X	# at the body of the message - start looking for /^M/
X	while ( <CURR> ) {
X		next unless /^M/;
X		last;
X	}
X	die "$ARGV[$curr] isn't a uuencoded file\n" if eof(CURR);
X	# OK, we're at the start of the good stuff (probably)
X	print DECO $_;
X	while ( <CURR> ) {
X		if (/^M/) {
X			print DECO $_;
X		}
X		else {
X			last;
X		}
X	}
X	# done with current file
X	close(CURR);
X	$curr++;
X}
X
X# time to do the last file in the set
X$curr = $maxindex;
Xopen(CURR,"<$ARGV[$curr]") || die "Can't open $ARGV[$curr]\n";
X# skip the header junk
Xwhile ( <CURR> ) {
X	next unless /^$/;
X	last;
X}
X# at the body of the message - start looking for /^M/
Xwhile ( <CURR> ) {
X	next unless /^M/;
X	last;
X}
X# OK, we're at the start of the good stuff (probably)
Xprint DECO $_;
Xwhile ( <CURR> ) {
X	print DECO $_ unless /^end/;
X	if ( /^end/ ) {
X		print DECO $_;
X		last;
X	}
X	die "Last file on command line doesn't contain \"end\"\n" if eof(CURR);
X}
X# done with final file
Xclose(CURR);
X# close the pipe to uudecode and exit
Xclose(DECO);
Xexit(0);
END_OF_uumerge
if test 3509 -ne `wc -c <uumerge`; then
    echo shar: \"uumerge\" unpacked with wrong size!
fi
chmod +x uumerge
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0