Index: main/trunk/greenstone2/perllib/plugouts/MARCXMLPlugout.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugouts/MARCXMLPlugout.pm	(revision 28705)
+++ main/trunk/greenstone2/perllib/plugouts/MARCXMLPlugout.pm	(revision 28706)
@@ -28,4 +28,5 @@
 use strict;
 no strict 'refs';
+no strict 'subs'; # allow barewords (eg STDERR) as function arguments
 
 eval {require bytes};
@@ -44,5 +45,4 @@
 		   'desc' => "{MARCXMLPlugout.group}",
 		   'type' => "flag",
-                   'deft' => "0",
 		   'reqd' => "no",    
 		   'hiddengli' => "no"},
@@ -51,5 +51,5 @@
 		   'type' => "string",
 		   'deft' => "dc2marc-mapping.xml",
-		   'reqd' => "yes",    
+		   'reqd' => "no",    
 		   'hiddengli' => "no"},
 		 { 'name' => "xslt_file", 
@@ -81,47 +81,66 @@
     $self->{'buffered_output'} ="";
      
+    # xslt_file is checked in BasePlugout
+    # check the mapping file here
     my $full_path_to_mapping_file = &util::locate_config_file($self->{'mapping_file'});
     if (! defined $full_path_to_mapping_file) {
-	print STDERR "Can not find $self->{'mapping_file'}, please make sure you have supplied the correct file path\n";
+	print STDERR "Can not find mapping file $self->{'mapping_file'}, please make sure you have supplied the correct file path or put the file into the collection's etc or greenstone's etc folder\n";
 	die "\n";
     }
     $self->{'mapping_file'} = $full_path_to_mapping_file;
 
-   return bless $self, $class;
+    return bless $self, $class;
 }
 
-sub saveas {
+sub begin {
+
+    my $self= shift (@_);
+    if ($self->{'group'}) {
+	# all output goes into this file
+	my $output_dir = $self->get_output_dir();
+	&FileUtils::makeAllDirectories ($output_dir) unless -e $output_dir;
+
+	$self->{'short_doc_file'} = "marc.xml";
+    }
+}
+# override BasePlugout process
+sub process {
     my $self = shift (@_);
-    my ($doc_obj,$doc_dir) = @_;
+    my ($doc_obj) = @_;
 
-   
+    my $output_info = $self->{'output_info'};
+    return if (!defined $output_info);
+
+    $self->process_metafiles_metadata ($doc_obj);  
+    
     if ($self->{'group'}){
         $self->{buffered_output} .= $self->get_top_metadata_list($doc_obj)."\n"; 
-        return;
     }
-    
-    $self->process_metafiles_metadata ($doc_obj);
+    else {
+	# find out which directory to save to
+	my $doc_dir = $self->get_doc_dir($doc_obj);
+	my $output_file = &FileUtils::filenameConcatenate ($self->get_output_dir(), $doc_dir, "marc.xml");	  
+	$self->open_xslt_pipe($output_file,$self->{'xslt_file'});
+	
+	my $outhandler = $self->{'xslt_writer'}; 
+	$self->output_xml_header($outhandler, "MARCXML", 1);
+	print $outhandler $self->get_top_metadata_list($doc_obj);
+	$self->output_xml_footer($outhandler,"MARCXML");  
+	$self->close_xslt_pipe(); 
+	$self->{'short_doc_file'} = &FileUtils::filenameConcatenate ($doc_dir, "marc.xml");  
+    }
 
-    my $output_dir = $self->get_output_dir();
-    &FileUtils::makeAllDirectories ($output_dir) unless -e $output_dir;
-    
-    my $working_dir = &FileUtils::filenameConcatenate ($output_dir, $doc_dir);    
-    &FileUtils::makeAllDirectories ($working_dir) unless -e $working_dir;
 
-    my $output_file = &FileUtils::filenameConcatenate ($working_dir, "marc.xml");
- 
-    $self->open_xslt_pipe($output_file,$self->{'xslt_file'});
+     # write out data to archiveinf-doc.db
+    if ($self->{'generate_databases'}) {
+	$self->store_output_info_reference($doc_obj);    
+	$self->archiveinf_db($doc_obj); 
+    }
+    if ($self->{'group'}){
+  	$self->{'gs_count'}++; 
+	$self->{'group_position'}++;
+    }
+}
 
-    my $outhandler = $self->{'xslt_writer'}; 
-
-    $self->output_xml_header($outhandler, "MARCXML", 1);
-    print $outhandler $self->get_top_metadata_list($doc_obj);
-    $self->output_xml_footer($outhandler,"MARCXML");  
-    $self->close_xslt_pipe(); 
-
-    $self->{'short_doc_file'} = &FileUtils::filenameConcatenate ($doc_dir, "marc.xml");  
-
-    $self->store_output_info_reference($doc_obj);              
-}
 
 # returns a xml element of the form <MetadataList><Metadata name="metadata-name">metadata_value</Metadata>...</MetadataList>
@@ -154,9 +173,6 @@
     return unless $self->{'group'} and  $self->{buffered_output};
 
-    my $output_dir = $self->get_output_dir();
-    &FileUtils::makeAllDirectories ($output_dir) unless -e $output_dir;
+    my $output_file = &FileUtils::filenameConcatenate($self->get_output_dir(), $self->{'short_doc_file'});
 
-    my $output_file = &FileUtils::filenameConcatenate($output_dir, "marc.xml");
- 
     $self->open_xslt_pipe($output_file,$self->{'xslt_file'});
 
@@ -175,3 +191,4 @@
 }
 
+
 1;
