#!/usr/bin/perl -w

###########################################################################
#
# activate.pl -- to be called after building a collection to activate it.
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2009 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.
#
###########################################################################


# This program is designed to support the building process of Greenstone.
# It deactivates the collection just built, if the web server is running
# and is a persistent web server (or if the library_URL provided as 
# parameter to this script is of a currently running web server). It then 
# moves building to index, before activating the collection on the GS2 or 
# GS3 web server again if necessary.


BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}


use strict;
no strict 'refs'; # allow filehandles to be variables and vice versa
no strict 'subs'; # allow barewords (eg STDERR) as function arguments

use File::Basename;
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;

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) = @_; 
			# 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;		
			if($response_content =~ m/$check_message_against_regex/) {
				&print_msg(" Response as expected.\n", 3);
				return 1;
			} else {
				&print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
				&print_msg("*** Got message:\n$response_content.\n", 4);
				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
		}
	}	
}

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) = @_;

	# 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;
	return config($library_url, $command, $check_responsemsg_against_regex, $site, $expected_error_code);
}

# 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) = @_;
	
	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);
}

# 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 = shift(@_); # gs3 or gs2
	
	# 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 = &util::filename_cat($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");
		}		
	}
	
	# 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(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
		
		$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;
}


# 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 -removeold -keepold -verbosity v\n";
		print STDERR "\t-library_url URL] <[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;
	
	my $removeold = 0;
	my $keepold = 0;
	my $incremental = 0; # used by solr

	my $library_url = undef; # to be specified on the cmdline if not using a GS-included web server
	
    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 "-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 "-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 = &util::filename_cat($collection_dir, "building") unless (defined $build_dir);
	$index_dir = &util::filename_cat($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 = &util::filename_cat($build_dir,"build.cfg");
    } else {
		$build_cfg_filename = &util::filename_cat($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(!&util::dir_exists($build_dir)) {
	&print_msg("No building folder at $build_dir to move to index.\n");
	exit -1 unless ($buildtype eq "solr"); #&& $incremental);
    } elsif (&util::is_dir_empty($build_dir)) {
	&print_msg("Nothing in building folder $build_dir to move into index folder.\n");
	exit -1 unless ($buildtype eq "solr"); #&& $incremental);
    }
    

    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
    }
    
	# 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); # 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) { # 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) { # 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);
			    }
			}	
		}
	}
	
	# 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(&util::dir_exists($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);
			    }
			}	

			&util::rm_r($index_dir);
			
			# Wait for a couple of seconds, just for luck
			sleep 2;
			
			if (&util::dir_exists($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\"");
		&util::mv($build_dir, $index_dir);
		if(&util::dir_exists($build_dir) || !&util::dir_exists($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");
		    }
		}
	    
		# 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\"");
		&util::mv_dir_contents($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);
		}
	    }
	}

	# copy the just updated ext/solr/solr.xml to web/ext/solr/solr.xml
	$solr_server->copy_solrxml_to_web();
    }

	# 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 valid server URL
		
			&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");
				    my $collection_active = &ping_library_collection($library_url, $gs_mode, $qualified_collection, $site);

				    if(!$collection_active) {
					&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);
					if(!$collection_active) {
					    &print_msg("ERROR: collection $qualified_collection did not get activated\n");
					}
				    }
				}
			}
		}
	} 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($buildtype eq "solr") {
	if ($solr_server->explicitly_started()) {
	    $solr_server->stop();
	}
    }
}

&main(scalar(@ARGV),@ARGV);
