# This recursive plugin processes all files in a table of contents (toc.txt)
# file. The toc.txt file should contain a list of filenames followed by 
# one or more subject classifications to which that document belongs. The 
# subject classifications should correspond to those in the first field 
# of the collections subject file if the Hierarchy classifier plugin is
# being used.

package TOCPlug;

use plugin;
use BasPlug;
use lang;
use doc;
use util;
use cfgread;

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

sub new {
    my ($class) = @_;
    $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 1 if processed, 0 if not processed
# Note that $base_dir might be "" and that $file might 
# include directories
sub read {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;

    my $tocfile = &util::filename_cat($base_dir, $file, "toc.txt");
    if (!-f $tocfile) {
	# not a directory containing a toc file
	return 0;
    }

    # found a toc.txt file
    print STDERR "TOCPlug: processing $tocfile\n";

    # read in the toc.txt
    my $list = &cfgread::read_cfg_file ($tocfile, undef, '^[^#]\w');

    # process each document
    foreach $docfile (keys (%$list)) {
	# note that $list->{$docfile} is an array reference
	$metadata->{'Subject'} = $list->{$docfile};
	&plugin::read ($pluginfo, $base_dir, $docfile, $metadata, $processor);
    }

    return 1; # was processed
}


1;
