Index: gli/trunk/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java
===================================================================
--- gli/trunk/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java	(revision 16333)
+++ gli/trunk/src/org/greenstone/gatherer/remote/RemoteGreenstoneServer.java	(revision 16334)
@@ -166,9 +166,33 @@
 	// returns message "Greenstone version is: <version number of the Greenstone remote server>"
 	String result = performAction(new RemoteGreenstoneServerVersionAction());
-	int index = result.indexOf(":") + 1; // includes space after colon 
-	result = result.substring(index);
-	System.err.println("***** version: " + result);
+	int index = result.indexOf(":"); 
+	if(index != -1) {
+	    result = result.substring(index+1).trim(); // skip the space after colon, must remove surrounding spaces
+	}
 	int greenstoneVersion = Integer.parseInt(result);
 	return greenstoneVersion;
+    }
+
+    /** For constructing the preview command (the library URL) with */
+    static public String getLibraryURL(String serverHomeURL)
+    {
+	// returns message "Greenstone library URL suffix is: <e.g. /greenstone3/library or /gsdl/cgi-bin/library>"
+	String libSuffix = performAction(new RemoteGreenstoneServerLibraryURLSuffixAction());
+	int index = libSuffix.indexOf(":"); 
+	if(index != -1) {
+	    libSuffix = libSuffix.substring(index+1).trim(); // skip the space after colon and remove surrounding spaces
+	}
+	
+	// serverHomeURL is of the form, http://domain/other/stuff. We want the prefix upto & including domain
+	// and prepend that to the libraryURLSuffix
+	index = -1;
+	for(int i = 0; i < 3; i++) {
+	    index = serverHomeURL.indexOf("/", index+1);
+	    if(index == -1) { // shouldn't happen, but if it does, we'll be in an infinite loop
+		break; 
+	    }
+	}
+	serverHomeURL = serverHomeURL.substring(0, index);
+	return serverHomeURL + libSuffix;
     }
 
@@ -273,5 +297,5 @@
 		    try {
 			remote_greenstone_server_action.perform();
-
+			
 			// No exceptions were thrown, so the action was successful
 			remote_greenstone_server_action.processed_successfully = true;
@@ -290,4 +314,16 @@
 			remote_greenstone_server_action.processed_successfully = false;			
 		    }
+		    catch (FileNotFoundException exception) {
+			// FileNotFoundException happens when there's no GS server at the user-provided
+			// url (the address of gliserver.pl is wrong).
+			exit = true;
+			DebugStream.printStackTrace(exception);
+			JOptionPane.showMessageDialog(Gatherer.g_man, 
+						      Dictionary.get("RemoteGreenstoneServer.Error", 
+								     "No gliserver.pl found. " + exception.getMessage()), 
+						      Dictionary.get("RemoteGreenstoneServer.Error_Title"), 
+						      JOptionPane.ERROR_MESSAGE);
+			remote_greenstone_server_action.processed_successfully = false;
+		    }
 		    catch (Exception exception) {
 			DebugStream.printStackTrace(exception);
@@ -568,9 +604,18 @@
 	    throws Exception
 	{
-	    String greenstone_version_command = "cmd=greenstone-server-version";
-	    action_output = sendCommandToServer(greenstone_version_command, null);
+	    action_output = sendCommandToServer("cmd=greenstone-server-version", null);
 	}	
     }
 
+    static private class RemoteGreenstoneServerLibraryURLSuffixAction
+	extends RemoteGreenstoneServerAction
+    {
+	public void perform()
+	    throws Exception
+	{
+	    action_output = sendCommandToServer("cmd=get-library-url-suffix", null);
+	}	
+    }
+    
     /**
      * --------------------------------------------------------------------------------------------
@@ -1071,4 +1116,12 @@
     }
 
+    /** Returns true or false depending on whether authentication is required for the cmd 
+     * string embedded in the given gliserver_args. No authentication is required for either 
+     * of the commands greenstone-server-version and get-library-url-suffix. */
+    static private boolean isAuthenticationRequired(String gliserver_args) {
+	return ((gliserver_args.indexOf("greenstone-server-version") == -1) 
+		&& (gliserver_args.indexOf("get-library-url-suffix") == -1));
+    }
+
 
     /** Returns the command output if the action completed, throws some kind of exception otherwise. */
@@ -1077,4 +1130,5 @@
     {
 	while (true) {
+	    
 	    // Check that Configuration.gliserver_url is set
 	    if (Configuration.gliserver_url == null) {
@@ -1083,9 +1137,10 @@
 
 	    // Ask for authentication information (if necessary), then perform the action
-	    authenticateUser();
+	    if(isAuthenticationRequired(gliserver_args)) {
+		authenticateUser();
+	    }
 	    String gliserver_url_string = Configuration.gliserver_url.toString();
 	    String command_output = sendCommandToServerInternal(gliserver_url_string, gliserver_args, shell);
-	    // System.err.println("Command output: " + command_output);
-
+	    
 	    // Check the first line to see if authentication has failed; if so, go around the loop again
 	    if (command_output.startsWith("ERROR: Authentication failed:")) {
@@ -1112,4 +1167,5 @@
 	    }
 
+	    
 	    // There were no exceptions thrown so the action must have succeeded
 	    return command_output;
@@ -1231,6 +1287,8 @@
 
 	// Add username and password, and a timestamp
-	cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName();
-        cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword());
+	if(isAuthenticationRequired(cgi_args)) {
+	    cgi_args += "&un=" + remote_greenstone_server_authentication.getUserName();
+	    cgi_args += "&pw=" + new String(remote_greenstone_server_authentication.getPassword());
+	}
 	cgi_args += "&ts=" + System.currentTimeMillis();
 	if (Gatherer.GS3){
