Index: trunk/gsdl/perllib/docsave.pm
===================================================================
--- trunk/gsdl/perllib/docsave.pm	(revision 8090)
+++ trunk/gsdl/perllib/docsave.pm	(revision 8094)
@@ -27,4 +27,6 @@
 # archives directory of a collection (as xml)
 
+use strict;
+no strict 'refs';
 
 package docsave;
@@ -38,5 +40,5 @@
 
 sub BEGIN {
-    @ISA = ('docproc');
+    @docsave::ISA = ('docproc');
 }
 
@@ -56,5 +58,5 @@
     $self->{'gs_count'} = 0;
 
-    $self->{'outhandle'} = STDERR;
+    $self->{'outhandle'} = 'STDERR';
     $self->{'outhandle'} = $outhandle if defined $outhandle;
     # set a default for the archive directory
@@ -89,106 +91,110 @@
     if ($self->{'groupsize'} > 1) {
 	$self->group_process ($doc_obj);
+	return;
+    }
+
+    # groupsize is 1 (i.e. one document per XML file) so sortmeta 
+    # may be used
 	
+    my $OID = $doc_obj->get_OID();
+    $OID = "NULL" unless defined $OID;
+
+    # get document's directory
+    my $doc_dir = $self->get_doc_dir ($OID);
+     
+    
+    # copy all the associated files, add this information as metadata
+    # to the document
+    $self->process_assoc_files ($doc_obj, $doc_dir);
+	
+    my $doc_file 
+	= &util::filename_cat ($self->{'archive_dir'}, $doc_dir, "doc.xml");
+      
+    #***define doctxt.xml file
+    my $doc_txt_file
+	= &util::filename_cat ($self->{'archive_dir'}, $doc_dir,"doctxt.xml");
+    my $working_dir
+	=&util::filename_cat ($self->{'archive_dir'}, $doc_dir);
+        
+    #***define docmets.xmlfile
+    my $doc_mets_file
+	= &util::filename_cat ($self->{'archive_dir'},$doc_dir, "docmets.xml");
+ 
+    my $short_doc_file;
+    my $save_as = $self->{'saveas'} || "GA";
+    if ($save_as eq "GA") {
+	$short_doc_file = util::filename_cat ($doc_dir, "doc.xml");
+    } elsif ($save_as eq "METS") {
+	#my $short_txt_doc_file=&util::filename_cat ($doc_dir, "doctxt.xml");
+	$short_doc_file=&util::filename_cat ($doc_dir, "docmets.xml");
     } else {
-	# groupsize is 1 (i.e. one document per XML file) so sortmeta 
-	# may be used
+	return;
+    }
+    # save for later (for close_file_output())
+    $self->{'short_doc_file'}=$short_doc_file;
+
+    if ($save_as eq "GA") {
+	if (!open (OUTDOC, ">$doc_file")) {
+	    print $outhandle "docsave::process could not write to file $doc_file\n";
+	    return;
+	}
+	    
+	# save this document
+	$self->output_xml_header('docsave::OUTDOC');
+	$doc_obj->output_section('docsave::OUTDOC',
+				 $doc_obj->get_top_section());
+	$self->output_xml_footer('docsave::OUTDOC');
+
+	close OUTDOC;
+    } elsif ($save_as eq "METS") {
+	# save the document without metadata:doctxt.xml
+
+	if (!open(OUTDOC_TXT, ">$doc_txt_file")){
+	    print $outhandle "docsave::process could not write to file $doc_mets_file\n";
+	    return;
+	}
+
+	$self->output_txt_xml_header('docsave::OUTDOC_TXT');
+	$doc_obj->output_txt_section('docsave::OUTDOC_TXT', $doc_obj->get_top_section());
+	#$self->output_txt_xml_footer('docsave::OUTDOC_TXT');
 	
-	my $OID = $doc_obj->get_OID();
-	$OID = "NULL" unless defined $OID;
-
-
-	# get document's directory
-	my $doc_dir = $self->get_doc_dir ($OID);
-     
-    
-	# copy all the associated files, add this information as metadata
-	# to the document
-	$self->process_assoc_files ($doc_obj, $doc_dir);
-	
-	my $doc_file 
-	    = &util::filename_cat ($self->{'archive_dir'}, $doc_dir, "doc.xml");
-      
-        #***define doctxt.xml file
-        my $doc_txt_file
-            = &util::filename_cat ($self->{'archive_dir'}, $doc_dir,"doctxt.xml");
-	my $working_dir
-            =&util::filename_cat ($self->{'archive_dir'}, $doc_dir);
-        
-        #***define docmets.xmlfile
-        my $doc_mets_file
-            = &util::filename_cat ($self->{'archive_dir'},$doc_dir, "docmets.xml");
- 
-        my $short_doc_file = &util::filename_cat ($doc_dir, "doc.xml");
-	#my $short_txt_doc_file=&util::filename_cat ($doc_dir, "doctxt.xml");
-        my $short_mets_doc_file=&util::filename_cat ($doc_dir, "docmets.xml");
-	
-	my $save_as = $self->{'saveas'};
-
-	if ($save_as eq "GA") {
-	    if (!open (OUTDOC, ">$doc_file")) {
-		print $outhandle "docsave::process could not write to file $doc_file\n";
-		return;
-	    }
+	# Convert doctxt.xml file to docmets.xml
+	if (!open(OUTDOC_METS,">$doc_mets_file")){
+	    print $outhandle "docsave::process could not write to file $doc_mets_file\n";
+	    return;
+	}
 	    
-	    # save this document
-	    $self->output_xml_header('docsave::OUTDOC');
-	    $doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
-	    $self->output_xml_footer('docsave::OUTDOC');
-
-	    close OUTDOC;
-	}
-	elsif ($save_as eq "METS") {
-	    # save the document without metadata:doctxt.xml
-
-	    if (!open(OUTDOC_TXT, ">$doc_txt_file")){
-		print $outhandle "docsave::process could not write to file $doc_mets_file\n";
-		return;
-	    }
-
-	    $self->output_txt_xml_header('docsave::OUTDOC_TXT');
-	    $doc_obj->output_txt_section('docsave::OUTDOC_TXT', $doc_obj->get_top_section());
-	    #$self->output_txt_xml_footer('docsave::OUTDOC_TXT');
-                
-	    # Convert doctxt.xml file to docmets.xml
-	    if (!open(OUTDOC_METS,">$doc_mets_file")){
-		print $outhandle "docsave::process could not write to file $doc_mets_file\n";
-		return;
-	    }
-	    
-	    $self->output_mets_xml_header('docsave::OUTDOC_METS', $OID);
-	    $doc_obj->output_mets_section('docsave::OUTDOC_METS',$doc_obj->get_top_section(), $working_dir);
-	    $self->output_mets_xml_footer('docsave::OUTDOC_METS');
-
-	    close OUTDOC_TXT;
-	    close OUTDOC_METS;
-	}
-	else {
-	    print $outhandle "docsave::process unrecognised saveas type, $save_as\n";
-	    return;
-	}
-
-	if ($self->{'gzip'}) {
-	    my $doc_file = $self->{'gs_filename'};
-	    `gzip $doc_file`;
-	    $doc_file .= ".gz";
-	    $short_doc_file .= ".gz";
-	    if (!-e $doc_file) {
-		print $outhandle "error while gzipping: $doc_file doesn't exist\n";
-		return 0;
-	    }
-	}
-
-	# do the sortmeta thing
-	my ($metadata);	if (defined ($self->{'sortmeta'})) {
-	    $metadata = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), $self->{'sortmeta'});
-	}
-
-	# store reference in the archive_info
-	if ($self->{'saveas'} eq "METS"){
-	    $self->{'archive_info'}->add_info($OID, $short_mets_doc_file, $metadata);
-	} else {
-	    $self->{'archive_info'}->add_info($OID, $short_doc_file, $metadata);
-	}
-    }
+	$self->output_mets_xml_header('docsave::OUTDOC_METS', $OID);
+	$doc_obj->output_mets_section('docsave::OUTDOC_METS',
+				      $doc_obj->get_top_section(),
+				      $working_dir);
+	$self->output_mets_xml_footer('docsave::OUTDOC_METS');
+
+	close OUTDOC_TXT;
+	close OUTDOC_METS;
+    } else { # save_as isn't GA or METS
+	print $outhandle "docsave::process unrecognised saveas type, $save_as\n";
+	return;
+    }
+
+    if ($self->{'gzip'}) {
+	my $doc_file = $self->{'gs_filename'};
+	`gzip $doc_file`;
+	$doc_file .= ".gz";
+	$short_doc_file .= ".gz";
+	if (!-e $doc_file) {
+	    print $outhandle "error while gzipping: $doc_file doesn't exist\n";
+	    return 0;
+	}
+    }
+
+    # do the sortmeta thing
+    my ($metadata);	if (defined ($self->{'sortmeta'})) {
+	$metadata = $doc_obj->get_metadata_element($doc_obj->get_top_section(),
+						   $self->{'sortmeta'});
+    }
+
+    # store reference in the archive_info
+    $self->{'archive_info'}->add_info($OID, $short_doc_file, $metadata);
 }
 
@@ -289,5 +295,5 @@
 
     my @assoc_files = ();
-    foreach $assoc_file (@{$doc_obj->get_assoc_files()}) {
+    foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
 	my ($dir, $afile) = $assoc_file->[1] =~ /^(.*?)([^\/\\]+)$/;
 	$dir = "" unless defined $dir;
@@ -321,5 +327,11 @@
 
     my $OID = $self->{'gs_OID'};
-    my $short_doc_file = $self->{'gs_short_filename'};
+    my $short_doc_file;
+    # can we use 'short_doc_file' for GA too?
+    if (exists($self->{'saveas'}) && $self->{'saveas'} eq "METS") {
+	$short_doc_file=$self->{'short_doc_file'};
+    } else { # "GA"
+	$short_doc_file=$self->{'gs_short_filename'};
+    }
 
     if ($self->{'gzip'}) {
@@ -336,9 +348,5 @@
 
     # store reference in the archive_info
-    if ($self->{'saveas'} eq "METS"){
-	$self->{'archive_info'}->add_info($OID, $short_mets_doc_file);
-    } else {
-	$self->{'archive_info'}->add_info($OID, $short_doc_file);
-    }
+    $self->{'archive_info'}->add_info($OID, $short_doc_file);
    
     return 1;
