Index: /gli/trunk/gli.sh
===================================================================
--- /gli/trunk/gli.sh	(revision 15609)
+++ /gli/trunk/gli.sh	(revision 15610)
@@ -1,3 +1,58 @@
 #!/bin/sh
+
+# Function that, when given gsdlpath as parameter, will return the
+# version of greenstone that is to run (2 or 3). If things are not,
+# this program will exit here.
+get_version() {
+    # first parameter is value of gsdlpath
+    if [ -f "${1}/gs3-setup.sh" ]; then
+	return 3
+    elif [ -f "${1}/setup.bash" ]; then
+	return 2
+    else
+	echo "Error: can't determine which Greenstone version is being run."
+	exit 1
+    fi
+}
+
+# Function that is passed the following paramters (in order):
+# - the gsdlpath (GS3 home, GS2 home or gs2build for GS3), 
+# - the version of greenstone that's running, and 
+# - the language GLI is set to
+# and checks the installation. 
+# If things are not right, this program will exit here.
+check_installation() {
+# Check that the Greenstone installation looks OK
+    if [ "$3" = "es" ]; then
+	echo "Revisando GSDL: $1"
+    elif [ "$3" = "fr" ]; then
+	echo "Vérification de GSDL: $1"
+    elif [ "$3" = "ru" ]; then
+	echo "ðÒÏ×ÅÒËÁ GSDL: $1"
+    else
+	echo "Checking GSDL: $1"
+    fi
+    # even if we are only checking for gs2build (gsdl2path), we still 
+    # need the file setup.bash to exist in the following condition:
+    if [ ! -f "${1}/gs3-setup.sh" -a ! -f "${1}/setup.bash" ] ; then
+	echo
+	if [ "$3" = "es" ]; then
+	    echo "No se pudo encontrar la instalación de Greenstone $2 o está incompleta."
+	    echo "Trate de reinstalar Greenstone $2 y a continuación ejecute nuevamente"
+	    echo "este guión."
+	elif [ "$3" = "fr" ]; then
+	    echo "L'installation de Greenstone $2 est introuvable ou incomplète."
+	    echo "Essayez de réinstaller Greenstone $2 et exécutez ce script à nouveau."
+	elif [ "$3" = "ru" ]; then
+	    echo "éÎÓÔÁÌÌÑÃÉÑ Greenstone $_version ÎÅ ÂÙÌÁ ÎÁÊÄÅÎÁ ÉÌÉ ÏÎÁ ÎÅÐÏÌÎÁ."
+	    echo "ðÏÐÒÏÂÕÊÔÅ ÐÏ×ÔÏÒÎÏ ÕÓÔÁÎÏ×ÉÔØ Greenstone $2, Á ÚÁÔÅÍ ××ÅÓÔÉ ÜÔÏÔ ÓËÒÉÐÔ ÓÎÏ×Á."
+	else
+	    echo "The Greenstone $2 installation could not be found, or is incomplete."
+	    echo "Try reinstalling Greenstone $2 then running this script again."
+	fi
+	exit 1
+    fi
+}
+
 glilang=en
 
@@ -66,54 +121,110 @@
 
 ##  ---- Determine GSDLHOME ----
+## gsdlpath can be either Greenstone 3 or Greenstone 2
 gsdlpath=
-
 # Some users may set the above line manually
-if [ "x$gsdlpath" = "x" ]; then
+
+
+# This variable is set automatically:
+_version=
+if [ "x$gsdlpath" != "x" ]; then
+    get_version $gsdlpath
+    _version=$?
+# otherwise $gsdlpath is not yet set
+else
     # Check the environment variable first
-    if [ "x$GSDLHOME" != "x" ]; then
+    # Check whether environment variables for both GS2 and GS3 are set
+    # and if so, warn the user that we have defaulted to GS3
+    if [ "x$GSDLHOME" != "x" -a "x$GSDL3SRCHOME" != "x" ]; then
+        # _version not set, but both env vars set, so default to 3
+	_version=3
+	gsdlpath=$GSDL3SRCHOME
+	echo "Both Greenstone 2 and Greenstone 3 environments are set." 
+	echo "It is assumed you want to run Greenstone 3."
+	echo "If you want to run Greenstone 2, please unset the"
+	echo "environment variable GSDL3SRCHOME before running GLI."
+	echo ""
+    elif [ "x$GSDL3SRCHOME" != "x" ]; then
+	echo "Only gsdl3srchome set"
+	gsdlpath=$GSDL3SRCHOME
+	_version=3
+	echo "$gsdlpath"
+    elif [ "x$GSDLHOME" != "x" ]; then
 	gsdlpath=$GSDLHOME
-
+	_version=2
     # If it is not set, assume that the GLI is installed as a subdirectory of Greenstone
     else
 	gsdlpath=`(cd .. && pwd)`
-    fi
-fi
-
-# Check that the Greenstone installation looks OK
-if [ "$glilang" = "es" ]; then
-    echo "Revisando GSDL: $gsdlpath"
-elif [ "$glilang" = "fr" ]; then
-    echo "Vérification de GSDL: $gsdlpath"
-elif [ "$glilang" = "ru" ]; then
-    echo "ðÒÏ×ÅÒËÁ GSDL: $gsdlpath"
-else
-    echo "Checking GSDL: $gsdlpath"
-fi
-if [ ! -f "${gsdlpath}/setup.bash" ] ; then
-    echo
-    if [ "$glilang" = "es" ]; then
-	echo "No se pudo encontrar la instalación de Greenstone o está incompleta."
-        echo "Trate de reinstalar Greenstone y a continuación ejecute nuevamente"
-	echo "este guión."
-    elif [ "$glilang" = "fr" ]; then
-	echo "L'installation de Greenstone est introuvable ou incomplète."
-	echo "Essayez de réinstaller Greenstone et exécutez ce script à nouveau."
-    elif [ "$glilang" = "ru" ]; then
-	echo "éÎÓÔÁÌÌÑÃÉÑ Greenstone ÎÅ ÂÙÌÁ ÎÁÊÄÅÎÁ ÉÌÉ ÏÎÁ ÎÅÐÏÌÎÁ."
-	echo "ðÏÐÒÏÂÕÊÔÅ ÐÏ×ÔÏÒÎÏ ÕÓÔÁÎÏ×ÉÔØ Greenstone, Á ÚÁÔÅÍ ××ÅÓÔÉ ÜÔÏÔ ÓËÒÉÐÔ ÓÎÏ×Á."
-    else
-	echo "The Greenstone installation could not be found, or is incomplete."
-	echo "Try reinstalling Greenstone then running this script again."
-    fi
-    exit 1
-fi
-
-# Setup Greenstone, unless it has already been done
-if [ "x$GSDLHOME" = "x" ]; then
-    cd "$gsdlpath"
-    . setup.bash
-    cd "$thisdir"
-fi
-
+	# Still need to find what version we are running:
+	# GS3 main directory contains file gs3-setup.sh, GS2 only setup.bash
+	get_version $gsdlpath
+	_version=$?
+    fi
+fi
+
+echo "***Greenstone version found: $_version"
+echo
+
+# Check that the main Greenstone installation for the version we're running looks OK
+check_installation $gsdlpath $_version $glilang
+
+
+# Need to source the correct setup file depending on whether we are running
+# gs3 or gs2
+# If we're running version GS2
+if [ "$_version" -eq 2 ]; then
+    echo "Version is 2"
+    # Setup Greenstone 2, unless it has already been done
+    if [ "x$GSDLHOME" = "x" ]; then
+	cd "$gsdlpath"
+	. setup.bash
+	cd "$thisdir"
+    fi
+# else, if we're running GS3
+elif [ "$_version" -eq 3 ]; then
+    # Setup Greenstone 3, unless it has already been done
+    if [ "x$GSDL3HOME" = "x" -o "x$GSDL3SRCHOME" = "x" ]; then
+	cd "$gsdlpath"
+	. gs3-setup.sh
+	cd "$thisdir"
+    fi
+    
+    ## if Greenstone version 3 is running, we want to set gsdl2path
+    ##  ---- Determine GSDLHOME ----
+    ## may be already set, or manually entered here.
+    gsdl2path=
+    
+    # Some users may set the above line manually
+    if [ "x$gsdl2path" = "x" ]; then
+        # Check the environment variable first
+	if [ "x$GSDLHOME" != "x" ]; then
+	    echo "GSDLHOME environment variable is set to $GSDLHOME."
+	    echo "Will use this to find build scripts."
+	    gsdl2path=$GSDLHOME
+        # If it is not set, assume that the gs2build subdirectory of Greenstone 3 exists
+	else
+	    gsdl2path=$GSDL3SRCHOME/gs2build
+	fi
+    fi
+    # Check that Greenstone 3's Greenstone 2 stuff looks OK (in gs2build)
+    check_installation $gsdl2path "" $glilang
+  
+    # Setup Greenstone 3's gs2build, unless it has already been done
+    if [ "x$GSDLHOME" = "x" ]; then
+	cd "$gsdl2path"
+	. setup.bash
+	cd "$thisdir"
+    fi
+
+else 
+    echo "Greenstone version unknown."    
+    exit 1
+fi
+
+echo
+echo "***GSDLHOME is: $GSDLHOME"
+echo "***GSDL3SRCHOME is: $GSDL3SRCHOME"
+echo "***GSDL3HOME is: $GSDL3HOME"
+echo
 
 ##  ---- Check Perl exists ----
@@ -270,7 +381,9 @@
 fi
 
+# basic_command is the cmd string common to both Greenstone 3 and Greenstone 2 execution
+basic_command="$javapath -Xmx128M -classpath classes/:GLI.jar:lib/apache.jar:lib/qfslib.jar org.greenstone.gatherer.GathererProg"
 stop_gli=0
 while [ "$stop_gli" = "0" ] ; do 
-
+    
 # Other arguments you can provide to GLI to work around memory limitations, or debug
 # -Xms<number>M    To set minimum memory (by default 32MB)
@@ -280,25 +393,29 @@
 # -Xprof           Function call profiling
 # -Xloggc:<file>   Write garbage collection log
-
-
-
-  $javapath -Xmx128M -classpath classes/:GLI.jar:lib/apache.jar:lib/qfslib.jar org.greenstone.gatherer.GathererProg -gsdl $GSDLHOME -gsdlos $GSDLOS $*
-  exit_status=$?
-
-  if [ "$exit_status" != "2" ] ; then
-    stop_gli=1
-  else
-    echo
-    if [ "$glilang" = "es" ]; then
-        echo "Restarting/Ejecutando la Interfaz de la $PROGNAME..."
-    elif [ "$glilang" = "fr" ]; then
-        echo "Restarting/Exécution de $PROGNAME..."
-    elif [ "$glilang" = "ru" ]; then
-        echo "Restarting/ôÅËÕÝÉÊ ÂÉÂÌÉÏÔÅÞÎÙÊ $PROGNAME..."
-    else
-        echo "Restarting the $PROGNAME..."
-    fi
-
-  fi
+    
+    exit_status=0
+    if [ "$_version" -eq 2 ]; then
+	`$basic_command -gsdl $GSDLHOME -gsdlos $GSDLOS $*`
+	exit_status=$?
+    elif [ "$_version" -eq 3 ]; then    
+	`$basic_command -gsdl $GSDLHOME -gsdlos $GSDLOS -gsdl3 $GSDL3HOME -gsdl3src $GSDL3SRCHOME $*`
+	exit_status=$?
+    fi
+    
+    if [ "$exit_status" != "2" ] ; then
+	stop_gli=1
+    else
+	echo
+	if [ "$glilang" = "es" ]; then
+	    echo "Restarting/Ejecutando la Interfaz de la $PROGNAME..."
+	elif [ "$glilang" = "fr" ]; then
+	    echo "Restarting/Exécution de $PROGNAME..."
+	elif [ "$glilang" = "ru" ]; then
+	    echo "Restarting/ôÅËÕÝÉÊ ÂÉÂÌÉÏÔÅÞÎÙÊ $PROGNAME..."
+	else
+	    echo "Restarting the $PROGNAME..."
+	fi
+	
+    fi
 done 
 
