Index: greenstone3/branches/3.03/gs3-server.bat
===================================================================
--- greenstone3/branches/3.03/gs3-server.bat	(revision 14724)
+++ greenstone3/branches/3.03/gs3-server.bat	(revision 14728)
@@ -1,131 +1,25 @@
 @echo off
 
-set SERVERLANG=en
+echo Greenstone 3 Server
+echo Copyright (C) 2006, New Zealand Digital Library Project, University Of Waikato
+echo This software comes with ABSOLUTELY NO WARRANTY; for details see LICENSE.txt
+echo This is free software, and you are welcome to redistribute it
 
-:: -------- Run the Greenstone 3 Server --------
-
-:: This script must be run from within the directory in which it lives
-if exist gs3-server.bat goto start
-    if "%SERVERLANG%" == "en" echo This script must be run from the directory in which it resides.
-    if "%SERVERLANG%" == "es" echo Este guiÂ¢n deberÂ  ejecutarse desde el directorio en el que reside.
-    if "%SERVERLANG%" == "fr" echo Ce script doit Ëtre exâcutâ âŠ partir du râpertoire dans lequel il se trouve.
-    if "%SERVERLANG%" == "ru" echo ÂÃ¢Â®Ã¢ Ã¡ÂªÃ ÂšÂ¯Ã¢ Â€Â®Â«ÂŠÂ¥Â­ Â¡Ã«Ã¢Ã¬ Â¢Â§Ã¯Ã¢ ÂšÂ§ Â€ÂšÃ Â¥ÂªÃ¢Â®Ã ÂšÂš, Â¢ ÂªÂ®Ã¢Â®Ã Â®Â© Â®Â­ Ã Â Ã¡Â¯Â®Â«Â®ÂŠÂ¥Â­
-    goto exit
-
-:start
-if "%OS%" == "Windows_NT" goto findGSDL
-    :: Invoke a new command processor to ensure there's enough environment space
-    if "%1" == "Second" goto findGSDL
-        command /E:2048 /C %0 Second %1 %2 %3 %4 %5 %6 %7 %8 %9
-        goto done
-
-:findGSDL
-echo.
-if "%SERVERLANG%" == "en" echo Greenstone3 Server
-if "%SERVERLANG%" == "en" echo Copyright (C) 2006, New Zealand Digital Library Project, University Of Waikato
-if "%SERVERLANG%" == "en" echo This program comes with ABSOLUTELY NO WARRANTY; for details see LICENSE.txt
-if "%SERVERLANG%" == "en" echo This is free software, and you are welcome to redistribute it
-
-echo.
 ::  ---- Determine GSDL3HOME ----
+:: Some users may set the above line manually
 set GSDL3PATH=
 
-:: Some users may set the above line manually
- if not "%GSDL3PATH%" == "" goto prepGSDL
+:: The default location is the current directory
+ if "%GSDL3PATH%" == "" set GSDL3PATH=.
 
- :: The default location is the current directory
- set GSDL3PATH=.
+:: Setup Greenstone, unless it has already been done
+if "%GSDL3SRCHOME%" == "" call "%GSDL3PATH%\gs3-setup.bat" SetEnv > nul
 
- :: If it is set, use the GSDLHOME environment variable
-  if "%GSDL3SRCHOME%" == "" goto prepGSDL
-  set GSDL3PATH=%GSDL3SRCHOME%
-
-:prepGSDL
-:: Setup Greenstone, unless it has already been done
-if not "%GSDL3SRCHOME%" == "" goto doneGSDL
-   call "%GSDL3PATH%\gs3-setup.bat" SetEnv > nul
-
-:doneGSDL
-:: GSDL3PATH is no longer needed, since GSDL3SRCHOME should now be set
 set GSDL3PATH=
 
-:findJava
-:: ---- Check Java exists ----
-set JAVAPATH=
+"%RUNJAVA%" -cp "%CLASSPATH%" org.greenstone.server.Server "%GSDL3SRCHOME%"
 
-:: 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
 
-:: 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 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
-
-    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
-
-   set JAVAPATH=%JAVAPATH%\bin
-   goto testJava
-
-:javahome
-    set JAVAPATH=%JAVA_HOME%\bin
-
-:testJava
-:: Check that a Java executable has been found
-  if "%SERVERLANG%" == "en" echo Checking Java: %JAVAPATH%
-  if "%SERVERLANG%" == "es" echo Revisando Java: %JAVAPATH%
-  if "%SERVERLANG%" == "fr" echo Vârification de Java: %JAVAPATH%
-  if "%SERVERLANG%" == "ru" echo ÂÃ Â®Â¢Â¥Ã ÂªÂ  Java: %JAVAPATH%
-  if exist "%JAVAPATH%\java.exe" goto checkServer
-
-:noJava
- echo.
-    if "%SERVERLANG%" == "en" echo Failed to locate an appropriate version of Java. You must install a
-    if "%SERVERLANG%" == "en" echo Java Runtime Environment (version 1.4 or greater) before running the
-    if "%SERVERLANG%" == "en" echo Greenstone3 Server.
-    goto exit
-
-:checkServer
-::---- Check that the server program has been compiled ----
-if exist "server.jar" goto runServer
-if "%SERVERLANG%" == "en" echo "You need to compile the code before running the server"
-goto exit
-
-:runServer
-echo Running the Greenstone Server
-:: -Xms32M          To set minimum memory
-:: -Xmx32M          To set maximum memory
-:: -verbose:gc      To set garbage collection messages
-:: -Xincgc          For incremental garbage collection
-:: -Xprof           Function call profiling
-:: -Xloggc:<file>   Write garbage collection log
-
-"%JAVAPATH%\java" -cp "%CLASSPATH%" org.greenstone.server.Server "%GSDL3SRCHOME%"
-
-:exit
-
-:done
-:: ---- Clean up ----
-set JAVAPATH=
 
  
Index: greenstone3/branches/3.03/gs3-server.sh
===================================================================
--- greenstone3/branches/3.03/gs3-server.sh	(revision 14724)
+++ greenstone3/branches/3.03/gs3-server.sh	(revision 14728)
@@ -2,10 +2,8 @@
 serverlang=en
 
-echo
 echo "Greenstone 3 Server"
 echo "Copyright (C) 2006, New Zealand Digital Library Project, University Of Waikato"
 echo "This software comes with ABSOLUTELY NO WARRANTY; for details see LICENSE.txt"
 echo "This is free software, and you are welcome to redistribute it"
-echo
 
 ##  -------- Run the Greenstone 3 Server --------
@@ -14,16 +12,7 @@
 thisdir=`pwd`
 if [ ! -f "${thisdir}/gs3-server.sh" ]; then
-    if [ "$serverlang" == "es" ]; then
-        echo "Este guión deberá ejecutarse desde el directorio en el que reside."
-    elif [ "$serverlang" == "fr" ]; then
-	echo "Ce script doit être exécuté à partir du répertoire dans lequel il se trouve."
-    elif [ "$serverlang" == "ru" ]; then
-	echo "üÔÏÔ ÓËÒÉÐÔ ÄÏÌÖÅÎ ÂÙÔØ ×ÚÑÔ ÉÚ ÄÉÒÅËÔÏÒÉÉ, × ËÏÔÏÒÏÊ ÏÎ ÒÁÓÐÏÌÏÖÅÎ"
-    else
-	echo "This script must be run from the directory in which it resides."
-    fi
+    echo "This script must be run from the directory in which it resides."
     exit 1
 fi
-
 
 ##  ---- Determine GSDL3SRCHOME ----
@@ -31,77 +20,19 @@
 
 # Some users may set the above line manually
-if [ "x$gsdl3path" == "x" ]; then
-    # Check the environment variable first
-    if [ "x$GSDL3SRCHOME" != "x" ]; then
-	gsdl3path=$GSDL3SRCHOME
-    else
-	gsdl3path=`pwd`
-    fi
+if [ -z "$gsdl3path" ]; then
+   gsdl3path=`pwd`  
 fi
 
-# Setup Greenstone3, unless it has already been done
-if [ "x$GSDL3SRCHOME" == "x" ]; then
-    pushd $gsdl3path > /dev/null
-    source gs3-setup.sh
-    popd > /dev/null
-fi
-
-## ---- Check Java exists ----
-javapath=
-
-# Some users may set the above line manually
-if [ "x$javapath" = "x" ]; then
-
-    # If it is set, use the JAVA_HOME environment variable
-    if [ "x$JAVA_HOME" != "x" ]; then
-        javapath="$JAVA_HOME/bin/java"
-
-    # Check if Java is on the search path
-    else
-        javapath=`which java 2> /dev/null`
-    fi
-fi
-
-# Check that a Java executable has been found
-if [ "$glilang" = "es" ]; then
-    echo "Revisando Java: $javapath"
-elif [ "$glilang" = "fr" ]; then
-    echo "V<E9>rification de Java: $javapath"
-elif [ "$glilang" = "ru" ]; then
-    echo "<F0><D2><CF><D7><C5><D2><CB><C1> Java: $javapath"
-else
-    echo "Checking Java: $javapath"
-fi
-if [ ! -x "$javapath" ]; then
-    echo
-    echo "Failed to locate an appropriate version of Java. You must installa"
-    echo "Java Runtime Environment (version 1.4 or greater) before running the"
-    echo "Greenstone Server."
-exit 1
-fi
-
-## -- Check the version of Java is new enough (1.4.0 or higher) to run the Server -- do we need this?
-#`$javapath -classpath . CheckJavaVersion > /dev/null`
-#if [ $? -ne 2 ] ; then
-#   echo "The version of the Java Runtime Environment you have installed is too"
-#   echo "old to run the Greenstone Librarian Interface. Please install a new"
-#   echo "version of the JRE (version 1.4 or newer) and rerun this script."
-#exit 1
-#fi
-
-## ---- Check that the server has been compiled ----
-if [ ! -f server.jar ]; then
-  echo "You need to compile the server before running this script"
-  exit 1
+if [ -z "$GSDL3SRCHOME" ]; then 
+  # Setup Greenstone3, unless it has already been done
+  pushd $gsdl3path > /dev/null
+  source gs3-setup.sh
+  popd > /dev/null
 fi
 
 
-# Other arguments you can provide to java 
-# -Xms<number>M    To set minimum memory (by default 32MB)
-# -Xmx<number>M    To set maximum memory (by default the nearest 2^n to the total remaining physical memory)
-# -verbose:gc      To set garbage collection messages
-# -Xincgc          For incremental garbage collection (significantly slows performance)
-# -Xprof           Function call profiling
-# -Xloggc:<file>   Write garbage collection log
+# JRE_HOME or JAVA_HOME must be set correctly to run this program
+if [ -n "$RUNJAVA" ]; then
+  "$RUNJAVA" -cp $CLASSPATH org.greenstone.server.Server $GSDL3SRCHOME 
+fi
 
-java -cp $CLASSPATH org.greenstone.server.Server $GSDL3SRCHOME 
Index: greenstone3/branches/3.03/gs3-setup.bat
===================================================================
--- greenstone3/branches/3.03/gs3-setup.bat	(revision 14724)
+++ greenstone3/branches/3.03/gs3-setup.bat	(revision 14728)
@@ -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/branches/3.03/gs3-setup.sh
===================================================================
--- greenstone3/branches/3.03/gs3-setup.sh	(revision 14724)
+++ greenstone3/branches/3.03/gs3-setup.sh	(revision 14728)
@@ -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
 
 
