# plugin which recurses through directories processing
# each file it finds

package RecPlug;

use BasPlug;
use plugin;
use util;


BEGIN {
    @ISA = ('BasPlug');
}

sub new {
    my ($class) = @_;
    my $self = new BasPlug ();

    return bless $self, $class;
}

# return 1 if this class might recurse using $pluginfo
sub is_recursive {
    my $self = shift (@_);

    return 1;
}


# return number of files processed, undef if can't process
# Note that $base_dir might be "" and that $file might 
# include directories
sub read {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;

    my (@dir, $subfile);

    my $count = 0;

    # see if this is a directory
    $dirname = &util::filename_cat ($base_dir, $file);
    if (-d $dirname) {

	# read all the files in the directory
	opendir (DIR, $dirname) ||
	    die "ERROR - couldn't read directory $dirname\n";
	@dir = readdir (DIR);
	closedir (DIR);

	print STDERR "RecPlug: getting directory $dirname\n";

	# process each file
	foreach $subfile (@dir) {
	    last if (defined $maxdocs && $maxdocs =~ /\d/ && $count >= $maxdocs);

	    if ($subfile !~ /^\.\.?$/) {
		# note: metadata is not carried on to the next level
		$count += &plugin::read ($pluginfo, $base_dir, &util::filename_cat($file, $subfile),
					 {}, $processor, $maxdocs);
	    }
	}
	return $count;
    }

    # wasn't a directory, someone else will have to process it
    return undef;
}


1;
