Index: /gs3-extensions/solr/trunk/src/perllib/solrserver.pm
===================================================================
--- /gs3-extensions/solr/trunk/src/perllib/solrserver.pm	(revision 25887)
+++ /gs3-extensions/solr/trunk/src/perllib/solrserver.pm	(revision 25888)
@@ -204,5 +204,6 @@
 }
 
-
+# Some of the Solr CoreAdmin API calls available. 
+# See http://wiki.apache.org/solr/CoreAdmin
 sub admin_reload_core
 {
@@ -215,9 +216,40 @@
 }
 
+sub admin_rename_core
+{
+    my $self = shift @_;
+    my ($oldcore, $newcore) = @_;
+
+    my $cgi_get_args = "action=RENAME&core=$oldcore&other=$newcore";
+
+    $self->_admin_service($cgi_get_args);
+}
+
+sub admin_swap_core
+{
+    my $self = shift @_;
+    my ($oldcore, $newcore) = @_;
+
+    my $cgi_get_args = "action=SWAP&core=$oldcore&other=$newcore";
+
+    $self->_admin_service($cgi_get_args);
+}
+
+# The ALIAS action is not supported in our version of solr (despite it
+# being marked as experimental in the documentation for Core Admin)
+sub admin_alias_core
+{
+    my $self = shift @_;
+    my ($oldcore, $newcore) = @_;
+
+    my $cgi_get_args = "action=ALIAS&core=$oldcore&other=$newcore";
+
+    $self->_admin_service($cgi_get_args);
+}
 
 sub admin_create_core
 {
     my $self = shift @_;
-    my ($core,$removeold) = @_;
+    my ($core, $data_parent_dir) = @_; # data_parent_dir is optional, can be index_dir. Defaults to builddir if not provided
 
     my ($ds_idx) = ($core =~ m/^.*-(.*?)$/);
@@ -227,7 +259,10 @@
     my $collect_home = $ENV{'GSDLCOLLECTDIR'};
     my $etc_dirname = &util::filename_cat($collect_home,"etc");
-	    
-    my $build_dir = $self->{'build_dir'};
-    my $idx_dirname = &util::filename_cat($build_dir,$ds_idx);
+
+    if(!defined $data_parent_dir) {
+	$data_parent_dir = $self->{'build_dir'};
+    } 
+    
+    my $idx_dirname = &util::filename_cat($data_parent_dir,$ds_idx); # "dataDir"  
 	    
     $cgi_get_args .= "&instanceDir=$etc_dirname";
@@ -237,5 +272,26 @@
 }
 
-
+# removes (unloads) core from th eext/solr/sorl.xml config file
+sub admin_unload_core
+{
+    my $self = shift @_;
+    my ($core) = @_;
+
+    my $cgi_get_args = "action=UNLOAD&core=$core"; # &deleteIndex=true from Solr3.3
+
+    $self->_admin_service($cgi_get_args);
+}
+
+sub copy_solrxml_to_web
+{
+    my $self = shift @_;
+
+    my $ext_solrxml = &util::filename_cat($ENV{'GEXT_SOLR'}, "solr.xml");
+    my $web_solrxml = &util::filename_cat($ENV{'GSDL3HOME'}, "ext", "solr", "solr.xml");
+
+    #print STDERR "@@@@ Copying $ext_solrxml to $web_solrxml...\n";
+
+    &util::cp($ext_solrxml, $web_solrxml);
+}
 
 sub start
Index: /main/trunk/greenstone2/bin/script/activate.pl
===================================================================
--- /main/trunk/greenstone2/bin/script/activate.pl	(revision 25887)
+++ /main/trunk/greenstone2/bin/script/activate.pl	(revision 25888)
@@ -520,4 +520,33 @@
 	$buildtype = "mg"; #mg is the default
     }
+
+    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() 
@@ -572,4 +601,11 @@
 	
 	if($removeold) {
+
+	    if ($buildtype eq "solr") {
+		# if solr, remove any cores using the index_dir before deleting this dir
+		foreach my $corename (@corenames) {
+		    $solr_server->admin_unload_core($corename);
+		}
+	    }	
 	    
 		if(&util::dir_exists($index_dir)) {
@@ -596,4 +632,11 @@
 	}
 	elsif ($keepold) {
+	    	if ($buildtype eq "solr") { 
+		    # if solr, remove any cores using the index_dir before deleting this dir
+		    foreach my $corename (@corenames) {			
+			$solr_server->admin_unload_core($corename);
+		    }
+		}
+	    
 		# Copy just the contents of building dir into the index dir, overwriting 
 		# existing files, but don't replace index with building.
@@ -602,4 +645,14 @@
 	}
 	
+    if ($buildtype eq "solr") {
+	# Call CREATE action to get the old cores pointing to the index folder
+	foreach my $corename (@corenames) {
+	    $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	
 	
@@ -643,4 +696,10 @@
 
     &print_msg("\n");
+
+    if($buildtype eq "solr") {
+	if ($solr_server->explicitly_started()) {
+	    $solr_server->stop();
+	}
+    }
 }
 
