Commits

Palmer, 2E0EOL  committed a3d4a67

Support -r for recursion.

  • Participants
  • Parent commits 01b56cf
  • Branches f/201202-palmer-recursion

Comments (0)

Files changed (1)

 use MongoDB;
 use Getopt::Std;
 use Data::Dumper;
+use File::Find;
 use boolean ':all';
 use strict;
 use warnings;
 use constant EXIT_SUCCESS => (0);
 use constant EXIT_FAILURE => (1);
 
+my ( %Wanted_Seen, %Wanted_Maildirs );
+
 sub Help($)
 {
 	my $detailed = false;
 		} elsif ( $o eq 'c' ) { # Collection name
 			print("-c <name>\n\tCollection name (A name of this Maildir (cumulative)\n");
 			$detailed = true;
+		} elsif ( $o eq 'r' ) { # Recursive
+			print("-r\n\tRecurse into all Maildirs within a path\n");
+			$detailed = true;
 		}
 	}
 
 		print("-d <dir>\n\tSpecified the path to the Maildir (for -i mode only)\n\n");
 		print("-c <name>\n\tCollection name (A name of this Maildir (cumulative))\n\n");
 		print("-? [<opt>]\n\tGet this help (optionally, specify arguments for detailed help)\n\n");
+		print("-r\n\tRecurse into all Maildirs within a path\n");
+	}
+}
+
+sub Wanted
+{
+	my @parts;
+	next unless ( $_ );
+	#print $File::Find::dir . ' -> ' . $_ . "\n";
+	return 0 if ( $Wanted_Seen{$File::Find::dir} ); # Skip directories seen (we don't care about contents)
+	$Wanted_Seen{$File::Find::dir} = 1; # Remember not to re-parse these directories.
+
+	@parts = split(m'/', $File::Find::dir);
+	if ( $parts[-1] eq 'cur' ) {
+		my $recons; # Reconsitituted path
+		pop(@parts); # Discard 'cur'
+		$recons = join('/', @parts);
+		$Wanted_Maildirs{$recons}++;
 	}
 }
 
 sub Import(@)
 {
 	my $total = 0;
+	my %Params = ( );
+	%Params = @_ if ( scalar(@_) && scalar(@_) % 2 == 0 );
+
+	if ( $Params{'Maildir'} ) {
+		$total += ImportMaildir(%Params);
+	} elsif ( $Params{'Dir'} ) {
+		find(
+			{	wanted => \&Wanted,
+				follow => 0
+			}, $Params{'Dir'}
+		);
+		%Wanted_Seen = ( ); # Don't need this temporary cache, it was used to avoid unnecessary
+		# string comparisons.
+
+		delete($Params{'Dir'});
+		foreach my $dir ( keys(%Wanted_Maildirs) ) {
+			$Params{'Maildir'} = $dir;
+			$total += ImportMaildir(%Params);
+		}
+	} else {
+		Carp::confess('No Maildir or Dir specified');
+	}
+
+	return $total;
+}
+
+sub ImportMaildir(@)
+{
+	my $total = 0;
 	my $mailbox = Mail::Box::Manager->new();
 
 	my %Params = ( );
 		Carp::confess('Missing required parameter - ' . $p);
 	}
 
+	printf("We\'re just parsing %s...\n", $Params{'Maildir'});
 	my $maildir = $mailbox->open(
 		folder => $Params{'Maildir'},
 		keep_dups => 0
 sub Main()
 {
 	my %opts;
-	getopts('?hic:d:s', \%opts);
+	getopts('?hic:d:sr', \%opts);
 	if ( $opts{'h'} || $opts{'?'} ) { Help(\%opts); return EXIT_FAILURE(); }
 	if ( $opts{'i'} ) {
-		Import(
+		my %importParams = (
 			Maildir => $opts{'d'} || '.',
+			Dir => $opts{'d'} || '.',
 			Collection => $opts{'c'} || 'dlmlstat'
 		);
+		if ( $opts{'r'} ) { # When recursive, use a Dir, not a Maildir
+			delete($importParams{Maildir});
+		} else { # And vice-versa, you cannot specify both.
+			delete($importParams{Dir});
+		}
+		Import(%importParams);
 	} else {
-		warn('Ignoring -d when not in -i mode') if ( $opts{'d'} );
+		foreach my $o ( ('d', 'r') ) {
+			next unless ( $opts{$o} );
+			warn(sprintf('Ignoring -%s when not in -i mode', $o));
+		}
 	}
 
 	if ( $opts{'s'} ) { # Stats mode