Index: /main/trunk/greenstone2/perllib/util.pm
===================================================================
--- /main/trunk/greenstone2/perllib/util.pm	(revision 28459)
+++ /main/trunk/greenstone2/perllib/util.pm	(revision 28460)
@@ -316,4 +316,13 @@
 
 
+sub get_collectlevel_tmp_dir
+{
+    my $tmp_dirname = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, "tmp");
+    &FileUtils::makeDirectory($tmp_dirname) if (!-e $tmp_dirname);
+
+    return $tmp_dirname;
+}
+
+
 sub filename_to_regex {
     my $filename = shift (@_);
@@ -373,4 +382,10 @@
     if ($join_char eq ";") {
 	$pathname =~ s/[\\\/]+/\\/g;
+	if ($^O eq "cygwin") {
+	    # Once we've collapsed muliple (potentialy > 2) slashes
+	    # For cygwin, actually want things double-backslahed 
+	    $pathname =~ s/\\/\\\\/g;
+	}
+
     } else {
 	$pathname =~ s/[\/]+/\//g; 
@@ -412,4 +427,21 @@
 
 
+sub makeFilenameJavaCygwinCompatible
+{
+    my ($java_filename) = @_;
+
+    if ($^O eq "cygwin") {
+	# To be used with a Java program, but under Cygwin
+	# Because the java binary that is native to Windows, need to
+	# convert the Cygwin paths (i.e. Unix style) to be Windows
+	# compatible
+	
+	$java_filename = `cygpath -wp "$java_filename"`;
+	chomp($java_filename);
+	$java_filename =~ s%\\%\\\\%g;
+    }
+
+    return $java_filename;
+}
 
 sub tidy_up_oid {
@@ -1125,4 +1157,130 @@
 
 
+
+#
+# The following comaes from activate.pl
+#
+# Designed to work 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.
+
+sub get_full_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 $gs_mode = ($ENV{'GSDL3SRCHOME'}) ? "gs3" : "gs2";
+	
+    my $url = undef;	
+    
+    if($gs_mode eq "gs2") {		
+	my $llssite_cfg = &FileUtils::filenameConcatenate($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 STDERR "util::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 $full_build_xml = &FileUtils::javaFilenameConcatenate($ENV{'GSDL3SRCHOME'},"build.xml");
+
+	my $perl_command = "ant -buildfile \"$full_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 STDERR "util::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;
+
+    $ENV{'FULL_GREENSTONE_URL_PREFIX'} = $url;
+
+    return $url;
+}
+
+
 # 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
