# functions to handle using the plugins

package plugin;


sub load_plugins {
    my ($collection, $plugin_list) = @_;
    my @plugin_objects = ();

    foreach $plugin (@$plugin_list) {
	# find the plugin
	if (-e "$ENV{'GSDLHOME'}/collect/$collection/perllib/plugins/${plugin}.pm") {
	    require "$ENV{'GSDLHOME'}/collect/$collection/perllib/plugins/${plugin}.pm";

	} elsif (-e "$ENV{'GSDLHOME'}/perllib/plugins/${plugin}.pm") {
	    require "$ENV{'GSDLHOME'}/perllib/plugins/${plugin}.pm";

	} else {
	    die "ERROR - couldn't find plugin $plugin\n";
	}

	# create a plugin object
	my ($plugobj);
	eval ("\$plugobj = new $plugin()");
	die "$@" if $@;

	# add this object to the list
	push (@plugin_objects, $plugobj);
    }

    return \@plugin_objects;
}

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

    # pass this file by each of the plugins in turn until one
    # is found which will process it
    foreach $plugobj (@$pluginfo) {
	if ($plugobj->read($pluginfo, $base_dir, $file, 
			   $metadata, $processor)) {
	    return;
	}
    }

    print STDERR "WARNING - no plugin could process $base_dir$file\n";
}


1;
