Index: /gsdl/trunk/perllib/g2futil.pm
===================================================================
--- /gsdl/trunk/perllib/g2futil.pm	(revision 15655)
+++ /gsdl/trunk/perllib/g2futil.pm	(revision 15656)
@@ -66,5 +66,5 @@
     my $cmd = "$prog $arguments";
 
-    print "*** cmd = $cmd\n";
+###    print "*** cmd = $cmd\n";
 
     if (open(CMD,"$cmd 2>&1 |"))
@@ -242,4 +242,7 @@
     my ($fedora_host, $collect_dir, $options) = @_;
     my $verbosity = $options->{'verbosity'};
+    my $hostname = $options->{'hostname'};
+    my $port     = $options->{'port'};
+    my $protocol = $options->{'protocol'};
 
     print STDERR "Ensuring that a correct gsdl.xml file exists on the Fedora server end\n";
@@ -247,5 +250,5 @@
 
     # 1. Find out which folder to write to: fedora_host or localhost 
-    # whichever contains fedora.xml is the one we want - if none, exit with error value
+    # whichever contains fedora.xml is the one we want (if none, exit with error value?)
     my $fedora_home = $ENV{'FEDORA_HOME'};
     my $base_path = &util::filename_cat($fedora_home, "tomcat", "conf", "Catalina");
@@ -254,5 +257,5 @@
     my $xmlFile = &util::filename_cat($host_path, "fedora.xml");
     if (!-e $xmlFile) {
-	# try seeing if folder localhost contains fedoraXML
+	# check if the folder localhost contains fedoraXML
 	$host_path = &util::filename_cat($base_path, "localhost");
 	$xmlFile = &util::filename_cat($host_path, "fedora.xml");
@@ -265,12 +268,9 @@
     # 2. Construct the string we are going write to the gsdl.xml file
     # a. get the parent directory of collect_dir by removinbg the word 
-    # "collect" from it and any optional OS-type slash at the end.
+    # "collect" from it and any optional OS-type slash at the end. 
+    # (Path slash direction does not matter here.)
     my $collectParentDir = $collect_dir;
     $collectParentDir =~ s/collect(\/|\\)?//;
-    
-    #replace \ in path as occurs on Windows with /?
-    #$collectParentDir =~ s/\\/\//g;
-    #print STDERR "**** collect's parent dir is: $collectParentDir\n";
-
+  
     # b. Use the collectParentDir to create the contents of gsdl.xml
     my $gsdlXMLcontents = "<?xml version='1.0' encoding='utf-8'?>\n<Context docBase=\"";
@@ -330,15 +330,30 @@
    
     
-    # QUESTION:
-    # Starting up the Fedora server takes a long time. How long should we wait before 
-    # import continues? g2f-import relies on an up-and-running Fedora server to purge the 
-    # collection from it whereas g2f-build.pl needs a ready Fedora server in order to make 
-    # it ingest the FedoraMETS.
-    # Let's try waiting 10s for the Fedora server to really be up and running after the 
-    # restart so import and build can work without glitches. But how can we check if this 
-    # duration is actually sufficient?
-    print STDERR "Fedora server restarted. Waiting 10 seconds to ensure the server is ready...\n";
-    sleep 10;
-
+    # Starting up the Fedora server takes a long time. We need to wait for the server to be
+    # ready before import can continue, because g2f-import relies on an up-and-running Fedora
+    # server to purge the collection from it while g2f-build.pl needs a ready Fedora server 
+    # in order to make it ingest the FedoraMETS. Sleeping is not sufficient (#sleep 10;) since
+    # the subsequent steps depend on a proper server restart.
+    # Dr Bainbridge's suggestion: test the server is ready with a call to wget.
+    
+    # Wget tries to retrieve the fedora search page (protocol://host:port/fedora/search)
+    # 20 times, waiting 3 seconds between each failed attempt. If it ultimately fails, we
+    # print a message to the user.
+    # The wget --spider option makes it check that the page is merely there rather than 
+    # downloading it (see http://www.gnu.org/software/wget/manual/wget.html#Download-Options)
+    # -q is for quiet, --tries for the number of retries, --waitretry is the number of seconds
+    # between each attempt. Usually wget returns the contents of the page, but in our case it
+    # will return 0 for success since we are not downloading.
+
+    print STDERR "Fedora server restarted. Waiting for it to become ready...\n";
+    
+    # print STDERR "****$protocol://$hostname:$port/fedora/search\n";
+    my $fedoraServerReady = system("wget -q --spider --waitretry=3 --tries=20 $protocol://$hostname:$port/fedora/search"); 
+    if($fedoraServerReady != 0) {
+	print STDERR "Fedora server is still not ready... ERROR: $!\n";
+	print "Exit status = ", $status/256, "\n";
+	die "Exiting....\n";
+    }
+    
     # return some indication that things went well
     return "gsdl.xml";
