Index: /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm
===================================================================
--- /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 37206)
+++ /main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm	(revision 37207)
@@ -29,4 +29,5 @@
 
 use cgiactions::baseaction;
+use cgiactions::metadataactioncommon;
 
 use dbutil;
@@ -34,4 +35,7 @@
 
 use JSON;
+
+# convenient to have shorter version around
+my $FLDV_HISTORY_DIR = $metadataactioncommon::FLDV_HISTORY_DIR;
 
 # This class is conditionally expanded with set-metadata, remove-metadata and insert-metadata subroutines
@@ -69,8 +73,9 @@
     # unused and untested
     # when DocEdit=1, need to retrieve a doc's full text (or doc section's full text) from archives
-    "get-archives-text" => {
-	'compulsory-args' => [ "d" ] },
-	#'compulsory-args' => [ "d" ],
-	#'optional-args' => [ "section" ] },
+        "get-archives-text" => {
+	        'compulsory-args' => [ "d" ],
+	        'optional-args'   => [ "dv" ] },
+	        #'compulsory-args' => [ "d" ],
+	        #'optional-args' => [ "section" ] },
     
 	#GET METHODS
@@ -81,5 +86,5 @@
 	"get-archives-metadata" => { 
 		'compulsory-args' => [ "d", "metaname" ],
-		'optional-args'   => [ "metapos" ] },
+		'optional-args'   => [ "dv", "metapos" ] },
 	
 	"get-index-metadata" => { 
@@ -152,13 +157,13 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
+    #my $gsdlhome  = $self->{'gsdlhome'};
     my $infodbtype = $self->{'infodbtype'};
     
     # live metadata gets/saves value scoped (prefixed) by the current usename 
     # so (for now) let's not bother to enforce authentication
-
+    
     # Obtain the collect dir
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 
@@ -398,6 +403,16 @@
 	    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
 	    # This now stores the full pathname
-	    my $doc_filename = $doc_rec->{'doc-file'}->[0];
-	    $doc_filename = &util::filename_cat($archive_dir, $doc_filename);
+	    my $doc_file = $doc_rec->{'doc-file'}->[0];
+
+	    # check if request if for file-level doc-version history 'nminus-<n>' version
+	    my $dv = $self->{'dv'};	
+	    if (defined $dv && ($dv ne "")) {
+		# Need to insert '_fldv_history/nminus-<n>' into doc_filename
+		
+		my ($doc_tailname, $doc_dirname) = File::Basename::fileparse($doc_file);
+		$doc_file = &util::filename_cat($doc_dirname,$FLDV_HISTORY_DIR,$dv,$doc_tailname);
+	    }
+	    
+	    my $doc_filename = &util::filename_cat($archive_dir, $doc_file);
 
 	    if($first_rec) {
@@ -688,5 +703,5 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
+    #my $gsdlhome  = $self->{'gsdlhome'};
 
     # Obtain the collect dir
@@ -736,5 +751,5 @@
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-	my $gsdlhome  = $self->{'gsdlhome'};
+	#my $gsdlhome  = $self->{'gsdlhome'};
 
 	# Obtain the collect dir
@@ -873,5 +888,4 @@
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
 
@@ -894,7 +908,18 @@
 
 	# This now stores the full pathname
-	my $doc_filename = $doc_rec->{'doc-file'}->[0];	
-
-	$gsdl_cgi->generate_ok_message($self->get_metadata_from_archive_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $docid_secnum));
+	my $doc_file = $doc_rec->{'doc-file'}->[0];	
+
+	# check if request if for file-level doc-version history 'nminus-<n>' version
+	my $dv = $self->{'dv'};	
+	if (defined $dv && ($dv ne "")) {
+	    # Need to insert '_fldv_history/nminus-<n>' into doc_filename
+	    
+	    my ($doc_tailname, $doc_dirname) = File::Basename::fileparse($doc_file);
+	    $doc_file = &util::filename_cat($doc_dirname,$FLDV_HISTORY_DIR,$dv,$doc_tailname);
+	}
+
+	my $doc_filename = &util::filename_cat($archive_dir, $doc_file);
+	    
+	$gsdl_cgi->generate_ok_message($self->get_metadata_from_archive_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $docid_secnum));
 
 }
@@ -908,5 +933,4 @@
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
 
@@ -925,10 +949,21 @@
 
 	# This now stores the full pathname
-	my $doc_filename = $doc_rec->{'doc-file'}->[0];
-	
+	my $doc_file = $doc_rec->{'doc-file'}->[0];
+
+	# check if request if for file-level doc-version history 'nminus-<n>' version
+	my $dv = $self->{'dv'};	
+	if (defined $dv && ($dv ne "")) {
+	    # Need to insert '_fldv_history/nminus-<n>' into doc_filename
+	    
+	    my ($doc_tailname, $doc_dirname) = File::Basename::fileparse($doc_file);
+	    $doc_file = &util::filename_cat($doc_dirname,$FLDV_HISTORY_DIR,$dv,$doc_tailname);
+	}
+	
+	my $doc_filename = &util::filename_cat($archive_dir, $doc_file);
+	    
 	my $metaname = undef;
 	my $metapos = -1;
 	
-	$gsdl_cgi->generate_ok_message($self->get_metadata_from_archive_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $docid_secnum));
+	$gsdl_cgi->generate_ok_message($self->get_metadata_from_archive_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $docid_secnum));
 
 }
Index: /main/trunk/greenstone2/perllib/cgiactions/metadataactioncommon.pm
===================================================================
--- /main/trunk/greenstone2/perllib/cgiactions/metadataactioncommon.pm	(revision 37207)
+++ /main/trunk/greenstone2/perllib/cgiactions/metadataactioncommon.pm	(revision 37207)
@@ -0,0 +1,32 @@
+##########################################################################
+#
+# metadatashared.pm -- 
+# A component of the Greenstone digital library software
+# from the New Zealand Digital Library Project at the 
+# University of Waikato, New Zealand.
+#
+# Copyright (C) 2009 New Zealand Digital Library Project
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+###########################################################################
+
+package metadataactioncommon;
+
+use strict;
+
+our $FLDV_HISTORY_DIR = "_fldv_history";
+
+1;
Index: /main/trunk/greenstone2/perllib/cgiactions/modmetadataaction.pm
===================================================================
--- /main/trunk/greenstone2/perllib/cgiactions/modmetadataaction.pm	(revision 37206)
+++ /main/trunk/greenstone2/perllib/cgiactions/modmetadataaction.pm	(revision 37207)
@@ -37,4 +37,5 @@
 
 use cgiactions::baseaction;
+use cgiactions::metadataactioncommon;
 
 use dbutil;
@@ -43,4 +44,6 @@
 use JSON;
 
+# convenient to have shorter version around
+my $FLDV_HISTORY_DIR = $metadataactioncommon::FLDV_HISTORY_DIR;
 
 $metadataaction::modmeta_action_table = #OR: our $modmeta_action_table =
@@ -53,5 +56,5 @@
 	"set-metadata" => { # generic set-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
 		'compulsory-args' => [ "metaname", "metavalue" ],
-		'optional-args'   => [ "where", "metapos", "metamode", "prevmetavalue", "d", "f" ] }, 
+		'optional-args'   => [ "where", "metapos", "metamode", "prevmetavalue", "d", "dv", "f" ] }, 
 
 	"set-index-metadata" => { 
@@ -61,6 +64,10 @@
 	"set-archives-metadata" => { 
 		'compulsory-args' => [ "d", "metaname", "metavalue" ],
-		'optional-args'   => [ "metapos", "metamode", "prevmetavalue" ] }, # metamode can be "accumulate", "override",
-	
+		'optional-args'   => [ "dv", "metapos", "metamode", "prevmetavalue" ] }, # metamode can be "accumulate", "override",
+
+        ###### !!!!!
+	# Should there not be a "set-archives-text" also ???!!!!??
+	###### !!!!!
+	    
 	"set-import-metadata" => { 
 		'compulsory-args' => [ "metaname", "metavalue" ],
@@ -106,5 +113,5 @@
 	"remove-archives-metadata" => { 
 		'compulsory-args' => [ "d", "metaname" ], #TODO: add f argument
-		'optional-args'   => [ "metapos", "metavalue", "metamode" ] },
+		'optional-args'   => [ "dv", "metapos", "metavalue", "metamode" ] },
 
 	"remove-live-metadata" => {
@@ -118,5 +125,5 @@
 	"remove-metadata" => { # generic remove-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
 		'compulsory-args' => [ "d", "metaname" ],
-		'optional-args'   => [ "where", "metapos", "metavalue", "metamode" ] }, # metamode is optional since remove-metadata can call any of remove_import_meta and remove_archives_meta, remove_index_meta, of which the first two accept metamode as an optional param
+		'optional-args'   => [ "where", "dv", "metapos", "metavalue", "metamode" ] }, # metamode is optional since remove-metadata can call any of remove_import_meta and remove_archives_meta, remove_index_meta, of which the first two accept metamode as an optional param
 
 
@@ -126,5 +133,6 @@
 
 	"erase-archives-metadata" => { 
-	    'compulsory-args' => [ "d", "metaname" ] },
+	    'compulsory-args' => [ "d", "metaname" ],
+	    'optional-args'   => [ "dv" ] },
 
 	"erase-index-metadata" => { 
@@ -145,5 +153,4 @@
 };
 
-
 sub _set_live_metadata
 {
@@ -152,5 +159,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    #my $gsdlhome  = $self->{'gsdlhome'};
     my $infodbtype = $self->{'infodbtype'};
 
@@ -158,5 +164,4 @@
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-    ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 
 
@@ -291,10 +296,8 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
     my $infodbtype = $self->{'infodbtype'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
      
     # Obtain the collect and archive dir    
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
     my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
    
@@ -365,4 +368,6 @@
     # look up additional args
     my $docid  = $self->{'d'};
+    my $dv     = $self->{'dv'};
+    
     my $metaname   = $self->{'metaname'};
     my $metavalue  = $self->{'metavalue'};
@@ -382,6 +387,6 @@
     }
 
-    my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
-				$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
+    my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,
+						    $docid,$dv, $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
    
     if ($status == 0) {
@@ -420,4 +425,5 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
 
+    # Obtain the collect dir
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
@@ -468,5 +474,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    #my $gsdlhome  = $self->{'gsdlhome'};
 
     if ($baseaction::authentication_enabled) {
@@ -475,11 +480,9 @@
     }
 
+    # Obtain the collect dir
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 	
     $gsdl_cgi->checked_chdir($collect_dir);
-
-    # Obtain the collect dir
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 
     # Make sure the collection isn't locked by someone else
@@ -621,7 +624,8 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
+
+    # Obtain the collect dir
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-
     
     # look up additional args
@@ -699,5 +703,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
 
     if ($baseaction::authentication_enabled) {
@@ -706,11 +709,10 @@
     }
 
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+    # Obtain the collect dir
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 	
     $gsdl_cgi->checked_chdir($collect_dir);
 
-    # Obtain the collect dir
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 
     # Make sure the collection isn't locked by someone else
@@ -731,7 +733,7 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
 
+    # Obtain the collect dir
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-
     
     # look up additional args
@@ -819,6 +821,7 @@
     }
 
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+    # Obtain the collect dir
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 	
     $gsdl_cgi->checked_chdir($collect_dir);
@@ -1110,5 +1113,5 @@
 {
     my $self = shift @_;
-    my ($gsdl_cgi, $archive_dir, $collect_dir, $collect, $infodbtype, $docid, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue) = @_;
+    my ($gsdl_cgi, $archive_dir, $collect_dir, $collect, $infodbtype, $docid, $dv, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue) = @_;
 
     my $info_mess = <<RAWEND;
@@ -1140,6 +1143,15 @@
     my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
     
+    # check if request if for file-level doc-version history 'nminus-<n>' version
+    if (defined $dv && ($dv ne "")) {
+	# Need to insert '_fldv_history/nminus-<n>' into doc_filename
+	
+	my ($doc_xml_tailname, $doc_xml_dirname) = File::Basename::fileparse($doc_xml_file);
+	$doc_xml_file = &util::filename_cat($doc_xml_dirname,$FLDV_HISTORY_DIR,$dv,$doc_xml_tailname);
+    }
+	
     # The $doc_xml_file is relative to the archives, and now let's get the full path
-    my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");    
+    my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");
+
     my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file);
 
@@ -1162,6 +1174,6 @@
 			$metaname,$metavalue,$metapos,$metamode,$docid_secnum,$prevmetavalue);
 
-	# return 0; # return 0 for now to indicate no error
-	return (defined $self->{'error_msg'}) ? 1 : 0;
+    # return 0; # return 0 for now to indicate no error
+    return (defined $self->{'error_msg'}) ? 1 : 0;
 }
 
@@ -1195,4 +1207,6 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
+
+    # Obtain the collect dir
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
@@ -1216,5 +1230,6 @@
 	my $status    = -1;
 	my $docid     = $doc_array_rec->{'docid'};
-
+	my $dv        = $doc_array_rec->{'dv'};
+	
 	push(@all_docids,$docid);
 	
@@ -1235,6 +1250,6 @@
 	    }		
 	    
-	    $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
-				$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
+	    $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,
+							 $docid,$dv, $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
 	} elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
 	    my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
@@ -1249,5 +1264,5 @@
 		foreach my $metavalue ( @$metavals ) {
 		    $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect,$infodbtype,
-								 $docid,$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
+								 $docid,$dv, $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
 		    
 		    if($metamode eq "override") { # now, having overridden the metavalue for the first, 
@@ -1287,5 +1302,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
 
     if ($baseaction::authentication_enabled) {
@@ -1294,11 +1308,9 @@
     }
 
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
+    # Obtain the collect dir
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 	
     $gsdl_cgi->checked_chdir($collect_dir);
-
-    # Obtain the collect dir
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 
     # Make sure the collection isn't locked by someone else
@@ -1317,11 +1329,10 @@
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
 	
-	my $site = $self->{'site'};
-		
+
 	# Obtain the collect and archive dir   
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);	
+	my $site = $self->{'site'};		
+	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 	
 	my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
@@ -1354,8 +1365,19 @@
 
 	# This now stores the full pathname
-	my $doc_filename = $doc_rec->{'doc-file'}->[0];	
-
-	my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $metavalue, $docid_secnum, $metamode);
-#	my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, undef, $docid_secnum);
+	my $doc_file = $doc_rec->{'doc-file'}->[0];	
+
+	# check if request if for file-level doc-version history 'nminus-<n>' version
+	my $dv = $self->{'dv'};	
+	if (defined $dv && ($dv ne "")) {
+	    # Need to insert '_fldv_history/nminus-<n>' into doc_filename
+	    
+	    my ($doc_tailname, $doc_dirname) = File::Basename::fileparse($doc_file);
+	    $doc_file = &util::filename_cat($doc_dirname,$FLDV_HISTORY_DIR,$dv,$doc_tailname);
+	}
+
+	my $doc_filename = &util::filename_cat($archive_dir, $doc_file);
+	    
+	my $status = $self->remove_from_doc_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $metavalue, $docid_secnum, $metamode);
+#	my $status = $self->remove_from_doc_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, undef, $docid_secnum);
 	
 	if ($status == 0) 
@@ -1835,5 +1857,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
 
     if ($baseaction::authentication_enabled) {
@@ -1842,4 +1863,5 @@
     }
 
+    # Obtain the collect dir
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
@@ -1865,4 +1887,5 @@
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
 
+    # Obtain the collect dir
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
@@ -2044,13 +2067,10 @@
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
-	
-	# Obtain the collect dir
-	## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
+
+	# Obtain the collect and archive dir
 	my $site = $self->{'site'};
 	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 	
-	## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 	my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
 
@@ -2243,13 +2263,10 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
     my $infodbtype = $self->{'infodbtype'};
 
     # Obtain the collect dir
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 
-    
     # look up additional args
     my $docid     = $self->{'d'};
@@ -2296,5 +2313,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
 	
     if ($baseaction::authentication_enabled) {
@@ -2356,5 +2372,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
     my $infodbtype = $self->{'infodbtype'};
 	
@@ -2362,5 +2377,4 @@
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 
         # look up additional args
@@ -2449,5 +2463,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
 	
     if ($baseaction::authentication_enabled) {
@@ -2457,7 +2470,6 @@
 
     # Obtain the collect dir
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 
     # Make sure the collection isn't locked by someone else
@@ -2520,5 +2532,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
     my $infodbtype = $self->{'infodbtype'};
 	
@@ -2526,7 +2537,6 @@
     my $site = $self->{'site'};
     my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
-
-        # look up additional args
+
+    # look up additional args
     my $docid     = $self->{'d'};
     if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
@@ -2588,5 +2598,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#    my $gsdlhome  = $self->{'gsdlhome'};
 	
     if ($baseaction::authentication_enabled) {
@@ -2596,7 +2605,6 @@
 
     # Obtain the collect dir
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 
     # Make sure the collection isn't locked by someone else
@@ -2617,10 +2625,8 @@
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
-	
+			
+	# Obtain the collect and archive dir   
 	my $site = $self->{'site'};
-		
-	# Obtain the collect and archive dir   
 	my $collect_dir = $gsdl_cgi->get_collection_dir($site);	
 	
@@ -2641,7 +2647,18 @@
 
 	# This now stores the full pathname
-	my $doc_filename = $doc_rec->{'doc-file'}->[0];	
-
-	my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $metavalue, $docid_secnum, $metamode);
+	my $doc_file = $doc_rec->{'doc-file'}->[0];	
+
+	# check if request if for file-level doc-version history 'nminus-<n>' version
+	my $dv = $self->{'dv'};	
+	if (defined $dv && ($dv ne "")) {
+	    # Need to insert '_fldv_history/nminus-<n>' into doc_filename
+	    
+	    my ($doc_tailname, $doc_dirname) = File::Basename::fileparse($doc_file);
+	    $doc_file = &util::filename_cat($doc_dirname,$FLDV_HISTORY_DIR,$dv,$doc_tailname);
+	}
+
+	my $doc_filename = &util::filename_cat($archive_dir, $doc_file);
+	
+	my $status = $self->remove_from_doc_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $metavalue, $docid_secnum, $metamode);
 	
 	if ($status == 0) 
@@ -2694,13 +2711,10 @@
 	my $collect   = $self->{'collect'};
 	my $gsdl_cgi  = $self->{'gsdl_cgi'};
-#	my $gsdlhome  = $self->{'gsdlhome'};
 	my $infodbtype = $self->{'infodbtype'};
 	
 	# Obtain the collect dir
-	## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 	my $site = $self->{'site'};
 	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
 	
-	## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
 	my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
 
@@ -2792,5 +2806,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
 	
     if ($baseaction::authentication_enabled) {
@@ -2818,5 +2831,4 @@
     my $collect   = $self->{'collect'};
     my $gsdl_cgi  = $self->{'gsdl_cgi'};
-    my $gsdlhome  = $self->{'gsdlhome'};
     my $infodbtype = $self->{'infodbtype'};
 	
@@ -2832,7 +2844,6 @@
 
     # Obtain the collect and archive dir   
-	my $site = $self->{'site'};
-	my $collect_dir = $gsdl_cgi->get_collection_dir($site);
-    ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
+    my $site = $self->{'site'};
+    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
 
