# From tumuc!fauern!ira.uka.de!smurf!urlichs Mon Jul  9 14:17:00 1990
# Article 1289 of alt.sources:
# Path: lan!tumuc!fauern!ira.uka.de!smurf!urlichs
# From: urlichs@smurf.sub.org (Matthias Urlichs)
# Newsgroups: alt.sources,comp.lang.perl
# Subject: Re: uumerge.pl
# Message-ID: <qskfe2.[75@smurf.sub.org>
# Date: 8 Jul 90 21:17:44 GMT
# References: <1990Jul7.182135.19069@alembic.acs.com>
# Organization: University of Karlsruhe, FRG
# Lines: 105
# Xref: lan alt.sources:1289 comp.lang.perl:1605

# In alt.sources, article <1990Jul7.182135.19069@alembic.acs.com>,
#   csu@alembic.acs.com (Dave Mack) writes:
# < #
# < # TODO: modify to allow more than one collection of files on
# < #     command line.
# < #
#
# Well, here's my version; after merging and uudecoding N files per hand I
# decided that enough was enough. (You might guess what newsgroup I'm talking
# about... :-)
#
# It also isn't perturbed if the occasional line in there does start with M.
#
# Warning: My programming style is atrocious, and my Perl style doubly so.
# Especially, the $x? variables really should be an array, the number of M lines
# required should be an option, and the M itself also.

#!/usr/local/bin/perl
$files = 0;
$isopen = 0;
$file = "";
$lines = 0;

$ARGV[$[] = "-" unless ($#ARGV >= $[);

sub nextline {
   local($next) = (0);
   if ($file eq "") { $next = 1; }
   else {
      $_ = <FILE>;
      if ($_ eq "") { $next = 1;  }
   }
   if ($next) {
      $file = $ARGV[$[];
      if ($#ARGV < $[) {
	 $_ = "";
      } elsif ($#ARGV >= $[) {
	 shift @ARGV;
	 print STDERR "Reading $file\n";
	 if ($file eq "-") {
	    open (FILE,"<&STDIN");
	 } else {
	    open (FILE,$file);
	 }
	 $_ = &nextline;
      } else {
	 $_ = "";
      }
   }
   $_;
}

loop: while(($_ = &nextline) ne "") {
    scan: while (1) {
       if (/^begin\s\d\d\d\s(.*)\s*$/) {
	  print STDERR "Writing $1\n";
	  open (OUT,"|uudecode");
	  print OUT $_;
	  $files++;
	  $isopen = 1;
	  last scan;
       }
    } continue {
       &nextline;
       last loop if ($_ eq "");
    }

    $isuu = 3;
    lines: while (($_ = &nextline) ne "") {
       last if /^end/;
       if (/^M/ && $isuu > 2) {
          print OUT $x3, $x2, $x1, $_;
          $x3 = ""; $x2 = ""; $x1 = "";
	  $lines++;
          next lines;
       } else {
          $x3 = $x2; $x2 = $x1; $x1 = $_;
	  if (/^M/) {
	    $isuu++;
	  } else {
	    $isuu = 0;
	  }
       }
    }

    print STDERR "$lines lines.\n";
    if (/^end/ && $isopen) {
        print OUT $x3, $x2, $x1, $_;
        close(OUT);
	$isopen = 0;
    } elsif ($isopen) {
	close(OUT);
	die "End of file not found";
    } else {
	die "No files found";
    }
    $lines = 0;
}

print STDERR "$files files found.\n";
exit 0;

# --
# Matthias Urlichs -- urlichs@smurf.sub.org -- urlichs@smurf.ira.uka.de
# Humboldtstrasse 7 - 7500 Karlsruhe 1 - FRG -- +49+721+621127(Voice)/621227(PEP)
