Index: trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java
===================================================================
--- trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java	(revision 7592)
+++ trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java	(revision 7601)
@@ -1167,7 +1167,10 @@
 	    if(installCollection()) {
 		// If we have a local library running (that we know about) then we ask it to add our newly create collection
-		if(Gatherer.config.exec_file != null) {
+		if (Gatherer.config.exec_file != null) {
 		    Gatherer.self.configServer(GSDLSiteConfig.ADD_COMMAND + collection.getName());
-		} else if (Gatherer.GS3) {
+		    // This is very important -- it ensures that the above command has finished
+		    Gatherer.self.configServer("");
+		}
+		else if (Gatherer.GS3) {
 		    convertToGS3Collection();
 		    Gatherer.self.configGS3Server(Gatherer.config.site_name, ServletConfiguration.ADD_COMMAND + collection.getName());
@@ -1177,10 +1180,10 @@
 		Gatherer.g_man.collectionChanged(ready());
 
-		// Now display a message dialog saying its all built
-		WarningDialog collection_built_warning_dialog = new WarningDialog("warning.CollectionBuilt", false);
-		collection_built_warning_dialog.setMessageOnly(true); // Not a warning
-		collection_built_warning_dialog.display();
-		collection_built_warning_dialog.dispose();
-		collection_built_warning_dialog = null;
+  		// Now display a message dialog saying its all built
+  		WarningDialog collection_built_warning_dialog = new WarningDialog("warning.CollectionBuilt", false);
+  		collection_built_warning_dialog.setMessageOnly(true); // Not a warning
+  		collection_built_warning_dialog.display();
+  		collection_built_warning_dialog.dispose();
+  		collection_built_warning_dialog = null;
 	    }
 	    else {
@@ -1416,20 +1419,43 @@
 
 	try {
-	    // We have to ensure that the local library doesn't have locks on any files
-	    if(!Gatherer.GS3 && Gatherer.config.exec_file != null) {
+	    // We have to ensure that the local library has released this collection so we can delete the index directory
+	    if (!Gatherer.GS3 && Gatherer.config.exec_file != null) {
 		Gatherer.self.configServer(GSDLSiteConfig.RELEASE_COMMAND + collection.getName());
-	    }
-
-	    File build_dir = new File(getCollectionBuild());
-	    Gatherer.println("Build = " + build_dir.getAbsolutePath());
-
-	    // Make sure an index directory exists to move files into
+
+		// This is very important -- it ensures that the above command has finished
+		// This prevents the nasty "could not remove index directory" race condition!
+		Gatherer.self.configServer("");
+	    }
+
 	    File index_dir = new File(getCollectionIndex());
-	    if (!index_dir.exists()) {
-		index_dir.mkdir();
-	    }
 	    Gatherer.println("Index = " + index_dir.getAbsolutePath());
 
-	    moveContentsInto(build_dir, index_dir, true);
+	    File building_dir = new File(getCollectionBuild());
+	    Gatherer.println("Building = " + building_dir.getAbsolutePath());
+
+	    // Get the build mode from the build options
+	    String build_mode = collection.build_options.getBuildValue("mode");
+
+	    // Special case for build mode "all": replace index dir with building dir
+	    if (build_mode == null || build_mode.equals(Dictionary.get("CreatePane.Mode_All"))) {
+		// Remove the old index directory
+		if (index_dir.exists()) {
+		    Utility.delete(index_dir);
+		    // Check the delete worked
+		    if (index_dir.exists()) {
+			throw new Exception("Index directory could not be removed.");
+		    }
+		}
+
+		// Move the building directory to become the new index directory
+		if (building_dir.renameTo(index_dir) == false) {
+		    throw new Exception("Build directory could not be moved.");
+		}
+	    }
+
+	    // Otherwise copy everything in the building dir into the index dir
+	    else {
+		moveContentsInto(building_dir, index_dir);
+	    }
 	}
 	catch (Exception exception) {
@@ -1437,11 +1463,10 @@
 	    return false;
 	}
-
 	return true;
     }
 
 
-    /** Moves all the files in one directory into another, overwriting if desired */
-    private void moveContentsInto(File source_directory, File target_directory, boolean overwrite)
+    /** Moves all the files in one directory into another, overwriting existing files */
+    private void moveContentsInto(File source_directory, File target_directory)
     {
 	File[] source_files = source_directory.listFiles();
@@ -1451,12 +1476,13 @@
 
 	    if (source_file.isDirectory()) {
-		moveContentsInto(source_file, target_file, overwrite);
+		moveContentsInto(source_file, target_file);
 		source_file.delete();
 	    }
 	    else {
-		if (target_file.exists() && overwrite) {
+		if (target_file.exists()) {
 		    target_file.delete();
-		    source_file.renameTo(target_file);
 		}
+
+		source_file.renameTo(target_file);
 	    }
 	}
