Index: /main/trunk/greenstone3/gs3-setup.sh
===================================================================
--- /main/trunk/greenstone3/gs3-setup.sh	(revision 31480)
+++ /main/trunk/greenstone3/gs3-setup.sh	(revision 31481)
@@ -9,5 +9,5 @@
 # java_min_version gets passed to search4j as the minimum java version
 java_min_version=1.5.0_00
-
+DEBUG=false
 
 function testSource(){
@@ -180,5 +180,6 @@
 
   # we now include a JRE with Mac (Mountain) Lion too, because from Yosemite onwards there's no system Java on Macs
-  HINT="`pwd`/packages/jre"
+  BUNDLED_JRE="`pwd`/packages/jre"
+  HINT=$BUNDLED_JRE
   
   #if [ "$GSDLOS" = "darwin" ] && [ ! -d "$HINT" ]; then
@@ -195,5 +196,5 @@
   fi
 
-  echo "**********************************************"
+  if [ "$DEBUG" == "true" ]; then echo "**********************************************"; fi
 
   # If the file utility exists, use it to determine the bitness of this GS3,
@@ -216,6 +217,11 @@
   #    can also use double brackets:  if [[ $? > 1 ]]; then ...
   if [ "$?" -gt "1" ]; then
-      echo "*** 'file' utility not found installed on this unix-based system."
-      echo "*** Unable to use 'file' utility to determine bitness of this GS3 to see if it matches that of any Java found."
+      if [ "$DEBUG" == "true" ]; then 
+	  echo "    'file' utility not found installed on this unix-based system."
+	  echo "    Unable to use 'file' utility to determine bitness of this GS3 to see if it matches that of any Java found."
+      fi
+      bitness=-1
+  elif [ ! -f "$GSDL3SRCHOME/lib/jni/libgdbmjava.so" ]; then
+      # the file we want to test the bitness of, to determine GS3's bitness by, doesn't exist yet
       bitness=-1
   else
@@ -239,45 +245,96 @@
   # we'll print a warning about this bitness mismatch at the end
 
+  javaset=false
   if [ -x bin/search4j ] ; then
-    FOUNDJAVAHOME="`bin/search4j -p \"$HINT\" -m $java_min_version`"
-    if [ "$?" == "0" ]; then
+      FOUNDJAVAHOME="`bin/search4j -d -p \"$HINT\" -m $java_min_version`"
+      javahome_retval=$?
+      FOUNDJREHOME="`bin/search4j -r -p \"$HINT\" -m $java_min_version`"
+      jrehome_retval=$?
+  fi
+
+  # 1. check the bitness of any JDK java found by search4j, and use if appropriate
+  if [ "$javahome_retval" == "0" ]; then
       checkJavaBitnessAgainstGSBitness "$FOUNDJAVAHOME" "$bitness"
 
       if [ "$?" == "0" ]; then
           #found a suitable java
-	  if [ "$bitness" != "-1" ]; then
-	      echo "*** The detected java is a matching $bitness bit"
+	  if [ "$bitness" != "-1" ] && [ "$DEBUG" == "true" ]; then
+	      echo "    The detected JDK at $FOUNDJAVAHOME is a matching $bitness bit"
 	  fi
-	  setupJavaAt "$FOUNDJAVAHOME"
-      else
-	  if [ "$bitness" != "-1" ]; then
-	      echo "*** The detected java is an incompatible bit architecture"
+	  setupJavaAt "$FOUNDJAVAHOME" "JDK"
+	  javaset=true
+
+      elif [ "$bitness" != "-1" ] && [ "$DEBUG" == "true" ]; then	  
+	  echo "    The detected JDK java at $FOUNDJAVAHOME is an incompatible bit architecture"
+      fi
+  fi
+
+  # 2. check bitness of any JRE java found by search4j, and use if appropriate
+  # http://tldp.org/LDP/abs/html/comparison-ops.html
+  if [ "$javaset" != "true" ] && [ "$jrehome_retval" == "0" ]; then      
+      checkJavaBitnessAgainstGSBitness "$FOUNDJREHOME" "$bitness"
+      
+      if [ "$?" == "0" ]; then
+          #found a suitable jre
+	  if [ "$bitness" != "-1" ] && [ "$DEBUG" == "true" ]; then
+	      echo "    The detected JRE at $FOUNDJREHOME is a matching $bitness bit"
 	  fi
-
-	  # check any bundled JRE
-	  if [ -d "$HINT" ]; then	  
-	      # For linux, the bundled JRE will be of a bitness matching this OS.
+	  setupJavaAt "$FOUNDJREHOME" "JRE"
+	  javaset=true
+	  
+      elif [ "$bitness" != "-1" ] && [ "$DEBUG" == "true" ]; then
+	  echo "    The detected JRE java at $FOUNDJREHOME is an incompatible bit architecture"
+      fi      
+  fi
+
+  # 3. check the bitness of any bundled JRE, and use if appropriate
+  if [ "$javaset" != "true" ] && [ -d "$BUNDLED_JRE" ]; then
+      checkJavaBitnessAgainstGSBitness "$FOUNDJREHOME" "$bitness"
+      
+      # For linux, the bundled JRE ought to be of a bitness matching this OS.
+      if [ "$?" == "0" ]; then
+	  if [ "$bitness" != "-1" ] && [ "$DEBUG" == "true" ]; then
+	      # bundled JRE matches GS bitness
 	      echo "*** Changing to use Greenstone's bundled $bitness-bit jre"
-	      setupJavaAt "$HINT" "JRE"
-	  else
-	      # go with the JAVA_HOME that search4j found
-	      echo "*** Using the Java detected: "
-	      setupJavaAt "$FOUNDJAVAHOME"
 	  fi
-      fi
-    else
-      #no suitable java exists
-      echo "  - ERROR: Failed to locate java $java_min_version or greater"
+	  setupJavaAt "$BUNDLED_JRE" "JRE"
+	  javaset=true
+
+      elif [ "$bitness" != "-1" ] && [ "$DEBUG" == "true" ]; then
+	  echo "    The bundled JRE java is an incompatible bit architecture"
+      fi
+  fi
+
+  # 4. None of the java found so far, if any (via search4j, bundled_jre), may have matched bitness wise
+  # So, fall back to using whichever is available in sequence anyway.
+  # We'll print a warning of bitness mismatch later
+
+  if [ "$javaset" != "true" ]; then
+      # go with any JAVA_HOME else JRE_HOME that search4j found, else with any bundled JRE if present
+      if [ "$javahome_retval" == "0" ]; then
+	  setupJavaAt "$FOUNDJAVAHOME" "JDK"
+	  javaset=true
+      elif [ "$jrehome_retval" == "0" ]; then
+	  setupJavaAt "$FOUNDJREHOME" "JRE"
+	  javaset=true
+      elif [ -d "$BUNDLED_JRE" ]; then
+	  # bundled JRE should be >= than minimum version of java required
+	  setupJavaAt "$BUNDLED_JRE" "JRE"
+	  javaset=true
+      fi
+  fi
+
+  # 5. lastly, check if java already setup
+  if [ "$javaset" != "true" ]; then
+
+    if [ -x bin/search4j ]; then
+
+      # no suitable java could be found by search4j
+      echo "  - ERROR: Failed to locate java $java_min_version or greater"	  
       echo "           Please set JAVA_HOME or JRE_HOME to point to an appropriate java"
       echo "           And add JAVA_HOME/bin or JRE_HOME/bin to your PATH"
-    fi
-
-  #otherwise no search4j, manually try the hint (the bundled jre) if it exists
-  elif [ -d "$HINT" ]; then
-      #found a suitable java
-      setupJavaAt "$HINT" "JRE"
-
-  #lastly, check if java already setup
-  else
+
+    else
+      # search4j wasn't present, and no bundled JRE, so check JAVA_HOME or JRE_HOME manually
       echo "*** Could not find an appropriate JDK or JRE java"
       echo "*** Attempting to use JAVA_HOME else JRE_HOME in the environment"
@@ -302,4 +359,5 @@
 	  return
       fi
+    fi
   fi
 
@@ -329,9 +387,11 @@
   fi
 
-  echo "**********************************************"
-}
-
+  if [ "$DEBUG" == "true" ]; then echo "**********************************************"; fi
+
+}
+
+# if bitness (parameter #2) is -1, then this function returns 0 (generally meaning success).
 function checkJavaBitnessAgainstGSBitness() {
-#    echo "Checking bitness"
+#    if [ "$DEBUG" == "true" ]; then echo "Testing bitness of java found at $java_installation"; fi
     java_installation="$1"
     bitness="$2"
@@ -360,7 +420,23 @@
 
 function setupJavaAt() {
-  export JAVA_HOME="$1"
-  addtopath PATH "$JAVA_HOME/bin"
-  echo "  - Exported JAVA_HOME to $JAVA_HOME"
+
+  # check the second parameter if non-null
+  if [ -n "$2" ] && [ "$2" == "JRE" ]; then
+    export JRE_HOME="$1"
+    addtopath PATH "$JAVA_HOME/bin"
+
+    BUNDLED_JRE="`pwd`/packages/jre"    
+    if [[ "$JRE_HOME" == *"$BUNDLED_JRE"* ]]; then
+	msg="the bundled"
+    fi
+
+    echo "  - Exported JRE_HOME to $msg $JRE_HOME"
+  else
+    export JAVA_HOME="$1"
+    addtopath PATH "$JAVA_HOME/bin"
+    echo "  - Exported JAVA_HOME to $JAVA_HOME"
+  fi
+
+  
 }
 
