Index: /main/trunk/greenstone2/bin/script/activate.pl
===================================================================
--- /main/trunk/greenstone2/bin/script/activate.pl	(revision 30519)
+++ /main/trunk/greenstone2/bin/script/activate.pl	(revision 30520)
@@ -90,465 +90,9 @@
 use File::Find;
 
-use HTTP::Response;
-use LWP::Simple qw($ua !head); # import useragent object as $ua from the full LWP to use along with LWP::Simple
-		# don't import LWP::Simple's head function by name since it can conflict with CGI:head())			
-#use CGI qw(:standard);  # then only CGI.pm defines a head()
-use Net::Ping;
-use URI;
-
+# Greenstone modules
+use activate;
 use colcfg;
 use scriptutil;
 use util;
-#use enum;
-
-# enumerations in perl, http://stackoverflow.com/questions/473666/does-perl-have-an-enumeration-type
-# Unfortunately, not part of perl's core
-#use enum qw(LEVEL_NONE LEVEL_ERROR LEVEL_INFO LEVEL_DEBUG); # debugging levels NONE == 0, ERROR=1 INFO=2 DEBUG=3
-
-# global variables
-#my $default_verbosity = LEVEL_ERROR; # by default we display basic error messages
-
-my $default_verbosity = 2; # by default we display basic error and info messages
-
-sub print_task_msg {
-    my ($task_msg, $verbosity_setting) = @_;
-    
-    $verbosity_setting = $default_verbosity unless $verbosity_setting;
-    #$verbosity_setting = 1 unless defined $verbosity;
-    if($verbosity_setting >= 1) {
-	print STDERR "\n";
-	print STDERR "************************\n";
-	print STDERR "* $task_msg\n";
-	print STDERR "************************\n";
-    }
-}
-
-# Prints messages if the verbosity is right. Does not add new lines.
-sub print_msg {
-    my ($msg, $min_verbosity, $verbosity_setting) = @_;
-    
-    # only display error messages if the current 
-    # verbosity setting >= the minimum verbosity level
-    # needed for that message to be displayed.
-	
-    $verbosity_setting = $default_verbosity unless defined $verbosity_setting;
-    $min_verbosity = 1 unless defined $min_verbosity;
-    if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
-	print STDERR "$msg";
-    }
-}
-
-# Method to send a command to a GS2 or GS3 library_URL
-# the commands used in this script can be activate, deactivate, ping, 
-# and is-persistent (is-persistent only implemented for GS2).
-sub config {
-    my ($library_url, $command, $check_message_against_regex, $site, $expected_error_code, $silent) = @_; 
-    # Gatherer.java's configGS3Server doesn't use the site variable
-    # so we don't have to either
-    
-    # for GS2, getting the HTTP status isn't enough, we need to read the output
-    # since this is what CollectionManager.config() stipulates.
-    # Using LWP::UserAgent::get($url) for this	
-    
-    if(!defined $library_url) {
-	return 0;
-    }
-    else {
-	$ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
-	# Need to set this, else it takes I don't know how long to timeout 
-	# http://www.perlmonks.org/?node_id=618534
-	
-	# http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
-	# use LWP::UserAgent's get($url) since it returns an HTTP::Response code
-	
-	my $response_obj = $ua->get( $library_url.$command);
-	
-	# $response_obj->content stores the content and $response_obj->code the HTTP response code
-	my $response_code = $response_obj->code();
-	
-	if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
-	    &print_msg("*** Command $library_url$command\n", 3);
-	    &print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
-	    
-	    # check the page content is as expected
-	    my $response_content = $response_obj->content;
-	    my $resultstr = $response_content;
-	    $resultstr =~ s@.*gs_content\"\>@@s;		
-	    $resultstr =~ s@</div>.*@@s;
-	    
-	    if($response_content =~ m/$check_message_against_regex/) {
-		&print_msg(" Response as expected.\n", 3);
-		&print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
-		return 1;
-	    } else {
-		# if we expect the collection to be inactive, then we'd be in silent mode: if so,
-		# don't print out the "ping did not succeed" response, but print out any other messages
-		
-		# So we only suppress the ping col "did not succeed" response if we're in silent mode
-		# But if any message other than ping "did not succeed" is returned, we always print it
-		if($response_content !~ m/did not succeed/ || !$silent) {
-		    &print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
-		    &print_msg("*** Got message:\n$response_content.\n", 4);
-		    &print_msg("*** Got result:\n$resultstr\n", 3);
-		}
-		return 0; # ping on a collection may "not succeed."
-	    }
-	} 
-	elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
-	    # check the page content is as expected
-	    if(defined $expected_error_code && $response_code == $expected_error_code) {
-		&print_msg(" Response status $response_code as expected.\n", 3);
-	    } else {
-		&print_msg("*** Command $library_url$command\n");
-		&print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
-	    }
-	    return 0; # return false, since the response_code was an error, expected or not
-	}
-	else {
-	    &print_msg("*** Command $library_url$command\n");
-	    &print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
-	    return 0;
-	}
-    }	
-}
-
-sub deactivate_collection {
-    my ($library_url, $gs_mode, $qualified_collection, $site) = @_;	
-    
-    if($gs_mode eq "gs2") {
-	my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
-	my $check_message_against_regex = q/configured release-collection/;
-	config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
-    }
-    elsif ($gs_mode eq "gs3") {
-	my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
-	my $check_message_against_regex = "collection: $qualified_collection deactivated";
-	config($library_url, $DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
-    }	
-}
-
-sub activate_collection {
-    my ($library_url, $gs_mode, $qualified_collection, $site) = @_;	
-    
-    if($gs_mode eq "gs2") {
-	my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
-	my $check_message_against_regex = q/configured add-collection/;
-	config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
-    }
-    elsif ($gs_mode eq "gs3") {
-	my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
-	my $check_message_against_regex = "collection: $qualified_collection activated";
-	config($library_url, $ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
-    }	
-}
-
-sub ping {
-    my ($library_url, $command, $gs_mode, $site, $silent) = @_;
-    
-    # If the GS server is not running, we *expect* to see a "500" status code.
-    # If the GS server is running, then "Ping" ... "succeeded" is expected on success. 
-    # When pinging an inactive collection, it will say it did "not succeed". This is
-    # a message of interest to return.
-    my $check_responsemsg_against_regex = q/(succeeded)/;
-    my $expected_error_code = 500;
-    
-    &print_msg("*** COMMAND WAS: |$command|***\n", 4);
-    
-    return config($library_url, $command, $check_responsemsg_against_regex, $site, $expected_error_code, $silent);
-}
-
-# send a pingaction to the GS library. General server-level ping.
-sub ping_library {
-    my ($library_url, $gs_mode, $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, $silent) = @_;
-    
-    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, $silent);
-}
-
-# return true if server is persistent, by calling is-persistent on library_url
-# this is only for GS2, since the GS3 server is always persistent
-sub is_persistent {
-    my ($library_url, $gs_mode) = @_;
-    
-    if($gs_mode eq "gs3") { # GS3 server is always persistent
-	return 1;
-    }
-    
-    my $command = "?a=is-persistent";	
-    my $check_responsemsg_against_regex = q/true/;	# isPersistent: true versus isPersistent: false 	
-    return config($library_url, $command, $check_responsemsg_against_regex);
-}
-
-sub get_library_URL {
-    my ($gs_mode, $lib_name) = @_; # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
-    
-    # If we get here, we are dealing with a server included with GS.
-    # For GS3, we ask ant for the library URL.
-    # For GS2, we derive the URL from the llssite.cfg file.
-    
-    my $url = undef;	
-    
-    if($gs_mode eq "gs2") {		
-	my $llssite_cfg = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "llssite.cfg");
-	
-	if(-f $llssite_cfg) {
-	    # check llssite.cfg for line with url property
-	    # for server.exe also need to use portnumber and enterlib properties			
-	    
-	    # Read in the entire contents of the file in one hit
-	    if (!open (FIN, $llssite_cfg)) {
-		&print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
-		return undef;
-	    }
-	    
-	    my $contents;
-	    sysread(FIN, $contents, -s FIN);			
-	    close(FIN);
-	    
-	    my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
-	    my $enterlib = "";
-	    my $portnumber = ""; # will remain empty (implicit port 80) unless it's specifically been assigned
-	    
-	    foreach my $line (@lines) {				
-		if($line =~ m/^url=(.*)$/) {
-		    $url = $1;					
-		} elsif($line =~ m/^enterlib=(.*)$/) {
-		    $enterlib = $1;					
-		} elsif($line =~ m/^portnumber=(.*)$/) {
-		    $portnumber = $1;					
-		}	
-	    }
-	    
-	    if(!$url) {
-		return undef;
-	    }
-	    elsif($url eq "URL_pending") { # library is not running
-		# do not process url=URL_pending in the file, since for server.exe 
-		# this just means the Enter Library button hasn't been pressed yet				
-		$url = undef;
-	    }
-	    else { 
-		# In the case of server.exe, need to do extra work to get the proper URL
-		# But first, need to know whether we're indeed dealing with server.exe:
-		
-		# compare the URL's domain to the full URL
-		# E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
-		my $uri = URI->new( $url );
-		my $host = $uri->host;
-		#print STDERR "@@@@@ host: $host\n";
-		if($url =~ m/http:\/\/$host(\/)?$/) {
-		    #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
-		    # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)					
-		    # If the domain with http:// prefix is completely the same as the URL, assume server.exe
-		    # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
-		    $url = $url.":".$portnumber.$enterlib;			
-		} # else, apache web server			
-				
-	    }			
-	}
-    } elsif($gs_mode eq "gs3") {
-	# Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
-	# app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
-	
-	# Or, run the new target get-default-servlet-url
-	# the output can look like:
-	#
-	# Buildfile: build.xml
-	# 	[echo] os.name: Windows Vista
-	#
-	# get-default-servlet-url:
-	#	[echo] http://localhost:8383/greenstone3/library
-	# BUILD SUCCESSFUL
-	# Total time: 0 seconds
-	
-	#my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
-	# see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
-	
-	# The get-default-servlet-url ant target can be run from anywhere by specifying the
-	# location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
-	# GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
-	# to run the activate.pl script in the first place
-	my $perl_command = "ant -buildfile \"$ENV{'GSDL3SRCHOME'}/build.xml\" get-default-servlet-url";
-	
-	if (open(PIN, "$perl_command |")) {
-	    while (defined (my $perl_output_line = <PIN>)) {
-		if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
-		    $url="http://".$1;
-		}
-	    }
-	    close(PIN);
-	} else {
-	    &print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
-	}
-	if(defined $lib_name) {
-	    # replace the servlet_name portion of the url found, with the given library_name
-	    $url =~ s@/[^/]*$@/$lib_name@;
-	}
-    }
-    
-    # either the url is still undef or it is now set
-    #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;		
-    #print STDERR "\n@@@@@ URL still undef\n" if !$url;
-    return $url;
-}
-
-### UNUSED METHODS TO MOVE TO util.pm?
-
-# This method is now unused. Using ping_library instead to send the ping action to a
-# GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
-# http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm
-# and http://www.perlmonks.org/?node_id=618534
-sub is_URL_active {
-    my $url = shift(@_); # gs3 or gs2 URL	
-    
-    my $status = 0;
-    if(defined $url) {
-	$ua->timeout(10); # set LWP useragent to 5s max timeout for testing the URL
-	# Need to set this, else it takes I don't know how long to timeout 
-	# http://www.perlmonks.org/?node_id=618534
-	
-	$status = LWP::Simple::head($url); # returns empty list of headers if it fails
-	# LWP::Simple::get($url) is more intensive, so don't need to do that
-	#print STDERR "**** $url is alive.\n" if $status;
-    }
-    return $status;
-}
-
-# Pinging seems to always return true, so this method doesn't work
-sub pingHost {
-    my $url = shift(@_); # gs3 or gs2 URL
-    
-    my $status = 0;
-    if(defined $url) {
-	# Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
-	# "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too	
-	
-	#my $host = $url;		
-	#$host =~ s@^http:\/\/(www.)?@@;		
-	#$host =~ s@\/.*@@;
-	#print STDERR "**** HOST: $host\n";
-	
-	# More robust way
-	# http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
-	my $uri = URI->new( $url );
-	my $host = $uri->host; 
-	
-	# Ping the host. http://perldoc.perl.org/Net/Ping.html	
-	my $p = Net::Ping->new();		
-	$status = $p->ping($host); # || 0. Appears to set to undef rather than 0
-	print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
-	$p->close();		
-    } 
-    # return whether pinging was a success or failure
-    return $status;
-}
-
-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
-	    
-	    # 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) { # only makes sense to issue activate and deactivate cmds to a persistent server
-		
-		&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 ($collection_active) {
-		    &print_msg("De-activating collection $qualified_collection\n");
-		    &deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
-		}
-		else {
-		    &print_msg("Collection is not active => No need to deactivate\n");
-		}
-	    }
-	    else {
-		&print_msg("Server is not persistent => No need to deactivate collection\n");
-	    }
-	}
-	else {
-	    &print_msg("No response to Ping => Taken to mean server is not running\n");
-	}
-
-    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
-		
-		# 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) { # persistent server, so can try activating collection
-		    
-		    &print_msg("Checking if the collection $qualified_collection is not already active\n");
-		    
-		    # Since we could have deactivated the collection at this point,
-		    # it is likely that it is not yet active. When pinging the collection
-		    # a "ping did not succeed" message is expected, therefore tell the ping
-		    # to proceed silently
-		    my $silent = 1;
-		    my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site, $silent);
-		    
-		    if (!$collection_active) {
-			&print_msg(" Collection is not active.\n");
-			&print_msg("Activating collection $qualified_collection\n");
-			&activate_collection($library_url, $gs_mode, $qualified_collection, $site);
-			
-			# unless an error occurred, the collection should now be active:
-			$collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site); # not silent if ping did not succeed
-			if(!$collection_active) {
-			    &print_msg("ERROR: collection $qualified_collection did not get activated\n");
-			}
-		    }
-		    else {
-			&print_msg("Collection is already active => No need to activate\n");
-		    }
-		}
-		else {
-		    &print_msg("Server is not persistent => No need to activate collection\n");
-		}
-	    }
-	    else {
-		&print_msg("No response to Ping => Taken to mean server is not running\n");
-	    }
-
-    return $is_persistent_server;
-}
 
 
@@ -577,5 +121,5 @@
     
     # http://stackoverflow.com/questions/6156742/how-can-i-capture-the-complete-commandline-in-perl
-    #print STDERR "@@@@@@@@@ ACTIVATE CMD: " . join " ", $0, @ARGV;
+    #print STDERR "@@@@@@@@@ ACTIVATE CMD: " . join " ", $0, @ARGV . "\n";
 	
     # get the collection details
@@ -592,11 +136,8 @@
     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
+
+    my $default_verbosity = 2;
+
+    my $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
     
@@ -627,10 +168,4 @@
 	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") {
@@ -653,10 +188,12 @@
     $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");
+
+    my $gsserver = new activate($qualified_collection, $site, $default_verbosity, $build_dir, $index_dir, $collect_dir, $library_url, $library_name);
+
+    $gsserver->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");
+	$gsserver->print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
 	exit -1;
     }
@@ -665,8 +202,6 @@
     # 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 $gs_mode = $gsserver->{'gs_mode'}; # "gs2" or "gs3", based on $site variable
+
     my $collect_cfg_filename = &colcfg::get_collect_cfg_name(STDERR, $gs_mode);
     my $collectcfg = &colcfg::read_collection_cfg ($collect_cfg_filename,$gs_mode);
@@ -701,8 +236,8 @@
     # 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");
+	$gsserver->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");
+	$gsserver->print_msg("Nothing in building folder $build_dir to move into index folder.\n");
 	exit -1 unless ($buildtype eq "solr"); #&& $incremental);
     }
@@ -718,20 +253,15 @@
     # 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
+	$library_url = $gsserver->get_library_URL(); # 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);
-	}
+    # 2. Ping the library URL, and if it's a persistent server and running, release the collection    
+    if($library_url && !$skipactivation) { # undef if no valid server URL	
+	$gsserver->do_deactivate();	
     }
 
     # 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	
@@ -778,5 +308,5 @@
 	
 	if(&FileUtils::directoryExists($index_dir)) {
-	    &print_task_msg("Removing \"index\"");
+	    $gsserver->print_task_msg("Removing \"index\"");
 	    
 	    if ($buildtype eq "solr") {
@@ -793,5 +323,5 @@
 	    
 	    if (&FileUtils::directoryExists($index_dir)) {
-		&print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
+		$gsserver->print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
 	    }
 	}
@@ -808,8 +338,8 @@
 	
 	# Move the building directory to become the new index directory
-	&print_task_msg("Moving \"building\" -> \"index\"");
+	$gsserver->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
+	    $gsserver->print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
 	}
     }
@@ -823,9 +353,9 @@
 	
 	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");
+	    $gsserver->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\"");		
+	    $gsserver->print_task_msg("Moving \"building\" -> \"index\"");		
 	    &FileUtils::moveDirectoryContents($build_dir, $index_dir);
 	}
@@ -854,5 +384,5 @@
 	$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	
@@ -861,18 +391,15 @@
     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);
-	    }	 
+	if($library_url && !$skipactivation) { # undef if no valid server URL
+	    $gsserver->do_activate();
 	}
     } else { # installcollection failed		
 	#CollectionManager.Preview_Ready_Failed
-	&print_msg("Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n");
-    }
-    
-    &print_msg("\n");
-    
-    if(!$just_activate && !$just_deactivate && $buildtype eq "solr") {
+	$gsserver->print_msg("Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n");
+    }
+    
+    $gsserver->print_msg("\n");
+    
+    if($buildtype eq "solr") {
 	if ($solr_server->explicitly_started()) {
 	    $solr_server->stop();
Index: /main/trunk/greenstone2/perllib/activate.pm
===================================================================
--- /main/trunk/greenstone2/perllib/activate.pm	(revision 30520)
+++ /main/trunk/greenstone2/perllib/activate.pm	(revision 30520)
@@ -0,0 +1,612 @@
+#############################################################################
+#
+# activate.pm -- functions to get the GS library URL, ping the library URL, 
+# activate and deactivate a collection.
+#
+# A component of the Greenstone digital library software
+# from the New Zealand Digital Library Project at the
+# University of Waikato, New Zealand.
+#
+# Copyright (C) 1999 New Zealand Digital Library Project
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+###############################################################################
+
+package activate;
+
+
+use strict;
+no strict 'refs'; # allow filehandles to be variables and vice versa
+no strict 'subs'; # allow barewords (eg STDERR) as function arguments
+
+use HTTP::Response;
+use LWP::Simple qw($ua !head); # import useragent object as $ua from the full LWP to use along with LWP::Simple
+		# don't import LWP::Simple's head function by name since it can conflict with CGI:head())			
+#use CGI qw(:standard);  # then only CGI.pm defines a head()
+use Net::Ping;
+use URI;
+
+use printusage;
+use parse2;
+
+#use enum;
+# enumerations in perl, http://stackoverflow.com/questions/473666/does-perl-have-an-enumeration-type
+# Unfortunately, not part of perl's core
+#use enum qw(LEVEL_NONE LEVEL_ERROR LEVEL_INFO LEVEL_DEBUG); # debugging levels NONE == 0, ERROR=1 INFO=2 DEBUG=3
+
+
+sub new
+{
+  my $class = shift(@_);
+
+  my ($qualified_collection, $site, $verbosity, $build_dir, $index_dir, $collect_dir, $library_url, $library_name) = @_;
+
+  my $self = {'build_dir' => $build_dir,
+              'index_dir' => $index_dir,
+              'collect_dir' => $collect_dir,
+              'site' => $site,
+              'qualified_collection' => $qualified_collection,
+	      #'is_persistent_server' => undef,
+              'library_url' => $library_url || $ENV{'GSDL_LIBRARY_URL'} || undef, # to be specified on the cmdline if not using a GS-included web server
+              'library_name' => $library_name,
+	      #'gs_mode' => "gs2",
+	      'verbosity' => $verbosity || 2
+             };
+
+
+  # Do we need 'listall' support in buildcol? If so, copy code from inexport
+  # later [jmt12]
+
+  if ((defined $site) && ($site ne "")) { # GS3
+      $self->{'gs_mode'} = "gs3";
+  } else {
+      $self->{'gs_mode'} = "gs2";
+  }
+
+  return bless($self, $class);
+}
+
+## TODO: gsprintf to $self->{'out'} in these 2 print functions
+## See buildcolutils.pm new() for setting up $out
+
+sub print_task_msg {
+    my $self = shift(@_);
+    my ($task_msg, $verbosity_setting) = @_;
+    
+    $verbosity_setting = $self->{'verbosity'} unless $verbosity_setting;
+    #$verbosity_setting = 1 unless defined $verbosity;
+    if($verbosity_setting >= 1) {
+	print STDERR "\n";
+	print STDERR "************************\n";
+	print STDERR "* $task_msg\n";
+	print STDERR "************************\n";
+    }
+}
+
+# Prints messages if the verbosity is right. Does not add new lines.
+sub print_msg {
+    my $self = shift(@_);
+    my ($msg, $min_verbosity, $verbosity_setting) = @_;
+
+    # only display error messages if the current 
+    # verbosity setting >= the minimum verbosity level
+    # needed for that message to be displayed.
+	
+    $verbosity_setting = $self->{'verbosity'} unless defined $verbosity_setting;
+    $min_verbosity = 1 unless defined $min_verbosity;
+    if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
+	print STDERR "$msg";
+    }
+}
+
+# Method to send a command to a GS2 or GS3 library_URL
+# the commands used in this script can be activate, deactivate, ping, 
+# and is-persistent (is-persistent only implemented for GS2).
+sub config {
+    my $self = shift(@_);
+    my ($command, $check_message_against_regex, $site, $expected_error_code, $silent) = @_;
+
+    my ($library_url);
+    $library_url = $self->{'library_url'};
+    #$site = $self->{'site'};
+
+
+    # Gatherer.java's configGS3Server doesn't use the site variable
+    # so we don't have to either
+    
+    # for GS2, getting the HTTP status isn't enough, we need to read the output
+    # since this is what CollectionManager.config() stipulates.
+    # Using LWP::UserAgent::get($url) for this	
+    
+    if(!defined $library_url) {
+	return 0;
+    }
+    else {
+	$ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
+	# Need to set this, else it takes I don't know how long to timeout 
+	# http://www.perlmonks.org/?node_id=618534
+	
+	# http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
+	# use LWP::UserAgent's get($url) since it returns an HTTP::Response code
+	
+	my $response_obj = $ua->get($library_url.$command);
+	
+	# $response_obj->content stores the content and $response_obj->code the HTTP response code
+	my $response_code = $response_obj->code();
+	
+	if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
+	    $self->print_msg("*** Command $library_url$command\n", 3);
+	    $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
+	    
+	    # check the page content is as expected
+	    my $response_content = $response_obj->content;
+	    my $resultstr = $response_content;
+	    $resultstr =~ s@.*gs_content\"\>@@s;		
+	    $resultstr =~ s@</div>.*@@s;
+	    
+	    if($response_content =~ m/$check_message_against_regex/) {
+		$self->print_msg(" Response as expected.\n", 3);
+		$self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
+		return 1;
+	    } else {
+		# if we expect the collection to be inactive, then we'd be in silent mode: if so,
+		# don't print out the "ping did not succeed" response, but print out any other messages
+		
+		# So we only suppress the ping col "did not succeed" response if we're in silent mode
+		# But if any message other than ping "did not succeed" is returned, we always print it
+		if($response_content !~ m/did not succeed/ || !$silent) {
+		    $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
+		    $self->print_msg("*** Got message:\n$response_content.\n", 4);
+		    $self->print_msg("*** Got result:\n$resultstr\n", 3);
+		}
+		return 0; # ping on a collection may "not succeed."
+	    }
+	} 
+	elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
+	    # check the page content is as expected
+	    if(defined $expected_error_code && $response_code == $expected_error_code) {
+		$self->print_msg(" Response status $response_code as expected.\n", 3);
+	    } else {
+		$self->print_msg("*** Command $library_url$command\n");
+		$self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
+	    }
+	    return 0; # return false, since the response_code was an error, expected or not
+	}
+	else {
+	    $self->print_msg("*** Command $library_url$command\n");
+	    $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
+	    return 0;
+	}
+    }	
+}
+
+sub deactivate_collection {
+    my $self = shift(@_);
+
+    my ($library_url, $gs_mode, $qualified_collection, $site);
+    $library_url = $self->{'library_url'};
+    $gs_mode = $self->{'gs_mode'};
+    $qualified_collection = $self->{'qualified_collection'};
+    $site = $self->{'site'};
+    
+    if($gs_mode eq "gs2") {
+	my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
+	my $check_message_against_regex = q/configured release-collection/;
+	$self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
+    }
+    elsif ($gs_mode eq "gs3") {
+	my $DEACTIVATE_COMMAND = "?a=s&sa=d&st=collection&sn=";
+	my $check_message_against_regex = "collection: $qualified_collection deactivated";
+	$self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
+    }	
+}
+
+sub activate_collection {
+    my $self = shift(@_);
+
+    my ($library_url, $gs_mode, $qualified_collection, $site);
+    $library_url = $self->{'library_url'};
+    $gs_mode = $self->{'gs_mode'};
+    $qualified_collection = $self->{'qualified_collection'};
+    $site = $self->{'site'};
+
+    if($gs_mode eq "gs2") {
+	my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
+	my $check_message_against_regex = q/configured add-collection/;
+	$self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
+    }
+    elsif ($gs_mode eq "gs3") {
+	my $ACTIVATE_COMMAND = "?a=s&sa=a&st=collection&sn=";
+	my $check_message_against_regex = "collection: $qualified_collection activated";
+	$self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, $site);
+    }	
+}
+
+sub ping {
+    my $self = shift(@_);
+    my $command = shift(@_);
+    my $silent = shift(@_);
+
+    my ($library_url, $gs_mode, $site);
+    $library_url = $self->{'library_url'};     
+    $gs_mode = $self->{'gs_mode'};
+    $site = $self->{'site'};
+    
+    # If the GS server is not running, we *expect* to see a "500" status code.
+    # If the GS server is running, then "Ping" ... "succeeded" is expected on success. 
+    # When pinging an inactive collection, it will say it did "not succeed". This is
+    # a message of interest to return.
+    my $check_responsemsg_against_regex = q/(succeeded)/;
+    my $expected_error_code = 500;
+    
+    $self->print_msg("*** COMMAND WAS: |$command|***\n", 4);
+    
+    return $self->config($command, $check_responsemsg_against_regex, $site, $expected_error_code, $silent);
+}
+
+# send a pingaction to the GS library. General server-level ping.
+sub ping_library {
+    my $self = shift(@_);
+
+    my ($library_url, $gs_mode, $site);
+    $library_url = $self->{'library_url'};
+    $gs_mode = $self->{'gs_mode'};
+    $site = $self->{'site'};
+
+    my $command = "";
+    if($gs_mode eq "gs2") {		
+	$command = "?a=ping";		
+    }
+    elsif ($gs_mode eq "gs3") {		
+	$command = "?a=s&sa=ping";
+    }
+    return $self->ping($command);
+}
+
+
+# send a pingaction to a collection in GS library to check if it's active
+sub ping_library_collection {
+    my $self = shift(@_);
+    my $silent = shift(@_);
+
+    my ($library_url, $gs_mode, $qualified_collection, $site);
+    $library_url = $self->{'library_url'};
+    $gs_mode = $self->{'gs_mode'};
+    $qualified_collection = $self->{'qualified_collection'};
+    $site = $self->{'site'};
+    #$silent = $self->{'silent'};
+
+    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 $self->ping($command, $silent);
+}
+
+# return true if server is persistent, by calling is-persistent on library_url
+# this is only for GS2, since the GS3 server is always persistent
+sub is_persistent {
+    my $self = shift(@_);
+
+    my ($library_url, $gs_mode);
+    $library_url = $self->{'library_url'};
+    $gs_mode = $self->{'gs_mode'};
+    
+    if($gs_mode eq "gs3") { # GS3 server is always persistent
+	return 1;
+    }
+    
+    my $command = "?a=is-persistent";	
+    my $check_responsemsg_against_regex = q/true/;	# isPersistent: true versus isPersistent: false 	
+    return $self->config($command, $check_responsemsg_against_regex);
+}
+
+sub set_library_URL {
+    my $self = shift(@_);
+    my $library_url = shift(@_);
+    $self->{'library_url'} = $library_url;
+}
+
+sub get_library_URL {
+    my $self = shift(@_);    
+    
+    # For web servers that are external to a Greenstone installation, 
+    # the user can pass in their web server's library URL.
+    if($self->{'library_url'}) {
+	return $self->{'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:
+    my ($gs_mode, $lib_name); # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
+    $gs_mode = $self->{'gs_mode'};
+    $lib_name = $self->{'library_name'};
+    
+    # If we get here, we are dealing with a server included with GS.
+    # For GS3, we ask ant for the library URL.
+    # For GS2, we derive the URL from the llssite.cfg file.
+    
+    my $url = undef;	
+    
+    if($gs_mode eq "gs2") {		
+	my $llssite_cfg = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "llssite.cfg");
+	
+	if(-f $llssite_cfg) {
+	    # check llssite.cfg for line with url property
+	    # for server.exe also need to use portnumber and enterlib properties			
+	    
+	    # Read in the entire contents of the file in one hit
+	    if (!open (FIN, $llssite_cfg)) {
+		$self->print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
+		return undef;
+	    }
+	    
+	    my $contents;
+	    sysread(FIN, $contents, -s FIN);			
+	    close(FIN);
+	    
+	    my @lines = split(/[\n\r]+/, $contents); # split on carriage-returns and/or linefeeds
+	    my $enterlib = "";
+	    my $portnumber = ""; # will remain empty (implicit port 80) unless it's specifically been assigned
+	    
+	    foreach my $line (@lines) {				
+		if($line =~ m/^url=(.*)$/) {
+		    $url = $1;					
+		} elsif($line =~ m/^enterlib=(.*)$/) {
+		    $enterlib = $1;					
+		} elsif($line =~ m/^portnumber=(.*)$/) {
+		    $portnumber = $1;					
+		}	
+	    }
+	    
+	    if(!$url) {
+		return undef;
+	    }
+	    elsif($url eq "URL_pending") { # library is not running
+		# do not process url=URL_pending in the file, since for server.exe 
+		# this just means the Enter Library button hasn't been pressed yet				
+		$url = undef;
+	    }
+	    else { 
+		# In the case of server.exe, need to do extra work to get the proper URL
+		# But first, need to know whether we're indeed dealing with server.exe:
+		
+		# compare the URL's domain to the full URL
+		# E.g. for http://localhost:8383/greenstone3/cgi-bin, the domain is localhost:8383
+		my $uri = URI->new( $url );
+		my $host = $uri->host;
+		#print STDERR "@@@@@ host: $host\n";
+		if($url =~ m/http:\/\/$host(\/)?$/) {
+		    #if($url !~ m/http:\/\/$host:$portnumber(\/)?/ || $url =~ m/http:\/\/$host(\/)?$/) {
+		    # (if the URL does not contain the portnumber, OR if the port is implicitly 80 and)					
+		    # If the domain with http:// prefix is completely the same as the URL, assume server.exe
+		    # then the actual URL is the result of suffixing the port and enterlib properties in llssite.cfg
+		    $url = $url.":".$portnumber.$enterlib;			
+		} # else, apache web server			
+				
+	    }			
+	}
+    } elsif($gs_mode eq "gs3") {
+	# Either check build.properties for tomcat.server, tomcat.port and app.name (and default servlet name).
+	# app.name is stored in app.path by build.xml. Need to move app.name in build.properties from build.xml
+	
+	# Or, run the new target get-default-servlet-url
+	# the output can look like:
+	#
+	# Buildfile: build.xml
+	# 	[echo] os.name: Windows Vista
+	#
+	# get-default-servlet-url:
+	#	[echo] http://localhost:8383/greenstone3/library
+	# BUILD SUCCESSFUL
+	# Total time: 0 seconds
+	
+	#my $output = qx/ant get-default-servlet-url/; # backtick operator, to get STDOUT (else 2>&1)
+	# see http://stackoverflow.com/questions/799968/whats-the-difference-between-perls-backticks-system-and-exec
+	
+	# The get-default-servlet-url ant target can be run from anywhere by specifying the
+	# location of GS3's ant build.xml buildfile. Activate.pl can be run from anywhere for GS3
+	# GSDL3SRCHOME will be set for GS3 by gs3-setup.sh, a step that would have been necessary
+	# to run the activate.pl script in the first place
+	my $perl_command = "ant -buildfile \"$ENV{'GSDL3SRCHOME'}/build.xml\" get-default-servlet-url";
+	
+	if (open(PIN, "$perl_command |")) {
+	    while (defined (my $perl_output_line = <PIN>)) {
+		if($perl_output_line =~ m@http:\/\/(\S*)@) { # grab all the non-whitespace chars
+		    $url="http://".$1;
+		}
+	    }
+	    close(PIN);
+	} else {
+	    $self->print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
+	}
+	if(defined $lib_name) {
+	    # replace the servlet_name portion of the url found, with the given library_name
+	    $url =~ s@/[^/]*$@/$lib_name@;
+	}
+    }
+    
+    # either the url is still undef or it is now set
+    #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;		
+    #print STDERR "\n@@@@@ URL still undef\n" if !$url;
+    
+    $self->{'library_url'} = $url;
+    return $url;
+}
+
+
+sub do_deactivate {
+    my $self = shift(@_);
+
+    my($is_persistent_server, $library_url, $gs_mode, $site, $qualified_collection);
+    $is_persistent_server = $self->{'is_persistent_server'};
+    $library_url = $self->get_library_URL(); #may not yet be set, so can't do $self->{'library_url'};
+    $site = $self->{'site'};
+    $gs_mode = $self->{'gs_mode'};
+    $qualified_collection = $self->{'qualified_collection'};
+
+
+    $self->print_msg("Pinging $library_url\n");		
+    if ($self->ping_library()) { # server running
+	
+	# server is running, so release the collection if 
+	# the server is persistent and the collection is active	
+
+	# don't need to work out persistency of server more than once, since the libraryURL hasn't changed
+	if (!defined $is_persistent_server) {
+	    $self->print_msg("Checking if Greenstone server is persistent\n");
+	    $is_persistent_server = $self->is_persistent();
+	    $self->{'is_persistent_server'} = $is_persistent_server;
+	}
+	
+	if ($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
+	    
+	    $self->print_msg("Checking if the collection $qualified_collection is already active\n");
+	    my $collection_active = $self->ping_library_collection();
+	    
+	    if ($collection_active) {
+		$self->print_msg("De-activating collection $qualified_collection\n");
+		$self->deactivate_collection();
+	    }
+	    else {
+		$self->print_msg("Collection is not active => No need to deactivate\n");
+	    }
+	}
+	else {
+	    $self->print_msg("Server is not persistent => No need to deactivate collection\n");
+	}
+    }
+    else {
+	$self->print_msg("No response to Ping => Taken to mean server is not running\n");
+    }
+    
+    return $is_persistent_server;
+}
+
+sub do_activate {
+    my $self = shift @_;
+
+    my($is_persistent_server, $library_url, $gs_mode, $site, $qualified_collection);
+    $is_persistent_server = $self->{'is_persistent_server'};
+    $library_url = $self->get_library_URL(); #may not yet be set, so can't do $self->{'library_url'};
+    $site = $self->{'site'};
+    $gs_mode = $self->{'gs_mode'};
+    $qualified_collection = $self->{'qualified_collection'};
+
+    $self->print_msg("Pinging $library_url\n");
+    if ($self->ping_library()) { # 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) {
+	    $self->print_msg("Checking if Greenstone server is persistent\n");
+	    $is_persistent_server = $self->is_persistent();
+	    $self->{'is_persistent_server'} = $is_persistent_server;
+	}
+	
+	if ($is_persistent_server) { # persistent server, so can try activating collection
+	    
+	    $self->print_msg("Checking if the collection $qualified_collection is not already active\n");
+	    
+	    # Since we could have deactivated the collection at this point,
+	    # it is likely that it is not yet active. When pinging the collection
+	    # a "ping did not succeed" message is expected, therefore tell the ping
+	    # to proceed silently
+	    my $silent = 1;
+	    my $collection_active = $self->ping_library_collection($silent);
+	    
+	    if (!$collection_active) {
+		$self->print_msg(" Collection is not active.\n");
+		$self->print_msg("Activating collection $qualified_collection\n");
+		$self->activate_collection();
+		
+		# unless an error occurred, the collection should now be active:
+		$collection_active = $self->ping_library_collection(); # not silent if ping did not succeed
+		if(!$collection_active) {
+		    $self->print_msg("ERROR: collection $qualified_collection did not get activated\n");
+		}
+	    }
+	    else {
+		$self->print_msg("Collection is already active => No need to activate\n");
+	    }
+	}
+	else {
+	    $self->print_msg("Server is not persistent => No need to activate collection\n");
+	}
+    }
+    else {
+	$self->print_msg("No response to Ping => Taken to mean server is not running\n");
+    }
+    
+    return $is_persistent_server;
+}
+
+
+#########################################################
+### UNUSED METHODS - CAN BE HANDY
+
+# This method is now unused. Using ping_library instead to send the ping action to a
+# GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
+# http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm
+# and http://www.perlmonks.org/?node_id=618534
+sub is_URL_active {
+    my $url = shift(@_); # gs3 or gs2 URL	
+    
+    my $status = 0;
+    if(defined $url) {
+	$ua->timeout(10); # set LWP useragent to 5s max timeout for testing the URL
+	# Need to set this, else it takes I don't know how long to timeout 
+	# http://www.perlmonks.org/?node_id=618534
+	
+	$status = LWP::Simple::head($url); # returns empty list of headers if it fails
+	# LWP::Simple::get($url) is more intensive, so don't need to do that
+	#print STDERR "**** $url is alive.\n" if $status;
+    }
+    return $status;
+}
+
+# Pinging seems to always return true, so this method doesn't work
+sub pingHost {
+    my $url = shift(@_); # gs3 or gs2 URL
+    
+    my $status = 0;
+    if(defined $url) {
+	# Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
+	# "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too	
+	
+	#my $host = $url;		
+	#$host =~ s@^http:\/\/(www.)?@@;		
+	#$host =~ s@\/.*@@;
+	#print STDERR "**** HOST: $host\n";
+	
+	# More robust way
+	# http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
+	my $uri = URI->new( $url );
+	my $host = $uri->host; 
+	
+	# Ping the host. http://perldoc.perl.org/Net/Ping.html	
+	my $p = Net::Ping->new();		
+	$status = $p->ping($host); # || 0. Appears to set to undef rather than 0
+	print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
+	$p->close();		
+    } 
+    # return whether pinging was a success or failure
+    return $status;
+}
+
+1;
Index: /main/trunk/greenstone2/perllib/buildcolutils.pm
===================================================================
--- /main/trunk/greenstone2/perllib/buildcolutils.pm	(revision 30519)
+++ /main/trunk/greenstone2/perllib/buildcolutils.pm	(revision 30520)
@@ -33,4 +33,5 @@
 use File::Basename;
 
+use activate;
 use colcfg;
 use dbutil;
@@ -685,8 +686,9 @@
 	
 	    if(!$dbSupportsConcurrentRW) {
-		#$self->{'justdeactivate'} = 1;
-		#$self->activate_collection();
-		#$self->{'justdeactivate'} = 0;
-		$self->activate_collection("just_deactivate");		
+		# all except the collection (1st parameter) can be empty. For GS3, also set the site parameter
+		my $gsserver = new activate( $self->get_collection(), $self->{'site'}, $self->{'verbosity'}, $self->{'builddir'}, $self->{'indexdir'}, $self->{'collectdir'}, $self->{'library_url'}, $self->{'library_name'});
+		$gsserver->print_task_msg("About to deactivate collection ".$self->get_collection());
+		#$self->{'library_url'} = $gsserver->get_library_URL();
+		$gsserver->do_deactivate();		
 	    }
 	    $_->make_infodatabase();	    
@@ -718,5 +720,9 @@
 	
 	    if(!$dbSupportsConcurrentRW) {
-		$self->activate_collection("just_deactivate");		
+		# all except the collection (1st parameter) can be empty. For GS3, also set the site parameter
+		my $gsserver = new activate( $self->get_collection(), $self->{'site'}, $self->{'verbosity'}, $self->{'builddir'}, $self->{'indexdir'}, $self->{'collectdir'}, $self->{'library_url'}, $self->{'library_name'});
+		$gsserver->print_task_msg("About to deactivate collection ".$self->get_collection());
+		#$self->{'library_url'} = $gsserver->get_library_URL();
+		$gsserver->do_deactivate();
 	    }
 	    $_->make_infodatabase(); 
@@ -794,15 +800,5 @@
 sub activate_collection
 {
-  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;
-      }
-  }
+  my $self = shift(@_);
 
   # if buildcol.pl was run with -activate, need to run activate.pl
@@ -812,6 +808,9 @@
     #my $quoted_argv = join(" ", map { "\"$_\"" } @ARGV);
     my @activate_argv = ();
+    push(@activate_argv, '-library_url', $self->{'library_url'}) if ($self->{'library_url'});
+    push(@activate_argv, '-library_name', $self->{'library_name'}) if ($self->{'library_name'});
     push(@activate_argv, '-collectdir', $self->{'collectdir'}) if ($self->{'collectdir'});
     push(@activate_argv, '-builddir', $self->{'builddir'}) if ($self->{'builddir'});
+    push(@activate_argv, '-indexdir', $self->{'indexdir'}) if ($self->{'indexdir'});
     push(@activate_argv, '-site', $self->{'site'}) if ($self->{'site'});
     push(@activate_argv, '-verbosity', $self->{'verbosity'}) if ($self->{'verbosity'});
@@ -819,6 +818,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'});
+    push(@activate_argv, '-skipactivation', $self->{'skipactivation'}) if ($self->{'skipactivation'});
+
     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/downloaders/WgetDownload.pm
===================================================================
--- /main/trunk/greenstone2/perllib/downloaders/WgetDownload.pm	(revision 30519)
+++ /main/trunk/greenstone2/perllib/downloaders/WgetDownload.pm	(revision 30520)
@@ -246,5 +246,5 @@
     #print STDOUT "Command is: $command\n";
 
-    # Wget's output needs to be monitored to find out when it has naturally termianted.
+    # Wget's output needs to be monitored to find out when it has naturally terminated.
     # Wget's output is sent to its STDERR so we can't use open2 without doing 2>&1.
     # On linux, 2>&1 launches a subshell which then launches wget, meaning that killing
