Index: /greenstone3/trunk/gs3-setup.bat
===================================================================
--- /greenstone3/trunk/gs3-setup.bat	(revision 14725)
+++ /greenstone3/trunk/gs3-setup.bat	(revision 14726)
@@ -1,3 +1,8 @@
 @echo off
+
+::get user defined environment variable
+set JRE_HOME=
+set JAVA_HOME=
+set CLASSPATH=
 
 if exist gs3-setup.bat goto start
@@ -33,62 +38,125 @@
 del setcp.bat
 
-echo CLASSPATH: %CLASSPATH%
+goto findJREHOME
 
-::cd %GSDL3HOME%
+:: ---- Check JRE_HOME ----
+:findJREHOME
+if not "%JRE_HOME%" == "" goto testJREHOME 
+   goto findJREHOME2 	 
 
-:findJava
-:: ---- Check Java SDK exists ----
-set JAVAPATH=
+::---- try JREHOME ----
+:findJREHOME2 
+if "%JREHOME%" == "" goto findJAVAHOME  
+   set JRE_HOME=%JREHOME%
+   testJREHOME
 
-:: Some users may set the above line manually
-if not "%JAVAPATH%" == "" goto testJava
-   
-:: If it is set, use the JAVA_HOME environment variable
-    if not "%JAVA_HOME%" == "" goto javahome
+::test JRE_HOME is set correctly
+:testJREHOME 
+if exist "%JRE_HOME%\bin\java.exe" goto gotJREHOME
+   echo JRE_HOME: "%JRE_HOME%" 	
+   echo Couldn't find "%JRE_HOME%\bin\java.exe"	
+   echo The JRE_HOME environment variable is not defined correctly
+   echo This environment variable is needed to run this program
+   goto pauseANdExit  	
 
-    :: Check if Java is on the search path
-    echo %PATH%| winutil\which.exe java.exe | winutil\setvar.exe JAVAPATH > setjava.bat
-    call setjava.bat
-    del setjava.bat
-    echo %JAVAPATH%
-    if not "%JAVAPATH%" == "" goto testJava
 
-    :: Still haven't found anything, so try looking in the registry (gulp!)
-    type nul > jdk.reg
-    regedit /E jdk.reg "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
-    type jdk.reg > jdk.txt
-    del jdk.reg
-    type nul > jre.reg
-    regedit /E jre.reg "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment"
-    type jre.reg > jre.txt
-    del jre.reg
+::set the RUNJAVA and exit
+:gotJREHOME
+ set RUNJAVA=%JRE_HOME%\bin\java.exe
+ set PATH=%JRE_HOME%\bin;%PATH%
+ echo JRE_HOME: "%JRE_HOME%"
+ echo Your environment has successfully been set up to run Greenstone3
+ goto exit
 
-    winutil\findjava.exe jdk.txt jre.txt | winutil\setvar.exe JAVAPATH > setjava.bat
-    del jdk.txt
-    del jre.txt
-    call setjava.bat
-    del setjava.bat
 
-    :: If nothing was found in the registry, we're stuck
-    if "%JAVAPATH%" == "" goto noJava
+:: ---- check JAVA_HOME ----
+:findJAVAHOME
+if not "%JAVA_HOME%" == "" goto testJAVAHOME
+   goto findJAVAHOME2
 
-    set JAVAPATH=%JAVAPATH%\bin
-    goto testJava
+ 
+::---- check JAVAHOME ----
+:findJAVAHOME2 
+if "%JAVAHOME%" == "" goto noJAVAHOME  
+   set JAVA_HOME=%JAVAHOME%
+   testJAVAHOME
 
-:javahome
-    set JAVAPATH=%JAVA_HOME%\bin
 
-:testJava
-if exist "%JAVAPATH%\java.exe" goto  exit
+:: test JAVA_HOME is set Correctly
+:testJAVAHOME
+if not exist "%JAVA_HOME%\bin\java.exe" goto wrongJAVAHOME
+if not exist "%JAVA_HOME%\bin\javac.exe" goto wrongJAVAHOME2
+goto gotJAVAHOME
+
+
+:wrongJAVAHOME
+echo JAVA_HOME: "%JAVA_HOME%"
+echo Couldn't find "%JAVA_HOME%\bin\java.exe"
+echo The JAVA_HOME environment variable is not defined correctly
+echo This environment variable is needed to run this program
+goto pauseANdExit  
+
+:wrongJAVAHOME2
+echo JAVA_HOME: "%JAVA_HOME%"
+echo Couldn't find "%JAVA_HOME%\bin\javac.exe"
+echo The JAVA_HOME environment variable is not defined correctly
+echo This environment variable is needed to run this program
+echo NB: JAVA_HOME should point to a JDK not a JRE
+goto pauseANdExit 
+
+::set the RUNJAVA and exit
+:gotJAVAHOME
+ set RUNJAVA=%JAVA_HOME%\bin\java.exe
+ set PATH=%JAVA_HOME%\bin;%PATH%
+ echo JAVA_HOME: "%JAVA_HOME%"
+ echo Your environment has successfully been set up to run Greenstone3
+ goto exit
+
+::try find java on the search path
+:noJAVAHOME
+:: Check if Java is on the search path
+ echo %PATH%| winutil\which.exe java.exe | winutil\setvar.exe JAVAPATH > setjava.bat
+ call setjava.bat
+ del setjava.bat
+
+if exist "%JAVAPATH%\bin\java.exe" goto gotJava
+ :: Still haven't found anything, so try looking in the registry (gulp!)
+  type nul > jdk.reg
+  regedit /E jdk.reg "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit"
+  type jdk.reg > jdk.txt
+  del jdk.reg
+  type nul > jre.reg
+  regedit /E jre.reg "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment"
+  type jre.reg > jre.txt
+  del jre.reg
+
+  winutil\findjava.exe jdk.txt jre.txt | winutil\setvar.exe JAVAPATH > setjava.bat
+  del jdk.txt
+  del jre.txt
+  call setjava.bat
+  del setjava.bat
+
+  :: If nothing was found in the registry, we're stuck
+  if "%JAVAPATH%" == "" goto noJava
+    goto gotJava
+
+:gotJava
+  set JRE_HOME=%JAVAPATH%
+  goto testJREHOME 
 
 
 :noJava
-    echo.
-    echo Failed to locate an appropriate version of Java. You must install a
-    echo Java Development Kit (version 1.4 or greater) before running the
+    echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
+    echo At least one of these environment variable is needed to run Greenstone3  
+    echo You must install a Java Run Environment (version 1.4 or greater) 
+    echo and set the JRE_HOME environment variable before running the
     echo Greenstone 3 Digital Library Software.
     pause
     goto exit
 
+:pauseAndExit
+ pause
+ goto exit 
+ 
 :exit
 
Index: /greenstone3/trunk/gs3-setup.sh
===================================================================
--- /greenstone3/trunk/gs3-setup.sh	(revision 14725)
+++ /greenstone3/trunk/gs3-setup.sh	(revision 14726)
@@ -1,101 +1,155 @@
 # if this file is executed, /bin/sh is used, as we don't start with #!
 # this should work under ash, bash, zsh, ksh, sh style shells.
-
 # make sure we are sourced, and not run
 
-if test "$0" != "`echo $0 | sed s/gs3-setup\.sh//`" ; then
-# if $0 contains "gs3-setup.sh" we've been run... $0 is shellname if sourced.
-# One exception is zsh has an option to set it temporarily to the script name
-  if test -z "$ZSH_NAME" ; then
-  # we aren't using zsh
-  gsdl_not_sourced=true
+export RUNJAVA=
+JRE_HOME=
+JAVA_HOME=
+CLASSPATH=$CLASSPATH
+
+function testSource(){
+  if test "$0" != "`echo $0 | sed s/gs3-setup\.sh//`" ; then
+  # if $0 contains "gs3-setup.sh" we've been run... $0 is shellname if sourced.
+  # One exception is zsh has an option to set it temporarily to the script name
+    if test -z "$ZSH_NAME" ; then
+    # we aren't using zsh
+     gsdl_not_sourced=true
+   fi 
   fi
-fi
 
-if test -n "$gsdl_not_sourced" ; then
-  echo "	Error: Make sure you source this script, not execute it. Eg:"
-  echo "		$ source gs3-setup.sh"
-  echo "	or"
-  echo "		$ . ./gs3-setup.sh"
-  echo "	not"
-  echo "		$ ./gs3-setup.sh"
-  unset gsdl_not_sourced
-  exit 1
-fi
+  if test -n "$gsdl_not_sourced" ; then
+     echo "	Error: Make sure you source this script, not execute it. Eg:"
+     echo "		$ source gs3-setup.sh"
+     echo "	or"
+     echo "		$ . ./gs3-setup.sh"
+     echo "	not"
+     echo "		$ ./gs3-setup.sh"
+     unset gsdl_not_sourced
+     exit 1
+  fi
 
-if test ! -f gs3-setup.sh ; then
-  echo "You must source the script from within the Greenstone home directory"
-  exit 1
-fi
+  if test ! -f gs3-setup.sh ; then
+    echo "You must source the script from within the Greenstone home directory"
+    exit 1
+  fi
+}
 
-GSDL3SRCHOME=`pwd`
-GSDL3HOME=$GSDL3SRCHOME/web
-export GSDL3HOME
-export GSDL3SRCHOME
-GSDLOS=`uname -s | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
-# check for running bash under cygwin
-if test "`echo $GSDLOS | sed 's/cygwin//'`" != "$GSDLOS" ; then
-    GSDLOS=windows
-fi
-export GSDLOS
+function setGS3ENV(){
+   GSDL3SRCHOME=`pwd`
+   GSDL3HOME=$GSDL3SRCHOME/web
+   export GSDL3HOME
+   export GSDL3SRCHOME
+   GSDLOS=`uname -s | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
+    # check for running bash under cygwin
+    if test "`echo $GSDLOS | sed 's/cygwin//'`" != "$GSDLOS" ; then
+        GSDLOS=windows
+    fi 
+    export GSDLOS
 
-#change this if external tomcat
-TOMCAT_HOME=$GSDL3SRCHOME/packages/tomcat
+    #change this if external tomcat
+    TOMCAT_HOME=$GSDL3SRCHOME/packages/tomcat
 
-PATH=$GSDL3SRCHOME/bin/script:$GSDL3SRCHOME/bin:$PATH
-export PATH
-MANPATH=$MANPATH:$GSDL3SRCHOME/doc/man
-export MANPATH
+    PATH=$GSDL3SRCHOME/bin/script:$GSDL3SRCHOME/bin:$PATH
+    export PATH
+    MANPATH=$MANPATH:$GSDL3SRCHOME/doc/man
+    export MANPATH
 
-CLASSPATH=$GSDL3HOME/WEB-INF/classes:$GSDL3SRCHOME/resources/java:$CLASSPATH
-for JARFILE in $GSDL3SRCHOME/*.jar; do
-    CLASSPATH=$CLASSPATH:$JARFILE
-done
-for JARFILE in $GSDL3SRCHOME/lib/jni/*.jar; do
-    CLASSPATH=$CLASSPATH:$JARFILE
-done
-for JARFILE in $GSDL3HOME/WEB-INF/lib/*.jar; do
-    CLASSPATH=$CLASSPATH:$JARFILE
-done
-for JARFILE in $TOMCAT_HOME/common/endorsed/*.jar; do
-    CLASSPATH=$CLASSPATH:$JARFILE
-done    
-for JARFILE in $GSDL3SRCHOME/build/*.jar; do
-    CLASSPATH=$CLASSPATH:$JARFILE
-done
+    CLASSPATH=$GSDL3HOME/WEB-INF/classes:$GSDL3SRCHOME/resources/java:$CLASSPATH
+     for JARFILE in $GSDL3SRCHOME/*.jar; do
+       CLASSPATH=$CLASSPATH:$JARFILE
+     done
+     for JARFILE in $GSDL3SRCHOME/lib/jni/*.jar; do
+        CLASSPATH=$CLASSPATH:$JARFILE
+     done
+     for JARFILE in $GSDL3HOME/WEB-INF/lib/*.jar; do
+        CLASSPATH=$CLASSPATH:$JARFILE
+     done
+     for JARFILE in $TOMCAT_HOME/common/endorsed/*.jar; do
+        CLASSPATH=$CLASSPATH:$JARFILE
+     done    
+     for JARFILE in $GSDL3SRCHOME/build/*.jar; do
+       CLASSPATH=$CLASSPATH:$JARFILE
+     done
+     export CLASSPATH
+     LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GSDL3SRCHOME/lib/jni
+     export LD_LIBRARY_PATH
+      ## for mac
+     DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$GSDL3SRCHOME/lib/jni
+     export DYLD_LIBRARY_PATH
+}
 
-export CLASSPATH
+function setJAVAHOME(){
+    # see if we can find java
+    java="java"
+    jfound=1
+    jok=1
+    # if JREHOME, JRE_HOME is set, we'll use it to determine where java lives 
+    if [ "$JRE_HOME" != "" ]; then
+       java="$JRE_HOME/bin/java"
+       export JRE_HOME=$JRE_HOME
+    elif [ "$JREHOME" != "" ]; then
+       java="$JREHOME/bin/java"
+       export JRE_HOME=$JREHOME
+     # JAVAHOME or JAVA_HOME is set we'll use it to determine where java lives, otherwise
+     #  we just hope it's on the search path
+     elif [ "$JAVA_HOME" != "" ] ; then
+       java="$JAVA_HOME/bin/java"
+       export JAVA_HOME=$JAVA_HOME
+     elif [ "$JAVAHOME" != "" ] ; then
+       java="$JAVAHOME/bin/java"
+       export JAVA_HOME=$JAVAHOME
+    else
+        java=`which java 2> /dev/null`
+        if [ ! -x "$java" ]; then
+	    echo "Failed to locate Java. You must install a java runtime environment"
+	    echo "(version 1.4 or greater) before installing Greenstone 3."
+            pauseAndExit
+        else
+	  export JRE_HOME=`echo $java | sed -n 's/[\\\/]bin[\\\/]java$//p'`
+        fi
+   fi
+   checkJAVAHOME
+}
 
-LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GSDL3SRCHOME/lib/jni
-export LD_LIBRARY_PATH
-## for mac
-DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$GSDL3SRCHOME/lib/jni
-export DYLD_LIBRARY_PATH
+function checkJAVAHOME(){
+# Make sure prerequisite environment variables are set
+  if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then
+    echo "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined"
+    echo "At least one of these environment variable is needed to run Greenstone3"  
+    pauseAndExit
+  else # check JRE_HOME
+      if [ -n "$JRE_HOME" ]; then
+         if [ ! -x "$JRE_HOME"/bin/java ]; then
+	    echo "JRE_HOME: $JRE_HOME" 
+	    echo "Couldn't find $JRE_HOME/bin/java"
+            echo "The JRE_HOME environment variable is not defined correctly"
+            echo "This environment variable is needed to run this program"
+	    pauseAndExit
+         else
+	   export RUNJAVA="$JRE_HOME"/bin/java 
+           checkVersion
+         fi  	
+      else
+          # check JAVA_HOME if JRE_HOME is not set   
+          if [ -n "$JAVA_HOME" ]; then
+              if [ ! -x "$JAVA_HOME"/bin/java -o ! -x "$JAVA_HOME"/bin/javac ]; then
+		echo "JAVA_HOME: $JAVA_HOME"  
+                echo "Couldn't find $JAVA_HOME/bin/java or $JAVA_HOME/bin/javac"
+                echo "The JAVA_HOME environment variable is not defined correctly"
+                echo "This environment variable is needed to run this program"
+                echo "NB: JAVA_HOME should point to a JDK not a JRE"
+		pauseAndExit
+	      else
+               export RUNJAVA="$JAVA_HOME"/bin/java
+               checkVersion
+              fi
+           fi
+        fi
+  fi
+}
 
-
-# see if we can find java
-java="java"
-jfound=1
-jok=1
-# if JAVAHOME or JAVA_HOME is set we'll use it to determine where java lives, otherwise
-#  we just hope it's on the search path
-if [ "$JAVA_HOME" != "" ] ; then
-    java="$JAVA_HOME/bin/java"
-elif [ "$JAVAHOME" != "" ] ; then
-    java="$JAVAHOME/bin/java"
-    export JAVA_HOME=$JAVAHOME
-else
-    java=`which java 2> /dev/null`
-    if [ ! -x "$java" ]; then
-	echo "Failed to locate Java. You must install a java runtime environment"
-	echo "(version 1.4 or greater) before installing Greenstone 3."
-	jfound=0
-	jok=0
-    else
-	export JAVA_HOME=`echo $java | sed -n 's/[\\\/]bin[\\\/]java$//p'`
-    fi
-fi
+function checkVersion(){
 if [ $jfound -eq 1 ]; then
-    javaversion=`$java -version 2>&1 | sed -n 's/^java version \"\(.*\)\"/\1/p'`
+    javaversion=`$RUNJAVA -version 2>&1 | sed -n 's/^java version \"\(.*\)\"/\1/p'`
     jvmajor=`echo $javaversion | sed -n 's/^\([0-9]*\).*$/\1/p'`
     jvminor=`echo $javaversion | sed -n 's/^[0-9]*\.\([0-9]*\).*$/\1/p'`
@@ -113,20 +167,28 @@
     echo "old to run Greenstone 3. Please install a new version of the JRE (version"
     echo "1.4 or newer) and rerun this installation."
+    export RUNJAVA=
+    pauseAndExit
 else
+    if [ -n "$JRE_HOME" ]; then
+     echo "JRE_HOME: $JRE_HOME" 
+     export PATH="$JRE_HOME"\bin:$PATH
+    fi
+    if [ -n "$JAVA_HOME" ]; then
+     echo "JAVA_HOME: $JAVA_HOME"
+     export PATH="$JAVA_HOME"\bin:$PATH
+    fi
     echo "Your environment has successfully been set up to run Greenstone3"
 fi
-
-#do we need to check for perl???
-# make sure perl is ok
-#perl=`which perl 2> /dev/null`
-# echo "perl: $perl"
-#if [ ! -x "$perl" ] ; then
-#    echo "Greenstone 3 requires perl in order to operate but this installation"
-#    echo "could not detect perl on your system. Please ensure that perl is installed"
-#    echo "and is on your search path then rerun this installation script."
-#    exit 1
-#fi
+}
 
 
+function pauseAndExit(){
+ echo -n "Please press any key to continue... "
+ read 
+}
+
+testSource
+setGS3ENV
+setJAVAHOME
 
 
