Index: /gsdl/trunk/perllib/inexport.pm
===================================================================
--- /gsdl/trunk/perllib/inexport.pm	(revision 20775)
+++ /gsdl/trunk/perllib/inexport.pm	(revision 20776)
@@ -80,4 +80,10 @@
 {
     my ($archive_info,$block_hash,$importdir,$archivedir,$verbosity,$incremental_mode) = @_;
+
+    # in this method, we want to know if metadata files are modified or not.
+    my $doc_db = "archiveinf-doc.gdb";
+    my $arcinfo_doc_filename = &util::filename_cat ($archivedir, $doc_db);
+
+    my $archiveinf_timestamp = -M $arcinfo_doc_filename;
 
     # First convert all files to absolute form
@@ -108,5 +114,5 @@
 
 	# entry in 'all_files' is moved to either 'existing_files', 
-	# 'deleted_files', 'new_files', or 'new_metadata_files'
+	# 'deleted_files', 'new_files', or 'new_or_modified_metadata_files'
 
 	if (!&util::filename_is_absolute($curr_file)) {
@@ -115,36 +121,34 @@
 	}
 
-	if (defined $block_hash->{'file_blocks'}->{$full_curr_file}) {
-	    # If in block list, we want to ignore it
-	    delete $block_hash->{'all_files'}->{$curr_file};
-
-	    if (defined $full_prev_all_files->{$full_curr_file}) {
-		# also make sure it is gone from 'previous' list so
-		# not mistaken for a file that needs to be deleted
-		delete $full_prev_all_files->{$full_curr_file};
-	    }
-	    next;
-	}
-
 	# figure out if new file or not
 	if (defined $full_prev_all_files->{$full_curr_file}) {
+	    # delete it so that only files that need deleting are left
+	    delete $full_prev_all_files->{$full_curr_file};
+	    
+	    # had it before. is it a metadata file?
+	    if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
 		
-	    if ($incremental_mode eq "all") {
-
-		# had it before
-		$block_hash->{'existing_files'}->{$full_curr_file} = 1;
-		
-		# Now remove it, so by end of loop only the files
-		# that need deleting are left
-	    
-		delete $full_prev_all_files->{$full_curr_file};
+		# is it modified??
+		if (-M $full_curr_file < $archiveinf_timestamp) {
+		    print STDERR "*** Detected a modified metadata file: $full_curr_file\n" if $verbosity > 2;
+		    # its newer than last build
+		    $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
+		}
 	    }
 	    else {
-		# Warning in "onlyadd" mode, but had it before!
-		print STDERR "Warning: File $full_curr_file previously imported.\n";
-		print STDERR "         Treating as new file\n";
-
-		$block_hash->{'new_files'}->{$full_curr_file} = 1;
-		delete $full_prev_all_files->{$full_curr_file};
+		if ($incremental_mode eq "all") {
+		    
+		    # had it before
+		    $block_hash->{'existing_files'}->{$full_curr_file} = 1;
+		    
+		}
+		else {
+		    # Warning in "onlyadd" mode, but had it before!
+		    print STDERR "Warning: File $full_curr_file previously imported.\n";
+		    print STDERR "         Treating as new file\n";
+		    
+		    $block_hash->{'new_files'}->{$full_curr_file} = 1;
+		    
+		}
 	    }
 	}
@@ -157,6 +161,6 @@
 		# Greenstone to always be this)
 
-##		print STDERR "***** Detected new metadata file: $full_curr_file\n";
-		$block_hash->{'new_metadata_files'}->{$full_curr_file} = 1;
+		print STDERR "***** Detected new metadata file: $full_curr_file\n" if $verbosity > 2;
+		$block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
 	    }
 	    else {
@@ -170,5 +174,5 @@
 
 
-    # Deal with complication of new metadata.xml files by forcing
+    # Deal with complication of new or modified metadata files by forcing
     # everything from this point down in the file hierarchy to
     # be freshly imported.  
@@ -178,5 +182,5 @@
     # associated with the relevant document(s).
 
-    foreach my $new_mdf (keys %{$block_hash->{'new_metadata_files'}}) {
+    foreach my $new_mdf (keys %{$block_hash->{'new_or_modified_metadata_files'}}) {
 	my ($fileroot,$situated_dir,$ext) = fileparse($new_mdf, "\\.[^\\.]+\$");
 
@@ -199,5 +203,5 @@
 	}
 
-	# Reindexing is accomplished by putting them in th list for reindexing (line above)
+	# Reindexing is accomplished by putting them in the list for reindexing (line above)
 	# and then tagging the arcinfo version as to be deleted.
 
@@ -279,14 +283,22 @@
 	my $src_rec = GDBMUtils::gdbmRecordToHash($arcinfo_src_filename,$file);
 	my $oids = $src_rec->{'oid'};
+	my $file_record_deleted = 0;
 	foreach my $oid (@$oids) {
 	    # Find out if it's a main doc, assoc file, or metadata
 
 	    my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$oid);
+	    my $doc_source_file = $doc_rec->{'src-file'}->[0];
+	    if (!&util::filename_is_absolute($doc_source_file)) {
+		$doc_source_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$doc_source_file);
+	    }
 
 	    if (is_assoc_file($file,$doc_rec)) {
+		## -- kjdon - here, do same thing as for metadata file??
+		## mark source for reimport??
 		# assoc file => mark it for re-indexing (safest thing to do)
 		my $curr_status = $archive_info->get_status_info($oid);
 
-
+		# mark source doc for reimport as one of its assoc files has changed or deleted
+		$block_hash->{'reindex_files'}->{$doc_source_file} = 1;
 		if (defined($curr_status) && (($curr_status ne "D") && ($curr_status ne "R"))) {
 		    if ($verbosity > 1) {
@@ -300,31 +312,22 @@
 		    &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
 		}
-		GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
+		GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file) unless $file_record_deleted;
+		$file_record_deleted = 1;
 	    }
 	    else {
 		# either src-file or metadata.xml file linking to src-file
-
-		my $src_file;
+		# actually, metadata files should not get here, as are 
+		# processed earlier
 
 		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];
+		    # => reindex the src-file it matches to
+
+		    $block_hash->{'reindex_files'}->{$doc_source_file} = 1;
+
+		    # remove the metadata file from the src-database
 		    
-		    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;
-
-		    # remove the metadata file from the src-database
-		    GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file);
-		}
-		else {
-		    # It's the main doc
-		    # => make it the target and mark it for deletion
-		    $src_file = $file;
+		    GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$file) unless $file_record_deleted;
+		    $file_record_deleted = 1;
 		}
 
@@ -336,5 +339,5 @@
 		if ($index_status ne "D") {
 		    if ($verbosity>1) {
-			print STDERR "$oid ($src_file) marked to be $mode_text on next buildcol.pl\n";
+			print STDERR "$oid ($doc_source_file) marked to be $mode_text on next buildcol.pl\n";
 		    }
 		    $archive_info->set_status_info($oid,"D");
@@ -343,5 +346,5 @@
 		    &GDBMUtils::gdbmDatabaseSet($arcinfo_doc_filename,$oid,$val);
 
-		    GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$src_file);
+		    GDBMUtils::gdbmDatabaseRemove($arcinfo_src_filename,$doc_source_file);
 		}
 
