###########################################################################
#
# plugout.pm -- functions to handle using plugouts
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 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.
#
###########################################################################

package plugout;

use strict; # to pick up typos and undeclared variables...
no strict 'refs'; # ...but allow filehandles to be variables and vice versa
no strict 'subs';

require util;
use gsprintf 'gsprintf';

# global variables
my $stats = {'num_processed' => 0,
	     'num_blocked' => 0,
	     'num_not_processed' => 0,
	     'num_not_recognised' => 0,
	     'num_archives' => 0
	     };

#globaloptions contains any options that should be passed to all plugouts
my ($verbosity, $outhandle, $failhandle, $globaloptions);

sub load_plugout{
    my ($plugout) = shift @_;

    my $colplugdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugouts");
    unshift (@INC, $colplugdir); 
    
    my $plugoutname = shift @$plugout;

    # find the plugout
    my $customplugname;
    if (defined($ENV{'GSDLCOLLECTION'}))
    {
        $customplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "custom", $ENV{'GSDLCOLLECTION'}, 
						 'perllib', 'plugouts', "${plugoutname}.pm");
    }

    my $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, 'perllib', 'plugouts', 
					  "${plugoutname}.pm");
    my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'}, 'perllib', 'plugouts',
					  "${plugoutname}.pm");
    if (defined($customplugname) && -e $customplugname) { require $customplugname; }
    elsif (-e $colplugname) { require $colplugname; }
    elsif (-e $mainplugname) { require $mainplugname; }
    else {
	gsprintf($outhandle, "{plugout.could_not_find_plugout}\n",
		 $plugoutname);
	    die "\n";
    }

    # create a plugout object
    my ($plugobj);

    eval ("\$plugobj = new \$plugoutname([],\$plugout)");
    die "$@" if $@;

    return $plugobj;
}

1;
