# This plugin processes all books in a table of contents file.
# A toc.txt file contains two things: a subject classification and a
# list of file names.

package TOCPlug;

use plugin;
use BasPlug;
use lang;
use doc;

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;
}

sub read_toc_subject {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;

    my ($infoline, $line, @line);

    open (TOCFILE, "$base_dir$file") || 
	die "TOCPlug::read_toc_subject couldn't open $tocfile\n";
    $infoline = <TOCFILE>;     # first line is an info line
    my $doc_obj = new doc($file, "classification");
    $doc_obj->set_OID ("CLSU");

    while (defined ($line = <TOCFILE>)) {
	$line =~ s/^\#.*$//;   # remove comments
	$line =~ s/\cM|\cJ//g; # remove end-of-line characters
	@line = split(/\t/, $line);

	# remove spaces at the start and end of each field
	map { s/^\s+|\s+$//g; } @line;

	# if this is a classification entry add it to the classification file
	if ((scalar(@line) >= 2) && ($line[0] ne "") && ($line[1] ne "")) {
	    my $classifier = $self->int_classification ($line[0]); # convert leading letter to int
	    $doc_obj->create_named_section($classifier);
	    $doc_obj->add_metadata($classifier, "Title", $line[1]);
	}
    }

    close (TOCFILE);
    
    # process the classification file
    $processor->process($doc_obj);
}

# converts leading letter of a classification into its ascii equivalent
# i.e C.2.4 becomes 67.2.4
sub int_classification {
    my $self = shift (@_);
    my ($classification) = @_;
    my $c = ord($classification);
    $classification =~ s/^./$c/;

    return $classification;
}

sub read_toc_files {
    my $self = shift (@_);
    my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;

    my ($infoline, $line);
# the fields are in the following order (separated by tabs):
# Classification, Classification title, Title, 
# Language, Creator

    open (TOCFILE, "$base_dir$file") || 
	die "TOCPlug::read_toc_files couldn't open $tocfile\n";
    $infoline = <TOCFILE>;     # first line is an info line

    while (defined ($line = <TOCFILE>)) {
	next if ($line =~ $line =~ /^\#.*$/) || ($line !~ /\w/);
	$line =~ s/\cM|\cJ//g; # remove end-of-line characters
	@line = split(/\t/, $line);

	# remove spaces at the start and end of each field
	map { s/^\s+|\s+$//g; } @line;

	# if this is a file entry process it
	if ((scalar(@line) >= 5) && ($line[1] eq "") && ($line[4] ne "")) {
	    my $metadata = {'Subject' => $line[0],
			    'Title'   => $line[2],
			    'Language'=> &lang::english_to_iso639($line[3])};

	    $metadata->{'Creator'} = $line[5] if defined $line[5]; 


	    my $subimportfile = "$base_dir$line[4]";

	    &plugin::read ($pluginfo, $base_dir, $line[4], $metadata, $processor);
	}
    }

    close (TOCFILE);
}


# 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 = "$file/toc.txt";
    if (!(-f "$base_dir$tocfile")) {
	# not a directory containing a toc file
	return 0;
    }

    # found a toc.txt file

    print STDERR "TOCPlug: processing $tocfile\n";

    # create the subject classification document
    $self->read_toc_subject ($pluginfo, $base_dir, $tocfile, {}, $processor);
    
    # process each file within this table of contents file
    $self->read_toc_files ($pluginfo, $base_dir, $tocfile, {}, $processor);

    return 1; # was processed
}


1;
