Index: /gsdl/trunk/perllib/inexport.pm
===================================================================
--- /gsdl/trunk/perllib/inexport.pm	(revision 19497)
+++ /gsdl/trunk/perllib/inexport.pm	(revision 19498)
@@ -125,5 +125,5 @@
 	}
 
-	# figure of if new file or not
+	# figure out if new file or not
 	if (defined $full_prev_all_files->{$full_curr_file}) {
 	    
@@ -152,5 +152,5 @@
     # In building the final list of files to delete, we test to see if
     # it exists on the filesystem and if it does (unusual for a file
-    # that's allegedly deleted!) , supress it from going into the final
+    # that's allegedly deleted!), supress it from going into the final
     # list
 
@@ -175,7 +175,19 @@
 
 
+sub is_assoc_file
+{
+    my ($file,$doc_rec) = @_;
+
+    foreach my $af (@{$doc_rec->{'assoc-file'}}) {
+	return 1 if ($af eq $file);
+    }
+
+    return 0;
+}
+
+
 sub _mark_docs_for_deletion
 {
-    my ($archive_info,$deleted_files_ref,$archivedir,$verbosity,$mode_text) = @_;
+    my ($archive_info,$block_hash,$deleted_files,$archivedir,$verbosity,$mode_text) = @_;
 
     my $doc_db = "archiveinf-doc.gdb";
@@ -186,5 +198,5 @@
 
     # record files marked for deletion in arcinfo
-    foreach my $file (@$deleted_files_ref) {
+    foreach my $file (@$deleted_files) {
 	# use 'archiveinf-src' GDBM file to look up all the OIDs
 	# this file is used in (note in most cases, it's just one OID)
@@ -194,24 +206,9 @@
 	foreach my $oid (@$oids) {
 
-	    # Find out if it's an assoc file or main doc
+	    # Find out if it's a main doc, assoc file, or metadata
 
 	    my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$oid);
-	    if ($doc_rec->{'src-file'}->[0] eq $file) {
-		# It's the main doc
-		# => mark it for deletion
-	
-		if ($verbosity>1) {
-		    print STDERR "$oid ($file) marked to be $mode_text on next buildcol.pl\n";
-		}
-		$archive_info->set_status_info($oid,"D");
-
-		my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
-		my ($index_status) = ($val =~ m/^<index-status>(.*)$/m);
-		if ($index_status ne "D") {
-		    $val =~ s/^<index-status>(.*)$/<index-status>D/m;
-		    &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
-		}
-	    }
-	    else {
+
+	    if (is_assoc_file($file,$doc_rec)) {
 		# assoc file => mark it for re-indexing (safest thing to do)
 		my $curr_status = $archive_info->get_status_info($oid);
@@ -229,7 +226,47 @@
 		    &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
 		}
+		GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
 	    }
-
-	    GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
+	    else {
+		# either src-file or metadata.xml file linking to src-file
+
+		my $src_file;
+
+		if ($doc_rec->{'src-file'}->[0] ne $file) {
+		    # it's a metadata file attached to this OID
+		    # => workout the src-file it matches to
+
+		    $src_file = $doc_rec->{'src-file'}->[0];
+		    
+		    my $src_filename = $src_file;
+		    if (!&util::filename_is_absolute($src_file)) {
+			$src_filename = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$src_file);
+		    }
+		    
+		    $block_hash->{'reindex_files'}->{$src_filename} = 1;
+		}
+		else {
+		    # It's the main doc
+		    # => make it the target and mark it for deletion
+		    $src_file = $file;
+		}
+
+		# Whether the main file directly or indirectly, mark for deletion/reindex
+
+		if ($verbosity>1) {
+		    print STDERR "$oid ($src_file) marked to be $mode_text on next buildcol.pl\n";
+		}
+		$archive_info->set_status_info($oid,"D");
+
+		my $val = &GDBMUtils::gdbmDatabaseGet($arcinfo_doc_filename,$oid);
+		my ($index_status) = ($val =~ m/^<index-status>(.*)$/m);
+		if ($index_status ne "D") {
+		    $val =~ s/^<index-status>(.*)$/<index-status>D/m;
+		    &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
+		}
+
+		GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$src_file);
+	    }
+
 	}
     }
@@ -245,5 +282,5 @@
 sub mark_docs_for_reindex
 {
-    my ($archive_info,$existing_files_ref,$archivedir,$verbosity) = @_;
+    my ($archive_info,$block_hash,$archivedir,$verbosity) = @_;
 
     # Reindexing is accomplished by deleting the previously indexed
@@ -260,13 +297,14 @@
     # deals with it appropriately.
 
+    my @existing_files = sort keys %{$block_hash->{'existing_files'}};
+
     my $doc_db = "archiveinf-doc.gdb";
     my $arcinfo_doc_filename = &util::filename_cat ($archivedir, $doc_db);
 
-
     my $archiveinf_timestamp = -M $arcinfo_doc_filename;
 
-    my $reindex_files_ref = [];
-
-    foreach my $existing_filename (@$existing_files_ref) {
+    my $reindex_files = [];
+
+    foreach my $existing_filename (@existing_files) {
 	
 	if (-M $existing_filename < $archiveinf_timestamp) {
@@ -281,12 +319,12 @@
 ###	    print STDERR "**** Deleting existing file: $existing_file\n";
 
-	    push(@$reindex_files_ref,$existing_file);
+	    push(@$reindex_files,$existing_file);
+	    $block_hash->{'reindex_files'}->{$existing_filename} = 1;
 	}
 
     }
     
-    _mark_docs_for_deletion($archive_info,$reindex_files_ref,$archivedir,$verbosity, "reindex");
-
-    return @$reindex_files_ref;
+    _mark_docs_for_deletion($archive_info,$block_hash,$reindex_files,$archivedir,$verbosity, "reindex");
+
 }
 
