Index: main/trunk/greenstone2/bin/script/activate.pl
===================================================================
--- main/trunk/greenstone2/bin/script/activate.pl	(revision 30513)
+++ main/trunk/greenstone2/bin/script/activate.pl	(revision 30517)
@@ -465,165 +465,8 @@
 }
 
-
-# Most of the arguments are familiar from the building scripts like buildcol.pl
-# The special optional argument -library_url is for when we're dealing with a web 
-# library server such as an apache that's separate from any included with GS2. 
-# In such a case, this script's caller should pass in -library_url <URL>.
-#
-# $site argument must be specified in the cmdline for collectionConfig.xml to get 
-# generated which makes $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
-sub main
-{
-    my ($argc,@argv) = @_;
-
-    if (($argc==0)  || (($argc==1) && ($argv[0] =~ m/^--?h(elp)?$/))) {
-	my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
-	
-	
-	print STDERR "\n";
-	print STDERR "Usage: $progname [-collectdir c -builddir b -indexdir i -site s -skipactivation -removeold -keepold -verbosity v\n";
-	print STDERR "\t-library_url URL -library_name n] <[colgroup/]collection>\n";
-	print STDERR "\n";
-	
-	exit(-1);
-    }
-	
-    # get the collection details
-    my $qualified_collection = pop @argv; # qualified collection
-    
-    my $collect_dir = undef; #"collect"; # can't be "collect" when only -site is provided for GS3
-    my $build_dir = undef;
-    my $index_dir = undef;
-    my $site = undef;
-    
-    # if run from server (java code), it will handle deactivation and activation to prevent open file handles when java launches this script and exits:
-    my $skipactivation = 0;
-    my $removeold = 0;
-    my $keepold = 0;
-    my $incremental = 0; # used by solr
-    
-    my $library_url = $ENV{'GSDL_LIBRARY_URL'} || undef; # to be specified on the cmdline if not using a GS-included web server
-    # the GSDL_LIBRARY_URL env var is useful when running cmdline buildcol.pl in the linux package manager versions of GS3
-    
-    my $library_name = undef;
-    
-    while (my $arg = shift @argv) {
-	if ($arg eq "-collectdir") {
-	    $collect_dir = shift @argv;
-	}
-	elsif ($arg eq "-builddir") {
-	    $build_dir = shift @argv;
-	}
-	elsif ($arg eq "-indexdir") {
-	    $index_dir = shift @argv;
-	}
-	elsif ($arg eq "-site") {
-	    $site = shift @argv;
-	}
-	elsif ($arg eq "-skipactivation") {
-	    $skipactivation = 1;
-	}
-	elsif ($arg eq "-removeold") {
-	    $removeold = 1;
-	}
-	elsif ($arg eq "-keepold") {
-	    $keepold = 1;
-	}
-	elsif ($arg eq "-incremental") {
-	    $incremental = 1;
-	}
-	elsif ($arg eq "-library_url") {
-	    $library_url = shift @argv;
-	}
-	elsif ($arg eq "-library_name") {
-	    $library_name = shift @argv;
-	}
-	elsif ($arg eq "-verbosity") { 
-	    $default_verbosity = shift @argv; # global variable
-	    
-	    # ensure we're working with ints not strings (int context not str context), in case verbosity=0
-	    # http://stackoverflow.com/questions/288900/how-can-i-convert-a-string-to-a-number-in-perl
-	    $default_verbosity = int($default_verbosity || 0); ### is this the best way?
-	}
-    }
-    
-    # work out the building and index dirs
-    my $collection_dir = &util::resolve_collection_dir($collect_dir, $qualified_collection, $site);
-    $build_dir = &FileUtils::filenameConcatenate($collection_dir, "building") unless (defined $build_dir);
-    $index_dir = &FileUtils::filenameConcatenate($collection_dir, "index") unless (defined $index_dir);
-    
-    &print_task_msg("Running  Collection  Activation  Stage");
-    
-    # get and check the collection name
-    if ((&colcfg::use_collection($site, $qualified_collection, $collect_dir)) eq "") {
-	&print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
-	exit -1;
-    }
-    
-    # Read in the collection configuration file.
-    # Beware: Only if $site is specified in the cmdline does collectionConfig.xml get 
-    # generated and does $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
-    my $gs_mode = "gs2";
-    if ((defined $site) && ($site ne "")) { # GS3
-	$gs_mode = "gs3";
-    }
-    my $collect_cfg_filename = &colcfg::get_collect_cfg_name(STDERR, $gs_mode);
-    my $collectcfg = &colcfg::read_collection_cfg ($collect_cfg_filename,$gs_mode);
-    
-    # look for build.cfg/buildConfig.xml
-    my $build_cfg_filename ="";	
-    
-    if ($gs_mode eq "gs2") {
-	$build_cfg_filename = &FileUtils::filenameConcatenate($build_dir,"build.cfg");
-    } else {
-	$build_cfg_filename = &FileUtils::filenameConcatenate($build_dir, "buildConfig.xml");
-	# gs_mode is GS3. Set the site now if this was not specified as cmdline argument
-	#$site = "localsite" unless defined $site;
-    }
-    
-    # We need to know the buildtype for Solr.
-    # Any change of indexers is already detected and handled by the calling code (buildcol or 
-    # full-rebuild), so that at this stage the config file's buildtype reflects the actual buildtype.
-    
-    # From buildcol.pl we use searchtype for determining buildtype, but for old versions, use buildtype
-    my $buildtype;
-    if (defined $collectcfg->{'buildtype'}) {
-	$buildtype = $collectcfg->{'buildtype'};
-    } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
-	$buildtype = "mgpp";
-    } else {
-	$buildtype = "mg"; #mg is the default
-    }
-	
-    # can't do anything without a build directory with something in it to move into index
-    # Except if we're (doing incremental) building for solr, where we want to still
-    # activate and deactivate collections including for the incremental case
-    if(!&FileUtils::directoryExists($build_dir)) {
-	&print_msg("No building folder at $build_dir to move to index.\n");
-	exit -1 unless ($buildtype eq "solr"); #&& $incremental);
-    } elsif (&FileUtils::isDirectoryEmpty($build_dir)) {
-	&print_msg("Nothing in building folder $build_dir to move into index folder.\n");
-	exit -1 unless ($buildtype eq "solr"); #&& $incremental);
-    }
-    
-    # Now the logic in GLI's CollectionManager.java	(processComplete() 
-    # and installCollection()) and Gatherer.configGS3Server().
-    
-    # 1. Get library URL
-    
-    # For web servers that are external to a Greenstone installation, 
-    # the user can pass in their web server's library URL.
-    # For web servers included with GS (like tomcat for GS3 and server.exe 
-    # and apache for GS2), we work out the library URL:
-    if(!$library_url) { 
-	$library_url = &get_library_URL($gs_mode, $library_name); # returns undef if no server is running
-    }
-    
-    # CollectionManager's installCollection phase in GLI
-    # 2. Ping the library URL, and if it's a persistent server and running, release the collection
-    
-    my $is_persistent_server = undef;
-    if(!$skipactivation && $library_url) { # undef if no valid server URL
-	
+sub do_deactivate {
+    my($is_persistent_server, $library_url, $gs_mode, $site, $qualified_collection) = @_;
+
+
 	&print_msg("Pinging $library_url\n");		
 	if (&ping_library($library_url, $gs_mode, $site)) { # server running
@@ -654,137 +497,12 @@
 	    &print_msg("No response to Ping => Taken to mean server is not running\n");
 	}
-	
-    }
-    
-    # 2b. If we're working with a solr collection, then start up the solrserver now.
-    my $solr_server;
-    my @corenames = ();
-    if($buildtype eq "solr") { # start up the jetty server	
-	my $solr_ext = $ENV{'GEXT_SOLR'}; # from solr_passes.pl
-	unshift (@INC, "$solr_ext/perllib");
-	require solrserver;
-
-	# Solr cores are named without taking the collection-group name into account, since solr
-	# is used for GS3 and GS3 doesn't use collection groups but has the site concept instead
-	my ($colname, $colgroup) = &util::get_collection_parts($qualified_collection);
-
-	# See solrbuilder.pm to get the indexing levels (document, section) from the collectcfg file
-	# Used to generate core names from them and remove cores by name
-	foreach my $level ( @{$collectcfg->{'levels'}} ){
-	    my ($pindex) = $level =~ /^(.)/;
-	    my $indexname = $pindex."idx";
-	    push(@corenames, "$site-$colname-$indexname"); #"$site-$colname-didx", "$site-$colname-sidx"
-        }
-	
-	# If the Solr/Jetty server is not already running, the following starts
-	# it up, and only returns when the server is "reading and listening"	
-	$solr_server = new solrserver($build_dir);
-	$solr_server->start();
-	
-	# We'll be moving building to index. For solr collection, there's further 
-	# special processing to make a corresponding change to the solr.xml
-	# by removing the temporary building cores and (re)creating the index cores
-    }
-
-
-    # 3. Do all the moving building to index stuff now	
-    
-    # If removeold: replace index dir with building dir.
-    # If keepold: move building's contents into index, where only duplicates will get deleted.
-    # removeold and keepold can't both be on at the same time
-    # incremental becomes relevant for solr, though it was irrelevant to what activate.pl does (moving building to index)
-    my $incremental_mode;
-    ($removeold, $keepold, $incremental, $incremental_mode) = &scriptutil::check_removeold_and_keepold($removeold, $keepold, 
-						   $incremental,
-						   $build_dir, # checkdir. Usually archives or export to be deleted. activate.pl deletes building 
-						   $collectcfg);
-	
-    if($removeold) {
-	
-	if(&FileUtils::directoryExists($index_dir)) {
-	    &print_task_msg("Removing \"index\"");
-	    
-	    if ($buildtype eq "solr") {
-		# if solr, remove any cores that are using the index_dir before deleting this dir
-		foreach my $corename (@corenames) {
-		    $solr_server->admin_unload_core($corename);
-		}
-	    }	
-	    
-	    &FileUtils::removeFilesRecursive($index_dir);
-	    
-	    # Wait for a couple of seconds, just for luck
-	    sleep 2;
-	    
-	    if (&FileUtils::directoryExists($index_dir)) {
-		&print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
-	    }
-	}
-	
-	# if remote GS server: gliserver.pl would call activate.pl to activate 
-	# the collection at this point since activate.pl lives on the server side
-	
-	if ($buildtype eq "solr") {
-	    # if solr, remove any cores that are using the building_dir before moving this dir onto index
-	    foreach my $corename (@corenames) {
-		$solr_server->admin_unload_core("building-$corename");
-	    }
-	}
-	
-	# Move the building directory to become the new index directory
-	&print_task_msg("Moving \"building\" -> \"index\"");
-	&FileUtils::moveFiles($build_dir, $index_dir);
-	if(&FileUtils::directoryExists($build_dir) || !&FileUtils::directoryExists($index_dir)) {			
-	    &print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
-	}
-    }
-    elsif ($keepold || $incremental) {
-	if ($buildtype eq "solr") {
-	    # if solr, remove any cores that may be using the building_dir before moving this dir onto index
-	    foreach my $corename (@corenames) {			
-		$solr_server->admin_unload_core("building-$corename") if $solr_server->admin_ping_core("building-$corename");
-	    }
-	}
-	
-	if($build_dir eq $index_dir) { # building_dir can have been set to "index" folder, see incremental-buildcol.pl
-	    &print_task_msg("building folder is index folder, not moving");
-	} else {
-	    # Copy just the contents of building dir into the index dir, overwriting 
-	    # existing files, but don't replace index with building.
-	    &print_task_msg("Moving \"building\" -> \"index\"");		
-	    &FileUtils::moveDirectoryContents($build_dir, $index_dir);
-	}
-    }
-    
-    if ($buildtype eq "solr") {
-	# Call CREATE action to get the old cores pointing to the index folder
-	foreach my $corename (@corenames) {
-	    if($removeold) {
-		# Call CREATE action to get all cores pointing to the index folder, since building is now index
-		$solr_server->admin_create_core($corename, $index_dir);
-		
-	    } elsif ($keepold || $incremental) { 
-		# Call RELOAD core. Should already be using the index_dir directory for $keepold and $incremental case
-		
-		# Ping to see if corename exists, if it does, reload, else create
-		if ($solr_server->admin_ping_core($corename)) {
-		    $solr_server->admin_reload_core($corename); 
-		} else {
-		    $solr_server->admin_create_core($corename, $index_dir);
-		}
-	    }
-	}
-	
-	# regenerate the solr.xml.in from solr.xml in case we are working off a dvd. 
-	$solr_server->solr_xml_to_solr_xml_in();
-    }
-    
-    # 4. Ping the library URL, and if it's a persistent server and running, activate the collection again	
-    
-    # Check for success: if building does not exist OR is empty OR if building is index (in which case there was no move)	
-    if($build_dir eq $index_dir || !&FileUtils::directoryExists($build_dir) || &FileUtils::isDirectoryEmpty($build_dir)) {
-	
-	if(!$skipactivation && $library_url) { # undef if no valid server URL
-	    
+
+    return $is_persistent_server;
+}
+
+sub do_activate {
+    my($is_persistent_server, $library_url, $gs_mode, $site, $qualified_collection) = @_;
+
+
 	    &print_msg("Pinging $library_url\n");
 	    if (&ping_library($library_url, $gs_mode, $site)) { # server running
@@ -830,4 +548,322 @@
 		&print_msg("No response to Ping => Taken to mean server is not running\n");
 	    }
+
+    return $is_persistent_server;
+}
+
+
+# Most of the arguments are familiar from the building scripts like buildcol.pl
+# The special optional argument -library_url is for when we're dealing with a web 
+# library server such as an apache that's separate from any included with GS2. 
+# In such a case, this script's caller should pass in -library_url <URL>.
+#
+# $site argument must be specified in the cmdline for collectionConfig.xml to get 
+# generated which makes $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
+sub main
+{
+    my ($argc,@argv) = @_;
+
+    if (($argc==0)  || (($argc==1) && ($argv[0] =~ m/^--?h(elp)?$/))) {
+	my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
+	
+	
+	print STDERR "\n";
+	print STDERR "Usage: $progname [-collectdir c -builddir b -indexdir i -site s -skipactivation -removeold -keepold -verbosity v\n";
+	print STDERR "\t-library_url URL -library_name n] <[colgroup/]collection>\n";
+	print STDERR "\n";
+	
+	exit(-1);
+    }
+    
+    # http://stackoverflow.com/questions/6156742/how-can-i-capture-the-complete-commandline-in-perl
+    #print STDERR "@@@@@@@@@ ACTIVATE CMD: " . join " ", $0, @ARGV;
+	
+    # get the collection details
+    my $qualified_collection = pop @argv; # qualified collection
+    
+    my $collect_dir = undef; #"collect"; # can't be "collect" when only -site is provided for GS3
+    my $build_dir = undef;
+    my $index_dir = undef;
+    my $site = undef;
+    
+    # if run from server (java code), it will handle deactivation and activation to prevent open file handles when java launches this script and exits:
+    my $skipactivation = 0;
+    my $removeold = 0;
+    my $keepold = 0;
+    my $incremental = 0; # used by solr
+    
+    # some further parameters
+    my $just_activate = 0; # don't do anything else than activating the collection if the server is running
+    my $just_deactivate = 0; # don't do anything else than deactivating the collection if the server is running
+
+
+    my $library_url = $ENV{'GSDL_LIBRARY_URL'} || undef; # to be specified on the cmdline if not using a GS-included web server
+    # the GSDL_LIBRARY_URL env var is useful when running cmdline buildcol.pl in the linux package manager versions of GS3
+    
+    my $library_name = undef;
+    
+    while (my $arg = shift @argv) {
+	if ($arg eq "-collectdir") {
+	    $collect_dir = shift @argv;
+	}
+	elsif ($arg eq "-builddir") {
+	    $build_dir = shift @argv;
+	}
+	elsif ($arg eq "-indexdir") {
+	    $index_dir = shift @argv;
+	}
+	elsif ($arg eq "-site") {
+	    $site = shift @argv;
+	}
+	elsif ($arg eq "-skipactivation") {
+	    $skipactivation = 1;
+	}
+	elsif ($arg eq "-removeold") {
+	    $removeold = 1;
+	}
+	elsif ($arg eq "-keepold") {
+	    $keepold = 1;
+	}
+	elsif ($arg eq "-incremental") {
+	    $incremental = 1;
+	}	
+	elsif ($arg eq "-justactivate") {
+	    $just_activate = 1;
+	}
+	elsif ($arg eq "-justdeactivate") {
+	    $just_deactivate = 1;
+	}
+	elsif ($arg eq "-library_url") {
+	    $library_url = shift @argv;
+	}
+	elsif ($arg eq "-library_name") {
+	    $library_name = shift @argv;
+	}
+	elsif ($arg eq "-verbosity") { 
+	    $default_verbosity = shift @argv; # global variable
+	    
+	    # ensure we're working with ints not strings (int context not str context), in case verbosity=0
+	    # http://stackoverflow.com/questions/288900/how-can-i-convert-a-string-to-a-number-in-perl
+	    $default_verbosity = int($default_verbosity || 0); ### is this the best way?
+	}
+    }
+    
+    # work out the building and index dirs
+    my $collection_dir = &util::resolve_collection_dir($collect_dir, $qualified_collection, $site);
+    $build_dir = &FileUtils::filenameConcatenate($collection_dir, "building") unless (defined $build_dir);
+    $index_dir = &FileUtils::filenameConcatenate($collection_dir, "index") unless (defined $index_dir);
+    
+    &print_task_msg("Running  Collection  Activation  Stage");
+    
+    # get and check the collection name
+    if ((&colcfg::use_collection($site, $qualified_collection, $collect_dir)) eq "") {
+	&print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
+	exit -1;
+    }
+    
+    # Read in the collection configuration file.
+    # Beware: Only if $site is specified in the cmdline does collectionConfig.xml get 
+    # generated and does $gs_mode=gs3, else collect.cfg gets generated and $gs_mode=gs2
+    my $gs_mode = "gs2";
+    if ((defined $site) && ($site ne "")) { # GS3
+	$gs_mode = "gs3";
+    }
+    my $collect_cfg_filename = &colcfg::get_collect_cfg_name(STDERR, $gs_mode);
+    my $collectcfg = &colcfg::read_collection_cfg ($collect_cfg_filename,$gs_mode);
+    
+    # look for build.cfg/buildConfig.xml
+    my $build_cfg_filename ="";	
+    
+    if ($gs_mode eq "gs2") {
+	$build_cfg_filename = &FileUtils::filenameConcatenate($build_dir,"build.cfg");
+    } else {
+	$build_cfg_filename = &FileUtils::filenameConcatenate($build_dir, "buildConfig.xml");
+	# gs_mode is GS3. Set the site now if this was not specified as cmdline argument
+	#$site = "localsite" unless defined $site;
+    }
+    
+    # We need to know the buildtype for Solr.
+    # Any change of indexers is already detected and handled by the calling code (buildcol or 
+    # full-rebuild), so that at this stage the config file's buildtype reflects the actual buildtype.
+    
+    # From buildcol.pl we use searchtype for determining buildtype, but for old versions, use buildtype
+    my $buildtype;
+    if (defined $collectcfg->{'buildtype'}) {
+	$buildtype = $collectcfg->{'buildtype'};
+    } elsif (defined $collectcfg->{'searchtypes'} || defined $collectcfg->{'searchtype'}) {
+	$buildtype = "mgpp";
+    } else {
+	$buildtype = "mg"; #mg is the default
+    }
+	
+    # can't do anything without a build directory with something in it to move into index
+    # Except if we're (doing incremental) building for solr, where we want to still
+    # activate and deactivate collections including for the incremental case
+    if(!&FileUtils::directoryExists($build_dir)) {
+	&print_msg("No building folder at $build_dir to move to index.\n");
+	exit -1 unless ($buildtype eq "solr"); #&& $incremental);
+    } elsif (&FileUtils::isDirectoryEmpty($build_dir)) {
+	&print_msg("Nothing in building folder $build_dir to move into index folder.\n");
+	exit -1 unless ($buildtype eq "solr"); #&& $incremental);
+    }
+    
+    # Now the logic in GLI's CollectionManager.java	(processComplete() 
+    # and installCollection()) and Gatherer.configGS3Server().
+    
+    # 1. Get library URL
+    
+    # For web servers that are external to a Greenstone installation, 
+    # the user can pass in their web server's library URL.
+    # For web servers included with GS (like tomcat for GS3 and server.exe 
+    # and apache for GS2), we work out the library URL:
+    if(!$library_url) { 
+	$library_url = &get_library_URL($gs_mode, $library_name); # returns undef if no server is running
+    }
+    
+    # CollectionManager's installCollection phase in GLI
+    # 2. Ping the library URL, and if it's a persistent server and running, release the collection
+    my $is_persistent_server = undef;
+    if($library_url && !$skipactivation) { # undef if no valid server URL
+	if($just_deactivate || !$just_activate) { # either $justdeactivate is provided or neither $justactivate and $justdeactivate are provided
+	    # and deactivation will go through
+	    $is_persistent_server = &do_deactivate($is_persistent_server, $library_url, $gs_mode, $site, $qualified_collection);
+	}
+    }
+
+    # 2b. If we're working with a solr collection, then start up the solrserver now.
+    my $solr_server;
+if(!$just_activate && !$just_deactivate) {
+    my @corenames = ();
+    if($buildtype eq "solr") { # start up the jetty server	
+	my $solr_ext = $ENV{'GEXT_SOLR'}; # from solr_passes.pl
+	unshift (@INC, "$solr_ext/perllib");
+	require solrserver;
+
+	# Solr cores are named without taking the collection-group name into account, since solr
+	# is used for GS3 and GS3 doesn't use collection groups but has the site concept instead
+	my ($colname, $colgroup) = &util::get_collection_parts($qualified_collection);
+
+	# See solrbuilder.pm to get the indexing levels (document, section) from the collectcfg file
+	# Used to generate core names from them and remove cores by name
+	foreach my $level ( @{$collectcfg->{'levels'}} ){
+	    my ($pindex) = $level =~ /^(.)/;
+	    my $indexname = $pindex."idx";
+	    push(@corenames, "$site-$colname-$indexname"); #"$site-$colname-didx", "$site-$colname-sidx"
+        }
+	
+	# If the Solr/Jetty server is not already running, the following starts
+	# it up, and only returns when the server is "reading and listening"	
+	$solr_server = new solrserver($build_dir);
+	$solr_server->start();
+	
+	# We'll be moving building to index. For solr collection, there's further 
+	# special processing to make a corresponding change to the solr.xml
+	# by removing the temporary building cores and (re)creating the index cores
+    }
+
+
+    # 3. Do all the moving building to index stuff now	
+    
+    # If removeold: replace index dir with building dir.
+    # If keepold: move building's contents into index, where only duplicates will get deleted.
+    # removeold and keepold can't both be on at the same time
+    # incremental becomes relevant for solr, though it was irrelevant to what activate.pl does (moving building to index)
+    my $incremental_mode;
+    ($removeold, $keepold, $incremental, $incremental_mode) = &scriptutil::check_removeold_and_keepold($removeold, $keepold, 
+						   $incremental,
+						   $build_dir, # checkdir. Usually archives or export to be deleted. activate.pl deletes building 
+						   $collectcfg);
+	
+    if($removeold) {
+	
+	if(&FileUtils::directoryExists($index_dir)) {
+	    &print_task_msg("Removing \"index\"");
+	    
+	    if ($buildtype eq "solr") {
+		# if solr, remove any cores that are using the index_dir before deleting this dir
+		foreach my $corename (@corenames) {
+		    $solr_server->admin_unload_core($corename);
+		}
+	    }	
+	    
+	    &FileUtils::removeFilesRecursive($index_dir);
+	    
+	    # Wait for a couple of seconds, just for luck
+	    sleep 2;
+	    
+	    if (&FileUtils::directoryExists($index_dir)) {
+		&print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
+	    }
+	}
+	
+	# if remote GS server: gliserver.pl would call activate.pl to activate 
+	# the collection at this point since activate.pl lives on the server side
+	
+	if ($buildtype eq "solr") {
+	    # if solr, remove any cores that are using the building_dir before moving this dir onto index
+	    foreach my $corename (@corenames) {
+		$solr_server->admin_unload_core("building-$corename");
+	    }
+	}
+	
+	# Move the building directory to become the new index directory
+	&print_task_msg("Moving \"building\" -> \"index\"");
+	&FileUtils::moveFiles($build_dir, $index_dir);
+	if(&FileUtils::directoryExists($build_dir) || !&FileUtils::directoryExists($index_dir)) {			
+	    &print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
+	}
+    }
+    elsif ($keepold || $incremental) {
+	if ($buildtype eq "solr") {
+	    # if solr, remove any cores that may be using the building_dir before moving this dir onto index
+	    foreach my $corename (@corenames) {			
+		$solr_server->admin_unload_core("building-$corename") if $solr_server->admin_ping_core("building-$corename");
+	    }
+	}
+	
+	if($build_dir eq $index_dir) { # building_dir can have been set to "index" folder, see incremental-buildcol.pl
+	    &print_task_msg("building folder is index folder, not moving");
+	} else {
+	    # Copy just the contents of building dir into the index dir, overwriting 
+	    # existing files, but don't replace index with building.
+	    &print_task_msg("Moving \"building\" -> \"index\"");		
+	    &FileUtils::moveDirectoryContents($build_dir, $index_dir);
+	}
+    }
+    
+    if ($buildtype eq "solr") {
+	# Call CREATE action to get the old cores pointing to the index folder
+	foreach my $corename (@corenames) {
+	    if($removeold) {
+		# Call CREATE action to get all cores pointing to the index folder, since building is now index
+		$solr_server->admin_create_core($corename, $index_dir);
+		
+	    } elsif ($keepold || $incremental) { 
+		# Call RELOAD core. Should already be using the index_dir directory for $keepold and $incremental case
+		
+		# Ping to see if corename exists, if it does, reload, else create
+		if ($solr_server->admin_ping_core($corename)) {
+		    $solr_server->admin_reload_core($corename); 
+		} else {
+		    $solr_server->admin_create_core($corename, $index_dir);
+		}
+	    }
+	}
+	
+	# regenerate the solr.xml.in from solr.xml in case we are working off a dvd. 
+	$solr_server->solr_xml_to_solr_xml_in();
+    }
+}#end if(!$just_activate && !$just_deactivate)
+    
+    # 4. Ping the library URL, and if it's a persistent server and running, activate the collection again	
+    
+    # Check for success: if building does not exist OR is empty OR if building is index (in which case there was no move)	
+    if($build_dir eq $index_dir || !&FileUtils::directoryExists($build_dir) || &FileUtils::isDirectoryEmpty($build_dir)) {
+	
+	if($library_url && (!$skipactivation)) { # undef if no valid server URL
+	    if($just_activate || !$just_deactivate) { # either $justactivate is provided or neither $justdeactivate and $justactivate are provided 
+		# then activation will go through		
+		$is_persistent_server = &do_activate($is_persistent_server, $library_url, $gs_mode, $site, $qualified_collection);
+	    }	 
 	}
     } else { # installcollection failed		
@@ -838,5 +874,5 @@
     &print_msg("\n");
     
-    if($buildtype eq "solr") {
+    if(!$just_activate && !$just_deactivate && $buildtype eq "solr") {
 	if ($solr_server->explicitly_started()) {
 	    $solr_server->stop();
Index: main/trunk/greenstone2/perllib/DBDrivers/BaseDBDriver.pm
===================================================================
--- main/trunk/greenstone2/perllib/DBDrivers/BaseDBDriver.pm	(revision 30513)
+++ main/trunk/greenstone2/perllib/DBDrivers/BaseDBDriver.pm	(revision 30517)
@@ -57,4 +57,5 @@
     $self->{'supports_persistentconnection'} = 0;
     $self->{'supports_rss'} = 0;
+    $self->{'supports_concurrent_read_and_write'} = 0;
     $self->{'supports_set'} = 0;
     $self->{'write_only'} = 0; # Some drivers are one way - i.e. STDOUTXML
@@ -383,4 +384,14 @@
 
 
+## @function supportsConcurrentReadAndWrite(void)  => integer
+#
+sub supportsConcurrentReadAndWrite 
+{
+    my $self = shift(@_);
+    return $self->{'supports_concurrent_read_and_write'};
+}
+## supportsConcurrentReadAndWrite(void) => integer ##
+
+
 ## @function supportsSet(void) => integer
 #
Index: main/trunk/greenstone2/perllib/DBDrivers/JDBM.pm
===================================================================
--- main/trunk/greenstone2/perllib/DBDrivers/JDBM.pm	(revision 30513)
+++ main/trunk/greenstone2/perllib/DBDrivers/JDBM.pm	(revision 30517)
@@ -50,4 +50,5 @@
     my $self = DBDrivers::70HyphenFormat->new(@_);
     $self->{'default_file_extension'} = 'jdb';
+    $self->{'supports_concurrent_read_and_write'} = 1;
 
     # Executables need a little extra work since we are using Java
Index: main/trunk/greenstone2/perllib/basebuilder.pm
===================================================================
--- main/trunk/greenstone2/perllib/basebuilder.pm	(revision 30513)
+++ main/trunk/greenstone2/perllib/basebuilder.pm	(revision 30517)
@@ -397,4 +397,8 @@
 }
 
+# By default, builders do support make_infodatabase()
+sub supports_make_infodatabase {
+    return 1;
+}
 
 
Index: main/trunk/greenstone2/perllib/buildcolutils.pm
===================================================================
--- main/trunk/greenstone2/perllib/buildcolutils.pm	(revision 30513)
+++ main/trunk/greenstone2/perllib/buildcolutils.pm	(revision 30517)
@@ -674,5 +674,25 @@
     #   processor [jmt12]
     map { local $_=$_; $_->build_indexes($indexname, $indexlevel); } @builders;
-    map { local $_=$_; $_->make_infodatabase(); }  @builders;
+
+    # when incrementally rebuilding a collection using a gdbm database, need to 
+    # deactivate the collecitob before make_infodatabase()
+    map { 
+	local $_=$_; 
+
+	if($_->supports_make_infodatabase()) {
+	    my $infodbtype = $_->{'infodbtype'};
+	    my $dbSupportsConcurrentRW = &dbutil::supportsConcurrentReadAndWrite($infodbtype);
+	
+	    if(!$dbSupportsConcurrentRW) {
+		#$self->{'justdeactivate'} = 1;
+		#$self->activate_collection();
+		#$self->{'justdeactivate'} = 0;
+		$self->activate_collection("just_deactivate");		
+	    }
+	    $_->make_infodatabase();	    
+	}
+
+    }  @builders;
+
     map { local $_=$_; $_->collect_specific(); } @builders;
   }
@@ -759,5 +779,16 @@
 sub activate_collection
 {
-  my $self = shift(@_);
+  my $self = shift(@_);  
+  my $activation_cmd = shift(@_);
+  my($just_activate, $just_deactivate);
+  if(defined $activation_cmd) {
+      $just_deactivate = 1 if($activation_cmd eq "just_deactivate");
+      $just_activate = 1 if($activation_cmd eq "just_activate");
+      if($activation_cmd eq "just_deactivate_and_activate") {
+	  $just_deactivate = 1;
+	  $just_activate = 1;
+      }
+  }
+
   # if buildcol.pl was run with -activate, need to run activate.pl
   # now that building's complete
@@ -773,4 +804,6 @@
     push(@activate_argv, '-keepold') if ($self->{'keepold'});
     push(@activate_argv, '-incremental') if ($self->{'incremental'});
+    push(@activate_argv, '-justactivate') if ($just_activate || $self->{'justactivate'});
+    push(@activate_argv, '-justdeactivate') if ($just_deactivate || $self->{'justdeactivate'});
     my $quoted_argv = join(' ', map { "\"$_\"" } @activate_argv);
     my $activatecol_cmd = '"' . &util::get_perl_exec(). '" -S activate.pl ' . $quoted_argv . ' "' . $self->get_collection() . '"';
Index: main/trunk/greenstone2/perllib/dbutil.pm
===================================================================
--- main/trunk/greenstone2/perllib/dbutil.pm	(revision 30513)
+++ main/trunk/greenstone2/perllib/dbutil.pm	(revision 30517)
@@ -532,4 +532,15 @@
 
 
+## @function supportsConcurrentReadAndWrite(string)  => boolean
+#
+sub supportsConcurrentReadAndWrite
+{
+    my $infodb_type = shift(@_);
+    my $driver = _loadDBDriver($infodb_type);
+    return $driver->supportsConcurrentReadAndWrite();
+}
+## supportsConcurrentReadAndWrite(string) => boolean ##
+
+
 ## @function write_infodb_entry(string, *) => void
 #
Index: main/trunk/greenstone2/perllib/extrabuilder.pm
===================================================================
--- main/trunk/greenstone2/perllib/extrabuilder.pm	(revision 30513)
+++ main/trunk/greenstone2/perllib/extrabuilder.pm	(revision 30517)
@@ -113,4 +113,9 @@
 }
 
+# for now, orthogonalbuilder subclasses don't support/require make_infodatabase()
+sub supports_make_infodatabase {
+    return 0;
+}
+
 sub make_infodatabase {
     my $self = shift (@_);
Index: main/trunk/greenstone2/perllib/inexport.pm
===================================================================
--- main/trunk/greenstone2/perllib/inexport.pm	(revision 30513)
+++ main/trunk/greenstone2/perllib/inexport.pm	(revision 30517)
@@ -1284,5 +1284,5 @@
 	    if ($existing_f =~ m/^$situated_dir/) {
 
-		print STDERR "**** Existing file $existing_f\nis located within\n$situated_dir\n";
+#		print STDERR "**** Existing file $existing_f\nis located within\n$situated_dir\n";
 
 		push(@$reindex_files,$existing_f);
