# NiupepaPlugin, 

package NiupepaPlugin;

use PagedImagePlugin;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa
no strict 'subs';

sub BEGIN {
    @NiupepaPlugin::ISA = ('PagedImagePlugin');
}

my $arguments =
    [ 
      { 'name' => "block_exp",
        'desc' => "{BasePlugin.block_exp}",
        'type' => "regexp",
        'deft' => &get_default_block_exp(),
        'reqd' => "no" }
];

my $options = { 'name'     => "NiupepaPlugin",
		'desc'     => "PagedImagePlugin variant for niupepa collection",
		'abstract' => "no",
		'inherits' => "yes",
		'args'     => $arguments };

sub new {
    my ($class) = shift (@_);
    my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
    push(@$pluginlist, $class);

    push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
    push(@{$hashArgOptLists->{"OptList"}},$options);

    my $self = new PagedImagePlugin($pluginlist, $inputargs, $hashArgOptLists);

    return bless $self, $class;
}

sub get_default_block_exp {
    my $self = shift (@_);

    return q^(images|text|meta\.txt$|\.issue$)^;
}


sub plugin_specific_process {
    my $self = shift(@_);
    my ($base_dir, $file,$doc_obj, $gli) = @_;
    print STDERR "in plugin specific process\n";
    my ($filename_full_path, $filename_no_path) =  &util::get_full_filenames($base_dir, $file); 
    my $topsection = $doc_obj->get_top_section();
    my ($full_dir) =  $filename_full_path =~ /^(.*?)([^\/\\]*)$/;
    print STDERR "$filename_full_path, $full_dir\n";
    my ($issuekey) = $filename_no_path =~ /^([^\.\_]*)/;
    my $commentary = &util::filename_cat($full_dir,"$issuekey.commentary");
    if (-e $commentary) {
	print STDERR "setting hascom 1 for $filename_no_path \n";
	$doc_obj->set_utf8_metadata_element ($topsection, "hascom", "1");
	$doc_obj->set_utf8_metadata_element ($topsection, "commentary", $issuekey . "commentary");
    } else {
	print STDERR "didn't find commentary $commentary\n";
    }
    my ($paper_id) = $filename_no_path;
    $paper_id =~ s/\.item//;
    my $abstract_OID = $paper_id . "abstract";
    my $abstract_file .= "$paper_id.abstract";
    my $afile = &util::filename_cat($full_dir, "abstracts", $abstract_file);
    if (-e $afile) {
	print STDERR "setting abstract $abstract_OID for $filename_no_path \n";
	$doc_obj->set_utf8_metadata_element ($topsection, 'abstract', $abstract_OID);
    }else {
	print STDERR "didn't find abstract $afile\n";
    }

    $doc_obj->set_utf8_metadata_element ($topsection, 'Title', &niupepautil::get_title_string($paper_id));
}
1;
