Index: /gsdl/trunk/bin/script/explode_metadata_database.pl
===================================================================
--- /gsdl/trunk/bin/script/explode_metadata_database.pl	(revision 16789)
+++ /gsdl/trunk/bin/script/explode_metadata_database.pl	(revision 16790)
@@ -16,4 +16,7 @@
 use parse2;
 use FileHandle;
+
+use File::Spec;
+use File::Basename;
 
 my $unicode_list =
@@ -95,5 +98,6 @@
 		'args' => $arguments };
 
-	    
+
+
 sub main
 {
@@ -131,4 +135,5 @@
     }
 
+    
     # There should one arg left after parsing (the filename)
     # Or the user may have specified -h, in which case we output the usage
@@ -177,87 +182,205 @@
     }
 
-    my $text = "";
-    # Use the plugin's read_file function to avoid duplicating code
-    $plugobj->read_file($filename, $input_encoding, undef, \$text);
-    # is there any text in the file??
-    die "\n" unless length($text);
-
     # Create a directory to store the document files...
-    my ($documents_directory_base) = ($filename =~ /(.*)\.[^\.]+$/);
-
-    # Split the text into records, using the plugin's split_exp
+    my ($exploded_base_dir) = ($filename =~ /(.*)\.[^\.]+$/);
+
+    my $orig_base_dir = &File::Basename::dirname($filename);
+
+
     my $split_exp = $plugobj->{'split_exp'};
-    my @metadata_records = split(/$split_exp/, $text);
-    print STDERR "Number of records: " . scalar(@metadata_records) . "\n";
-
-    # Write the metadata from each record to the metadata.xml file
-    my $record_number = 1;
-    my $documents_directory;
-    foreach my $record_text (@metadata_records) {
-	# Check if we need to start a new directory for these records
-	if (($record_number % $records_per_folder) == 1) {
-	    $documents_directory = $documents_directory_base;
-	    if (scalar(@metadata_records) > $records_per_folder) {
-		$documents_directory .= "." . sprintf("%8.8d", $record_number);
-	    }
-	    if (-d $documents_directory) {
-		die "Error: document directory $documents_directory already exists (bailing).\n";
-	    }
-	    &util::mk_dir($documents_directory);
-
-	    my $documents_metadata_xml_file = &util::filename_cat($documents_directory, "metadata.xml");
-	    if (-e $documents_metadata_xml_file) {
-		die "Error: documents metadata.xml file $documents_metadata_xml_file already exists (bailing).\n";
-	    }
-
-	    # Start the metadata.xml file
-	    open(METADATA_XML_FILE, ">$documents_metadata_xml_file");
-	    print METADATA_XML_FILE
-		"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" .
-		"<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">\n" .
-		"<DirectoryMetadata>\n";
-	}
-
-	# Use the plugin's process function to avoid duplicating code
+    if (defined $split_exp) {
+	# Read in file, and then split and process individual records
+
+	my $text = "";
+	# Use the plugin's read_file function to avoid duplicating code
+	$plugobj->read_file($filename, $input_encoding, undef, \$text);
+	# is there any text in the file??
+	die "\n" unless length($text);
+
+	# Split the text into records, using the plugin's split_exp
+
+	my @metadata_records = split(/$split_exp/, $text);
+	print STDERR "Number of records: " . scalar(@metadata_records) . "\n";
+	
+	# Write the metadata from each record to the metadata.xml file
+	my $record_number = 1;
+	foreach my $record_text (@metadata_records) {
+	    
+	    # Check if we need to start a new directory for these records
+	    my $documents_directory;
+	    check_need_new_directory($exploded_base_dir,$record_number,$records_per_folder,
+				     \@metadata_records,\$documents_directory);
+	    
+	    # Use the plugin's process function to avoid duplicating code
+	    my $doc_obj = new doc($filename, "nonindexed_doc");
+	    $plugobj->process(\$record_text, undef, undef, $filename, undef, $doc_obj, 0);
+	    
+	    
+	    # Try to get a doc to attach the metadata to
+	    # If no match found, create a dummy .nul file
+	    attach_metadata_or_nul_doc($document_field, $doc_obj, $record_number,
+				       $documents_directory, $orig_base_dir,
+				       $document_prefix, $document_suffix, $metadata_set, $verbosity);
+	    	    
+	    
+	    check_close_directory($record_number,$records_per_folder,\@metadata_records);
+	    
+	    $record_number = $record_number + 1;
+	}
+    }
+    else {
+	# Call metadata_read to sets up associated metadata 
+
+	my $pluginfo = undef;
+	my $metadata = {};
+
+	my $processor = undef;
+	my $maxdocs = undef;
+	my $gli = undef;
+
+	my $extrametakeys = {};
+	my $extrametadata = {};
+
+
+	$plugobj->metadata_read($pluginfo, "", $filename, $metadata, 
+				$extrametakeys, $extrametadata, $processor, $maxdocs, $gli);
+
+
+	my $documents_directory = need_new_directory($exploded_base_dir);
+
+	# Attach metadata to object
+	# => use the plugin's extra_metadata function to avoid duplicating code
 	my $doc_obj = new doc($filename, "nonindexed_doc");
-	$plugobj->process(\$record_text, undef, undef, $filename, undef, $doc_obj, 0);
-	# Get all the metadata assigned to this record
-	my $record_metadata = $doc_obj->get_all_metadata($doc_obj->get_top_section());
-	my $document_file;
-	
-	# try to get a doc to attach the metadata to
-	if (defined $document_field) {
-	    foreach my $pair (@$record_metadata) {
-		my ($field, $value) = (@$pair);
-		$value =~ s/\\\\/\\/g;
-
-		# Does this metadata element specify a document to obtain?
-		if ($field eq $document_field) {
-		    my $document_file_full = $document_prefix . $value . $document_suffix;
-		    $document_file = &obtain_document($document_file_full, $documents_directory, $verbosity);
-		    &write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
-		}
-	    }
-	}
-	# Create a dummy .nul file if we haven't obtained any documents for this record
-	if (not defined $document_file) {
-	    $document_file = sprintf("%8.8d", $record_number) . ".nul";
-	    open(DUMMY_FILE, ">$documents_directory/$document_file");
-	    close(DUMMY_FILE);
-	    &write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
-	}
-
-	if (($record_number % $records_per_folder) == 0 || $record_number == scalar(@metadata_records)) {
-	    # Finish and close the metadata.xml file
-	    print METADATA_XML_FILE "\n</DirectoryMetadata>\n";
-	    close(METADATA_XML_FILE);
-	}
-	$record_number = $record_number + 1;
-    }
+
+	$plugobj->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
+
+	# Try to get a doc to attach the metadata to
+	# If no match found, create a dummy .nul file
+	attach_metadata_or_make_nul_doc($document_field, $doc_obj, undef, 
+					$documents_directory, $orig_base_dir,
+					$document_prefix, $document_suffix, $metadata_set, $verbosity);
+
+
+	close_directory();
+    }
+
 
     # Explode means just that: the original file is deleted
     &util::rm($filename);
     $plugobj->clean_up_after_exploding();
-}
+
+}
+
+
+sub need_new_directory
+{
+    my ($exploded_base_dir) = @_;
+    
+    my $documents_directory = $exploded_base_dir;
+
+    if (-d $documents_directory) {
+	die "Error: document directory $documents_directory already exists (bailing).\n";
+    }
+    &util::mk_dir($documents_directory);
+
+    my $documents_metadata_xml_file = &util::filename_cat($documents_directory, "metadata.xml");
+    if (-e $documents_metadata_xml_file) {
+	die "Error: documents metadata.xml file $documents_metadata_xml_file already exists (bailing).\n";
+    }
+
+    # Start the metadata.xml file
+    open(METADATA_XML_FILE, ">$documents_metadata_xml_file");
+    print METADATA_XML_FILE
+	"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" .
+	"<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">\n" .
+	"<DirectoryMetadata>\n";
+
+    return $documents_directory;
+}
+
+sub check_need_new_directory
+{
+    my ($exploded_base_dir,$record_number, $records_per_folder,$metadata_records,
+	$documents_dir_ref) = @_;
+    
+
+    # Check if we need to start a new directory for these records
+    if (($record_number % $records_per_folder) == 1) {
+	my $documents_directory = $exploded_base_dir;
+
+	if (scalar(@$metadata_records) > $records_per_folder) {
+	    $documents_directory .= "." . sprintf("%8.8d", $record_number);
+	}
+
+	$$documents_dir_ref = need_new_directory($documents_directory);
+    }
+}
+
+
+
+
+
+sub attach_metadata_or_make_nul_doc
+{
+    my ($document_field, $doc_obj, $record_number, 
+	$documents_directory, $orig_base_dir,
+	$document_prefix, $document_suffix, $metadata_set, $verbosity) = @_;
+
+    my $record_metadata = $doc_obj->get_all_metadata($doc_obj->get_top_section());
+    my $document_file;
+
+    # try to get a doc to attach the metadata to
+    if (defined $document_field) {
+	foreach my $pair (@$record_metadata) {
+	    my ($field, $value) = (@$pair);
+
+	    $value =~ s/\\\\/\\/g;
+	    
+	    # Does this metadata element specify a document to obtain?
+	    if ($field eq $document_field) {
+		my $document_file_full = $document_prefix . $value . $document_suffix;
+
+		$document_file = &obtain_document($document_file_full, $documents_directory, $orig_base_dir, $verbosity);
+		&write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
+	    }
+	}
+	}
+    
+    # Create a dummy .nul file if we haven't obtained any documents for this record
+    if (not defined $document_file) {
+
+	if (defined ($record_number)) {
+	    $document_file = sprintf("%8.8d", $record_number) . ".nul";
+	}
+	else {
+	    $document_file = "doc.nul";
+	}
+
+	open(DUMMY_FILE, ">$documents_directory/$document_file");
+	close(DUMMY_FILE);
+	&write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
+    }
+
+}
+
+sub close_directory
+{
+    # Finish and close the metadata.xml file
+    print METADATA_XML_FILE "\n</DirectoryMetadata>\n";
+    close(METADATA_XML_FILE);
+
+}
+
+
+sub check_close_directory
+{
+    my ($record_number,$records_per_folder,$metadata_records) = @_;
+
+    if (($record_number % $records_per_folder) == 0 || $record_number == scalar(@$metadata_records)) {
+	# Finish and close the metadata.xml file
+	close_directory();
+    }
+}
+	    
 
 
@@ -322,7 +445,5 @@
 sub obtain_document
 {
-    my $document_file_full = shift(@_);
-    my $documents_directory = shift(@_);
-    my $verbosity = shift(@_);
+    my ($document_file_full,$documents_directory,$orig_base_dir,$verbosity) = @_;
     
     print STDERR "Obtaining document file $document_file_full...\n" if ($verbosity > 1);
@@ -332,5 +453,5 @@
 
     # Document specified is on the web
-    if ($document_file_full =~ /^http:/ || $document_file_full =~ /^ftp:/) {
+    if ($document_file_full =~ /^https?:/ || $document_file_full =~ /^ftp:/) {
 	$document_file_full =~ /([^\/]+)$/;
 	$document_file_name = $1;
@@ -351,15 +472,30 @@
     else {
 	my $dir_sep = &util::get_os_dirsep();
-	$document_file_full =~ /(.+$dir_sep)?(.*)$/;
+
+	$document_file_full =~ m/(.+$dir_sep)?(.*)$/;
 	$document_file_name = $2;
+
+
+	my $is_absolute = File::Spec->file_name_is_absolute($document_file_full);
+	print STDERR "doc file full = $document_file_full\n";
+
+	if (!$is_absolute) {
+	    $document_file_full 
+		= &util::filename_cat($orig_base_dir,$document_file_full);
+	}
+
 	$local_document_file = &util::filename_cat($documents_directory, $document_file_name);
 
-	# Only bother trying to copy the file if it contained some path information
-	if ($document_file_full ne $document_file_name) {
-	    &util::cp($document_file_full, $documents_directory);
-
-	    # Check the document was obtained successfully
-	    if (!-e $local_document_file) {
-		print STDERR "WARNING: Could not obtain document file $document_file_full\n";
+	&util::cp($document_file_full, $documents_directory);
+
+	# Check the document was obtained successfully
+	if (!-e $local_document_file) {
+	    print STDERR "WARNING: Could not obtain document file $document_file_full\n";
+	}
+	else {
+	    if ($document_file_full =~ m/^$orig_base_dir.*/) {
+		# file local to metadata record
+		# => copy has been made successfully, so remove original
+		&util::rm($document_file_full);
 	    }
 	}
@@ -377,2 +513,3 @@
 
 &main(@ARGV);
+
