Index: /gsdl/trunk/perllib/g2futil.pm
===================================================================
--- /gsdl/trunk/perllib/g2futil.pm	(revision 15394)
+++ /gsdl/trunk/perllib/g2futil.pm	(revision 15395)
@@ -226,4 +226,104 @@
 
 
+# Subroutine to write the gsdl.xml file in FEDORA_HOME/tomcat/conf/Catalina/<host/localhost>/
+# This xml file will tell Fedora where to find the parent folder of the GS collect dir
+# so that it can obtain the FedoraMETS files for ingestion. 
+# It depends on the Fedora server being on the same machine as the Greenstone server that 
+# this code is part of.
+sub write_gsdl_xml_file
+{
+    my ($fedora_host, $collect_dir) = @_;
+    
+    print STDERR "Ensuring that a correct gsdl.xml file exists on the Fedora server end\n";
+    # The top of this file has already made sure that FEDORA_HOME is set
+
+    # 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
+    my $fedora_home = $ENV{'FEDORA_HOME'};
+    my $base_path = &util::filename_cat($fedora_home, "tomcat", "conf", "Catalina");
+
+    my $host_path = &util::filename_cat($base_path, $fedora_host);
+    my $xmlFile = &util::filename_cat($host_path, "fedora.xml");
+    if (!-e $xmlFile) {
+	# try seeing if folder localhost contains fedoraXML
+	$host_path = &util::filename_cat($base_path, "localhost");
+	$xmlFile = &util::filename_cat($host_path, "fedora.xml");
+	if(!-e $xmlFile) {
+	    # try putting gsdl in this folder, but still print a warning
+	    print STDERR "**** $host_path does not contain file fedora.xml. Hoping gsdl.xml belongs there anyway\n";
+	}
+    }
+
+    # 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.
+    my $collectParentDir = $collect_dir;
+    $collectParentDir =~ s/collect(\/|\\)?//;
+    #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=\"";
+    $gsdlXMLcontents = $gsdlXMLcontents.$collectParentDir."\" path=\"/gsdl\"></Context>";
+    
+    # 3. If there is already a gsdl.xml file in host_path, compare the string we
+    # want to write with what is already in there. If they're the same, we can return
+    $xmlFile = &util::filename_cat($host_path, "gsdl.xml");
+    if(-e $xmlFile) {
+	# such a file exists, so read the contents
+	unless(open(FIN, "<$xmlFile")) { 
+	    print STDERR "***g2f-import.pl: Unable to open existing $xmlFile for comparing...Recoverable. $!\n";
+	    # doesn't matter, we'll just overwrite it then
+	}    
+	my $xml_contents;
+	{
+	    local $/ = undef;        # Read entire file at once
+	    $xml_contents = <FIN>;   # Now file is read in as one single 'line'
+	}
+	close(FIN); # close the file
+	if($xml_contents eq $gsdlXMLcontents) { 
+	    print STDERR "The old gsdl.xml file already contains the same.\n";
+	    # it already contains what we want, we're done
+	    return "gsdl.xml";
+	}
+    }
+
+    # 4. If we're here, the contents of gsdl.xml need to be updated:
+    # a. First stop the fedora server
+    my $stop_tomcat = &util::filename_cat($fedora_home, "tomcat", "bin", "shutdown.sh");
+    # execute the command 
+    $!=0; # does this initialise the return value?
+    if (system($stop_tomcat)!=0) { # to get the actual exit value, divide by 256, but not useful here
+	# possible tomcat was already stopped - it's not the end of the world
+	print STDERR "**** Failed to stop Fedora server. Perhaps it was not running. $!\n";
+    }
+
+    # b. overwrite the file that has outdated contents with the contents we just constructed
+    unless(open(FOUT, ">$xmlFile")) {  # create or overwrite gsdl.xml file
+	die "g2f-import.pl: Unable to open $xmlFile for telling Fedora where the collect dir is...ERROR: $!\n";
+    }
+    # write out the updated contents and close the file
+    print FOUT $gsdlXMLcontents;
+    close(FOUT);
+
+    # c. Restart the fedora server
+    my $start_tomcat = &util::filename_cat($fedora_home, "tomcat", "bin", "startup.sh");
+    $!=0;
+    if (system($start_tomcat)!=0) { 
+	print STDERR "Failed to restart the Fedora server... ERROR: $!\n";
+    }
+    # 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;
+
+    # return some indication that things went well
+    return "gsdl.xml";
+}
 
 
