Index: main/trunk/greenstone2/bin/script/activate.pl
===================================================================
--- main/trunk/greenstone2/bin/script/activate.pl	(revision 25579)
+++ main/trunk/greenstone2/bin/script/activate.pl	(revision 25581)
@@ -180,19 +180,39 @@
 }
 
-# send a pingaction to the GS library
+sub ping {
+	my ($library_url, $command, $gs_mode, $site) = @_;
+	
+	# "Ping" ... "succeeded" expected on success. (Ping on a collection may "not succeed".)	
+	my $check_responsemsg_against_regex = q/succeeded/;	
+	return config($library_url, $command, $check_responsemsg_against_regex, $site);
+}
+
+# send a pingaction to the GS library. General server-level ping.
 sub ping_library {
 	my ($library_url, $gs_mode, $site) = @_;
 	
-	if($gs_mode eq "gs2") {
-		my $command = "?a=ping";
-		# "Ping" ... "succeeded" expected on success. (Ping on a collection may "not succeed".)
-		my $check_responsemsg_against_regex = q/succeeded/;	
-		return config($library_url, $command, $check_responsemsg_against_regex);
-	}
-	elsif ($gs_mode eq "gs3") {
-		my $command = "?a=s&sa=ping";
-		my $check_responsemsg_against_regex = "Ping succeeded."; # no other ping response messages even exist for GS3
-		config($library_url, $command, $check_responsemsg_against_regex, $site);
-	}	
+	my $command = "";
+	if($gs_mode eq "gs2") {		
+		$command = "?a=ping";		
+	}
+	elsif ($gs_mode eq "gs3") {		
+		$command = "?a=s&sa=ping";
+	}
+	return &ping($library_url, $command, $gs_mode, $site);
+}
+
+
+# send a pingaction to a collection in GS library to check if it's active
+sub ping_library_collection {
+	my ($library_url, $gs_mode, $qualified_collection, $site) = @_;
+	
+	my $command = "";
+	if($gs_mode eq "gs2") {		
+		$command = "?a=ping&c=$qualified_collection";
+	}
+	elsif ($gs_mode eq "gs3") {		
+		$command = "?a=s&sa=ping&st=collection&sn=$qualified_collection";		
+	}
+	return &ping($library_url, $command, $gs_mode, $site);
 }
 
@@ -511,11 +531,18 @@
 	
 	my $is_persistent_server = undef;
-	if($library_url) { # running server
-		&print_msg("Pinging $library_url\n");
-		if($library_url && &ping_library($library_url, $gs_mode, $site)) {
-			# server is running, so release the collection if the server is persistent
+	if($library_url) { # undef if no valid server URL
+	
+		&print_msg("Pinging $library_url\n");		
+		if(&ping_library($library_url, $gs_mode, $site)) { # server running
+		
+			# server is running, so release the collection if 
+			# the server is persistent and the collection is active
 			&print_msg("Checking if Greenstone server is persistent\n");
-			$is_persistent_server = &is_persistent($library_url, $gs_mode);		
-			if($is_persistent_server) {
+			$is_persistent_server = &is_persistent($library_url, $gs_mode);
+			
+			&print_msg("Checking if the collection $qualified_collection is already active\n");
+			my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);
+			
+			if($is_persistent_server && $collection_active) {
 				&print_msg("De-activating collection $qualified_collection\n");
 				&deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
@@ -523,4 +550,5 @@
 		}
 	}
+	
 	# 3. Do all the moving building to index stuff now	
 
@@ -564,18 +592,24 @@
 	}
 	
-	
 	# 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	
 	if(!&util::dir_exists($build_dir) || &util::is_dir_empty($build_dir)) {
-		if($library_url) { # undef if no server is running
+	
+		if($library_url) { # undef if no valid server URL
+		
 			&print_msg("Pinging $library_url\n");
-			if(&ping_library($library_url, $gs_mode, $site)) {
-				# don't need to work out persistency of server more than once
+			if(&ping_library($library_url, $gs_mode, $site)) { # server running
+			
+				# don't need to work out persistency of server more than once, since the libraryURL hasn't changed				
 				if(!defined $is_persistent_server) {
 					&print_msg("Checking if Greenstone server is persistent\n");
 					$is_persistent_server = &is_persistent($library_url, $gs_mode);
-				}	
-				if($is_persistent_server) {
+				}
+				
+				&print_msg("Checking if the collection $qualified_collection is not already active\n");				
+				my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);					
+					
+				if($is_persistent_server && !$collection_active) {					
 					&print_msg("Activating collection $qualified_collection\n");
 					&activate_collection($library_url, $gs_mode, $qualified_collection, $site);
@@ -590,6 +624,2 @@
 
 &main(scalar(@ARGV),@ARGV);
-
-
-
-
