Index: main/trunk/greenstone2/perllib/g2futil.pm
===================================================================
--- main/trunk/greenstone2/perllib/g2futil.pm	(revision 21718)
+++ main/trunk/greenstone2/perllib/g2futil.pm	(revision 21719)
@@ -320,6 +320,7 @@
   
     # b. Use the collectParentDir to create the contents of gsdl.xml
+    my $greenstone_url_prefix = &util::get_greenstone_url_prefix(); # would have the required slash at front
     my $gsdlXMLcontents = "<?xml version='1.0' encoding='utf-8'?>\n<Context docBase=\"";
-    $gsdlXMLcontents = $gsdlXMLcontents.$collectParentDir."\" path=\"/gsdl\"></Context>";
+    $gsdlXMLcontents = $gsdlXMLcontents.$collectParentDir."\" path=\"$greenstone_url_prefix\"></Context>";
     
     # 3. If there is already a gsdl.xml file in host_path, compare the string we
Index: main/trunk/greenstone2/perllib/plugouts/FedoraMETSPlugout.pm
===================================================================
--- main/trunk/greenstone2/perllib/plugouts/FedoraMETSPlugout.pm	(revision 21718)
+++ main/trunk/greenstone2/perllib/plugouts/FedoraMETSPlugout.pm	(revision 21719)
@@ -68,5 +68,5 @@
     
     my $self = new METSPlugout($plugoutlist,$inputargs,$hashArgOptLists);
-    
+
     return bless $self, $class;
 }
@@ -544,8 +544,11 @@
 	$gsdl_href =~ s/\\/\//g;                   # make sure we have url paths (which only use / not \)
 	my $localfedora = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "fedora.xml");
-	if($ENV{'GSDL3SRCHOME'} && -e $localfedora) {                   # prepend gsdl
-	    $gsdl_href = "/greenstone3/sites/localsite/$gsdl_href";     # Fedora uses Greenstone's tomcat
+	
+	my $greenstone_url_prefix = &util::get_greenstone_url_prefix();
+	# prepend url_prefix (which will contain the forward slash upfront)
+	if($ENV{'GSDL3SRCHOME'} && -e $localfedora) {               # Fedora uses Greenstone's tomcat.
+	    $gsdl_href = "$greenstone_url_prefix/sites/localsite/$gsdl_href";     # Default: /greenstone3/sites/localsite/$gsdl_href
 	} else {
-	    $gsdl_href = "/gsdl/$gsdl_href";
+	    $gsdl_href = "$greenstone_url_prefix/$gsdl_href"; 	    # By default: "/greenstone/$gsdl_href";
 	}
 
@@ -680,8 +683,11 @@
 	       $gsdl_href =~ s/\\/\//g;                   # make sure we have url paths (which only use / not \)
 	       my $localfedora = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "fedora.xml");
-	       if($ENV{'GSDL3SRCHOME'} && -e $localfedora) {                   # prepend gsdl
-		   $gsdl_href = "/greenstone3/sites/localsite/$gsdl_href";     # Fedora uses Greenstone's tomcat
+
+	       my $greenstone_url_prefix = &util::get_greenstone_url_prefix();
+	       # prepend url_prefix (which will contain the forward slash upfront)
+	       if($ENV{'GSDL3SRCHOME'} && -e $localfedora) {                 # Fedora uses Greenstone's tomcat. 
+		   $gsdl_href = "$greenstone_url_prefix/sites/localsite/$gsdl_href";    # Default: /greenstone3/sites/localsite/$gsdl_href
 	       } else {
-		   $gsdl_href = "/gsdl/$gsdl_href";
+		   $gsdl_href = "$greenstone_url_prefix/$gsdl_href";         # By default: "/greenstone/$gsdl_href";
 	       }
 
Index: main/trunk/greenstone2/perllib/util.pm
===================================================================
--- main/trunk/greenstone2/perllib/util.pm	(revision 21718)
+++ main/trunk/greenstone2/perllib/util.pm	(revision 21719)
@@ -1142,4 +1142,78 @@
 
 
+# Returns the greenstone URL prefix extracted from the appropriate GS2/GS3 config file. 
+# By default, /greenstone3 for GS3 or /greenstone for GS2.
+sub get_greenstone_url_prefix() {
+    # if already set on a previous occasion, just return that
+    # (Don't want to keep repeating this: cost of re-opening and scanning files.)
+    return $ENV{'GREENSTONE_URL_PREFIX'} if($ENV{'GREENSTONE_URL_PREFIX'});
+
+    my ($configfile, $urlprefix, $defaultUrlprefix); 
+    my @propertynames = ();
+
+    if($ENV{'GSDL3SRCHOME'}) {
+	$defaultUrlprefix = "/greenstone3";
+	$configfile = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "greenstone3.xml");
+	push(@propertynames, qw/path\s*\=/);
+    } else {
+	$defaultUrlprefix = "/greenstone";
+	$configfile = &util::filename_cat($ENV{'GSDLHOME'}, "cgi-bin", "gsdlsite.cfg");
+	push(@propertynames, (qw/\nhttpprefix/, qw/\ngwcgi/)); # inspect one property then the other 
+    }
+
+    $urlprefix = &extract_propvalue_from_file($configfile, \@propertynames);
+
+    if(!$urlprefix) { # no values found for URL prefix, use default values
+	$urlprefix = $defaultUrlprefix;
+    } else {
+	#gwcgi can contain more than the wanted prefix, we split on / to get the first "directory" level
+	$urlprefix =~ s/^\///; # remove the starting slash
+	my @dirs = split(/(\\|\/)/, $urlprefix); 
+	$urlprefix = shift(@dirs);
+
+	if($urlprefix !~ m/^\//) { # in all cases: ensure the required forward slash is at the front
+	    $urlprefix = "/$urlprefix";
+	}
+    }
+
+    # set for the future
+    $ENV{'GREENSTONE_URL_PREFIX'} = $urlprefix;
+#    print STDERR "*** in get_greenstone_url_prefix(): $urlprefix\n\n";
+    return $urlprefix;
+}
+
+
+# Given a config file (xml or java properties file) and a list/array of regular expressions
+# that represent property names to match on, this function will return the value for the 1st
+# matching property name. If the return value is undefined, no matching property was found.
+sub extract_propvalue_from_file() {
+    my ($configfile, $propertynames) = @_;
+
+    my $value;
+    unless(open(FIN, "<$configfile")) { 
+	print STDERR "extract_propvalue_from_file(): Unable to open $configfile. $!\n";
+	return $value; # not initialised
+    }
+
+    # Read the entire file at once, as one single line, then close it
+    my $filecontents;
+    {
+	local $/ = undef;        
+	$filecontents = <FIN>;
+    }
+    close(FIN);
+
+    foreach my $regex (@$propertynames) {
+        ($value) = $filecontents=~ m/$regex\s*(\S*)/s; # read value of the property given by regex up to the 1st space
+	if($value) { 
+            $value =~ s/^\"//;     # remove any startquotes
+	    $value =~ s/\".*$//;   # remove the 1st endquotes (if any) followed by any xml
+	    last;		       # found value for a matching property, break from loop
+	}
+    }
+
+    return $value;
+}
+
 
 1;
