###########################################################################
#
# NULPlug.pm -- Plugin for dummy (.nul) files
#
# A component of the Greenstone digital library software from the New
# Zealand Digital Library Project at the University of Waikato, New
# Zealand.
#
# Copyright (C) 2005 Katherine Don
# Copyright (C) 2005 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################

# NULPlug - a plugin for dummy files

# This is a simple Plugin for importing dummy files, along with
# their metadata.  A fictional document will
# be created for every such file, and the metadata added to it.

# This is used mainly for the null files resulting from exploding metadata
# databases

package NULPlug;

use BasPlug;
use parsargv;


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

#my $arguments =
#    [ 
#      ];

my $options = { 'name'     => "NULPlug",
		'desc'     => "{NULPlug.desc}",
		'abstract' => "no",
		'inherits' => "yes" };
	#	'args'     => $arguments };


sub new {
    my ($class) = @_;
    my $self = new BasPlug ($class, @_);
    $self->{'plugin_type'} = "NULPlug";
    # 14-05-02 To allow for proper inheritance of arguments - John Thompson
    my $option_list = $self->{'option_list'};
    push( @{$option_list}, $options );

    if (!parsargv::parse(\@_, 
			 "allow_extra_options")) {
	print STDERR "\nIncorrect options passed to NULPlug, check your collect.cfg configuration file\n";
	$self->print_txt_usage("");  # Use default resource bundle
        die "\n";
    }


    return bless $self, $class;
}

sub get_default_process_exp {
    return '(?i)\.nul$';
}

# The NULPlug read() function. This function does all the right
# things to make general options work for a given plugin.  NULPlug
# overrides read() because there is no need to read the actual text of
# the file in, because the contents of the file is not text...
#
#
# 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, $gli) = @_;

    my $outhandle = $self->{'outhandle'};

    # Make sure we're processing the correct file
    my $filename = &util::filename_cat($base_dir, $file);
    return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
    if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
	return undef;
    }
    print STDERR "<Processing n='$file' p='NULPlug'>\n" if ($gli);
    print $outhandle "NULPlug processing \"$filename\"\n"
	    if $self->{'verbosity'} > 1;

    #if there's a leading directory name, eat it...
    $file =~ s/^.*[\/\\]//;
    
    # create a new document
    my $doc_obj = new doc ($filename, "indexed_doc");
    $doc_obj->set_OIDtype ($processor->{'OIDtype'});    
    #$doc_obj->set_OIDtype ("incremental");
    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
    $doc_obj->add_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($file)); # set the filename as Source metadata to be consistent with other plugins
    $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));

    #create an empty text string so we don't break downstream plugins 
    my $text = &gsprintf::lookup_string("{BasPlug.dummy_text}");
    
    # include any metadata passed in from previous plugins 
    my $section = $doc_obj->get_top_section();
    $self->extra_metadata ($doc_obj, $section, $metadata);
    
    $self->title_fallback($doc_obj,$section,$file);
    
    # do plugin specific processing of doc_obj
    unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
	print STDERR "<ProcessingError n='$file'>\n" if ($gli);
	return -1;
    }

    # do any automatic metadata extraction
    $self->auto_extract_metadata ($doc_obj);

    # add an OID
    $doc_obj->set_OID();
    $doc_obj->add_text($section, $text);
    
    # process the document
    $processor->process($doc_obj);

    $self->{'num_processed'} ++;
    return 1;
}


# NULPlug processing of doc_obj.  In practice we don't need to do
# anything here because the read function takes care of everything.

sub process {
    my $self = shift (@_);
    my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
    my $outhandle = $self->{'outhandle'};
    
    return 1;
}


1;











