Index: trunk/gsdl/bin/script/explode_metadata_database.pl
===================================================================
--- trunk/gsdl/bin/script/explode_metadata_database.pl	(revision 9038)
+++ trunk/gsdl/bin/script/explode_metadata_database.pl	(revision 9041)
@@ -10,12 +10,43 @@
 
 use parsargv;
-
-
+use printusage;
+
+my $arguments = 
+    [ { 'name' => "encoding",
+	'desc' => "{explode.encoding}",
+	'type' => "string",
+	'deft' => "iso_8859_1",
+	'reqd' => "no" } ,
+      { 'name' => "metadata_set",
+	'desc' => "{explode.metadata_set}",
+	'type' => "string",
+	'reqd' => "no" } ,
+      { 'name' => "plugin",
+	'desc' => "{explode.plugin}",
+	'type' => "string",
+	'reqd' => "yes" },
+      { 'name' => "filename_field",
+	'desc' => "{explode.filename_field}",
+	'type' => "string",
+	'reqd' => "no"}
+      ];
+	
+my $options = { 'name' => "explode_metadata_database.pl",
+		'desc' => "{explode.desc}",
+		'args' => $arguments };
+
+	    
 sub main
 {
+    my ($encoding, $metadata_set, $plugin, $filename_field);
+
+    
     # Parse command line arguments
     if (!parsargv::parse(\@ARGV, 
 			 'encoding/.*/iso_8859_1', \$encoding,
-			 'plugin/.*/', \$plugin)) {
+			 'metadata_set/.*/', \$metadata_set,
+			 'plugin/.*/', \$plugin,
+			 'filename_field/.*/', \$filename_field)) {
+	&PrintUsage::print_txt_usage($options, "{explode.params}");
 	die "\n";
     }
@@ -23,6 +54,30 @@
     # The metadata database filename is the first value that remains after the options have been parsed out
     my $filename = $ARGV[0];
-
-    # Create a new instance of the plugin specified...
+    if (!defined $filename || $filename !~ /\w/) { 
+	&PrintUsage::print_txt_usage($options, "{explode.params}");
+	print STDERR "You need to specify a filename";
+	die "\n";
+    }
+    # check that file exists
+    if (!-e $filename) {
+	print STDERR "File $filename doesn't exist...\n";
+	die "\n";
+    }
+    # check required options
+    if (!defined $plugin || $plugin !~ /\w/) {
+	&PrintUsage::print_txt_usage($options, "{explode.params}");
+	print STDERR "You need to specify a plugin";
+	die "\n";
+    }
+    
+    # check metadata set
+    if (defined $metadata_set && $metadata_set =~ /\w/) {
+	$metadata_set .= ".";
+    } else {
+	$metadata_set = "";
+    }
+
+    #check filename field
+    
     my $plugobj;
     require "$plugin.pm";
@@ -64,11 +119,29 @@
     my $record_number = 0;
     foreach $record_text (@metadata_records) {
-	$record_number = $record_number + 1;
-
+	
 	# 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);
-
-	my $document_file = sprintf("%4.4d", $record_number) . ".nul";
+	# try to get a file name
+	my $document_file;
+	if (defined $filename_field) {
+	    my $meta = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $filename_field);
+	    if (defined $meta) {
+		$meta =~ s/&\w{1,10};//g; # remove entities
+		$document_file = "$meta.nul";
+		my $num = 0;
+		while (-e "$documents_directory/$document_file") {
+		    $num++;
+		    $document_file = "$meta$num.nul";
+		}
+	    } else {
+		$record_number = $record_number + 1;
+		$document_file = sprintf("%4.4d", $record_number) . ".nul";
+	    }
+	} else {
+	    $record_number = $record_number + 1;
+	    $document_file = sprintf("%4.4d", $record_number) . ".nul";
+	}
+	
 	open(DUMMY_FILE, ">$documents_directory/$document_file");
 	close(DUMMY_FILE);
@@ -76,5 +149,5 @@
 	# Look at all the metadata assigned to this record
 	my $record_metadata = $doc_obj->get_all_metadata($cursection);
-	&write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata);
+	&write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
     }
 
@@ -84,5 +157,5 @@
 
     # Explode means just that: the original file is deleted
-    &util::rm($filename);
+    #&util::rm($filename);
 }
 
@@ -93,5 +166,6 @@
     my $file_name = shift(@_);
     my $record_metadata = shift(@_);
-
+    my $meta_prefix = shift(@_);
+    
     # Make $file_name XML-safe
     $file_name =~ s/</&lt;/g;
@@ -129,5 +203,5 @@
 	$value =~ s/>/&gt;/g;
 
-	print $metadata_xml_file "      <Metadata mode=\"accumulate\" name=\"$field\">$value</Metadata>\n";
+	print $metadata_xml_file "      <Metadata mode=\"accumulate\" name=\"$meta_prefix$field\">$value</Metadata>\n";
     }
 
