root/main/trunk/greenstone3/gs3-setup.sh @ 32371

Revision 31746, 17.2 KB (checked in by ak19, 2 years ago)

Fix to silly bug

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1# if this file is executed, /bin/sh is used, as we don't start with #!
2# this should work under ash, bash, zsh, ksh, sh style shells.
3
4#the purpose of this file is to check/set up the environment for greenstone3
5#sorts out:
6#  - gsdl3home
7#  - java
8
9# java_min_version gets passed to search4j as the minimum java version
10java_min_version=1.5.0_00
11DEBUG=false
12
13function testSource(){
14
15  if test "$0" != "`echo $0 | sed s/gs3-setup\.sh//`" ; then
16  # if $0 contains "gs3-setup.sh" we've been run... $0 is shellname if sourced.
17  # One exception is zsh has an option to set it temporarily to the script name
18    if test -z "$ZSH_NAME" ; then
19    # we aren't using zsh
20     gsdl_not_sourced=true
21   fi
22  fi
23
24  if test -n "$gsdl_not_sourced" ; then
25     echo "  Error: Make sure you source this script, not execute it. Eg:"
26     echo "    $ source gs3-setup.sh"
27     echo "  or"
28     echo "    $ . gs3-setup.sh"
29     echo "  not"
30     echo "    $ ./gs3-setup.sh"
31     unset gsdl_not_sourced
32     exit 1
33  fi
34
35  if test ! -f gs3-setup.sh ; then
36    echo "You must source the script from within the Greenstone home directory"
37    return 1
38  fi
39  return 0
40}
41
42# if GSDL3SRCHOME is set, then we assume that we have already sourced the
43# script so don't do it again. UNLESS, GSDL3SRCHOME doesn't match the
44# current directory in which case it was a different gs3 installation, so lets
45# do it now.x
46function testAlreadySourced() {
47  if [ ! -z "$GSDL3SRCHOME" ]; then
48    localgs3sourcehome="`pwd`"
49    if [ "$GSDL3SRCHOME" == "$localgs3sourcehome" ]; then
50      echo "Your environment is already set up for Greenstone3"
51      return 1
52    fi
53    echo "Your environment was set up for Greenstone 3 in $GSDL3SRCHOME."
54    echo "Overwriting that set up for the current Greenstone 3 in $localgs3sourcehome"
55  fi
56  return 0
57}
58
59function setGS3ENV() {
60
61  echo "Setting up your environment for Greenstone3"
62  ## main greenstone environment variables ##
63  GSDL3SRCHOME="`pwd`"
64  GSDL3HOME="$GSDL3SRCHOME/web"
65  export GSDL3HOME
66  export GSDL3SRCHOME
67
68  if test "x$GSDLOS" = "x" ; then
69    GSDLOS=`uname -s | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
70    # check for running bash under cygwin
71    if test "`echo $GSDLOS | sed 's/cygwin//'`" != "$GSDLOS" ; then
72      GSDLOS=windows
73    fi
74  fi
75  export GSDLOS
76  echo "  - Exported GSDL3HOME, GSDL3SRCHOME and GSDLOS"
77
78  #change this if external tomcat
79  TOMCAT_HOME="$GSDL3SRCHOME/packages/tomcat"
80
81  ## adjustments to users (existing) environment ##
82
83  #PATH
84  addtopath PATH "$GSDL3SRCHOME/bin/script"
85  addtopath PATH "$GSDL3SRCHOME/bin"
86  echo "  - Adjusted PATH"
87
88  #MANPATH
89  addtopath MANPATH "$GSDL3SRCHOME/doc/man"
90  echo "  - Adjusted MANPATH"
91 
92  #CLASSPATH
93  addtopath CLASSPATH "."
94  addtopath CLASSPATH "$GSDL3HOME/WEB-INF/classes"
95  addtopath CLASSPATH "$GSDL3SRCHOME/resources/java"
96  addtopath CLASSPATH "$GSDL3SRCHOME/cp.jar"
97
98  # Tomcat 5 jar files
99  for JARFILE in "$TOMCAT_HOME"/common/endorsed/*.jar; do
100    addtopath CLASSPATH "$JARFILE"
101  done   
102  # Tomcat 6 jar files
103  for JARFILE in "$TOMCAT_HOME"/lib/*.jar; do
104    addtopath CLASSPATH "$JARFILE"
105  done   
106 
107  #shouldn't need these as they will have been copied to their correct locations elsewhere in the greenstone3 installation
108  #for JARFILE in "$GSDL3SRCHOME"/build/*.jar; do
109  #  addtopath CLASSPATH "$JARFILE"
110  #done
111
112  echo "  - Adjusted CLASSPATH"
113
114  #LD_LIBRARY_PATH
115  addtopath LD_LIBRARY_PATH "$GSDL3SRCHOME/lib/jni"
116  addtopath DYLD_LIBRARY_PATH "$GSDL3SRCHOME/lib/jni"
117  echo "  - Adjusted LD_LIBRARY_PATH and DYLD_LIBRARY_PATH"
118
119  #ant
120  ANT_VERSION=1.7.1
121  if [ -x "$GSDL3SRCHOME/packages/ant/bin/ant" ]; then
122    ANT_HOME="$GSDL3SRCHOME/packages/ant"
123    export ANT_HOME
124    addtopath PATH "$ANT_HOME/bin"
125    echo "  - Setup Greenstone ant ($GSDL3SRCHOME/packages/ant)"
126  else
127    which ant &> /dev/null
128    if [ "$?" == "0" ]; then
129      echo "  - WARNING: Greenstone 'Ant' package missing - falling back to system Ant"
130      echo "             Note that Greenstone requires Ant $ANT_VERSION or greater"
131    elif [ "ANT_HOME" != "" ]; then
132      addtopath PATH "$ANT_HOME/bin"
133      echo "  - WARNING: Greenstone 'Ant' package missing - falling back to system Ant"
134      echo "             Note that Greenstone requires Ant $ANT_VERSION or greater"
135    else
136      echo "  - ERROR: Greenstone 'Ant' package missing - please install Ant yourself"
137      echo "           Note that Greenstone requires Ant $ANT_VERSION or greater"
138    fi
139  fi
140
141  #ImageMagick
142  #if test -d "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/imagemagick" ; then
143  #  addtopath PATH "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/imagemagick/bin"
144  #  MAGICK_HOME="$GSDL3SRCHOME/gs2build/bin/$GSDLOS/imagemagick"
145  #  export MAGICK_HOME
146  #  if test "$GSDLOS" = "linux"; then
147  #    addtopath LD_LIBRARY_PATH "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/imagemagick/lib"
148  #  elif test "$GSDLOS" = "darwin"; then
149  #    addtopath DYLD_LIBRARY_PATH "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/imagemagick/lib"
150  #  fi
151  #  echo "  - Setup ImageMagick"
152  #fi
153
154  #Ghostscript
155  if test -d "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/ghostscript"; then
156    addtopath PATH "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/ghostscript/bin"
157    GS_LIB="$GSDL3SRCHOME/gs2build/bin/$GSDLOS/ghostscript/share/ghostscript/8.63/lib"
158    export GS_LIB
159    GS_FONTPATH="$GSDL3SRCHOME/gs2build/bin/$GSDLOS/ghostscript/share/ghostscript/8.63/Resource/Font"
160    export GS_FONTPATH
161    echo "  - Setup GhostScript"
162  fi
163
164  #wvWare
165  # wvWare's environment is now set up by bin/script/wvware.pl
166  # The wvware.pl script can be called from the cmdline to perform wvware tasks.
167  # GLI calls gsConvert.pl which calls wvware.pl to similarly perform wvware tasks.
168#  if test -d "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/wv"; then
169#    if test "$GSDLOS" = "linux"; then
170#      addtopath LD_LIBRARY_PATH "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/wv/lib"
171#    elif test "$GSDLOS" = "darwin"; then
172#      addtopath DYLD_LIBRARY_PATH "$GSDL3SRCHOME/gs2build/bin/$GSDLOS/wv/lib"
173#    fi
174#    echo "  - Setup wvWare"
175#  fi
176
177}
178
179function checkJava() {
180
181  # we now include a JRE with Mac (Mountain) Lion too, because from Yosemite onwards there's no system Java on Macs
182  BUNDLED_JRE="`pwd`/packages/jre"
183  HINT=$BUNDLED_JRE
184 
185  #if [ "$GSDLOS" = "darwin" ] && [ ! -d "$HINT" ]; then
186  if [ "$GSDLOS" = "darwin" ]; then
187      if [ "$JAVA_HOME" != "" ] && [ -d "$JAVA_HOME" ]; then
188      HINT=$JAVA_HOME
189      elif [ ! -d "$HINT" ]; then
190      HINT=`/usr/libexec/java_home`
191          # old code used as fallback:
192      if [ ! -d "$HINT" ]; then
193              HINT=/System/Library/Frameworks/JavaVM.framework/Home
194      fi
195      fi
196  fi
197
198  if [ "$DEBUG" == "true" ]; then echo "**********************************************"; fi
199
200  # If the file utility exists, use it to determine the bitness of this GS3,
201  # particularly of this GS3's lib\jni\libgdbmjava.so (gdbmjava.dll), since we prefer a matching java
202  # If any executable doesn't exist, the return value is 127.
203  # If file utility exists, then 0 is returned on successful execution, 1 is an error exit code
204  # Running file without arg returns 1 therefore.
205
206  # Determine the bitness of this GS3 installation, by running:
207  # `file lib/jni/libgdbmjava.so`
208  # Output:
209  #    lib/jni/libgdbmjava.so: ELF 64-bit LSB  shared object, x86-64, version 1 (SYSV),
210  #    dynamically linked, BuildID[sha1]=5ae42cf69275408bdce97697d69e9e6fd481420d, not stripped
211  # On 32-bit linux, the output will contain "lib/jni/libgdbmjava.so: ELF 32-bit ..."
212  # Check output string contains bitness: http://stackoverflow.com/questions/229551/string-contains-in-bash
213
214  fileexists=`file 2&> /dev/null`
215
216  # Integer comparison, http://tldp.org/LDP/abs/html/comparison-ops.html
217  #    can also use double brackets:  if [[ $? > 1 ]]; then ...
218  if [ "$?" -gt "1" ]; then
219      if [ "$DEBUG" == "true" ]; then
220      echo "    'file' utility not found installed on this unix-based system."
221      echo "    Unable to use 'file' utility to determine bitness of this GS3 to see if it matches that of any Java found."
222      fi
223      bitness=-1
224  elif [ ! -f "$GSDL3SRCHOME/lib/jni/libgdbmjava.so" ]; then
225      # the file we want to test the bitness of, to determine GS3's bitness by, doesn't exist yet
226      bitness=-1
227  else
228      bitness=`file $GSDL3SRCHOME/lib/jni/libgdbmjava.so`
229      if [[ $bitness == *"64-bit"* ]]; then
230      bitness=64
231      echo "The installed Greenstone is $bitness bit"
232      elif [[ $bitness == *"32-bit"* ]]; then
233      bitness=32
234      echo "The installed Greenstone is $bitness bit"
235      else
236      bitness=-1
237      echo "WARNING: Greenstone installation is of unknown bitness. \"$bitness\" is neither 32 nor 64 bit"
238      fi
239  fi
240
241  # If search4j is present, use it to locate a java.
242  # If search4j finds a Java, then:
243  # - If its bitness doesn't match and there's a bundled jre, use the bundled jre instead.
244  # - If its bitness doesn't match and there's no bundled jre, use the java found by search4j anyway,
245  # we'll print a warning about this bitness mismatch at the end
246
247  javaset=false
248  if [ -x bin/search4j ] ; then
249      FOUNDJAVAHOME="`bin/search4j -d -p \"$HINT\" -m $java_min_version`"
250      javahome_retval=$?
251      FOUNDJREHOME="`bin/search4j -r -p \"$HINT\" -m $java_min_version`"
252      jrehome_retval=$?
253  fi
254
255  # 1. check the bitness of any JDK java found by search4j, and use if appropriate
256  if [ "$javahome_retval" == "0" ]; then
257      setJavaIfOK "$DEBUG" "$bitness" "$FOUNDJAVAHOME" "JDK"
258      if [ "$?" == "0" ]; then javaset="true"; fi
259  fi
260
261  # 2. check the bitness of any JRE java found by search4j, and use if appropriate
262  if [ "$javaset" != "true" ] && [ "$jrehome_retval" == "0" ]; then
263      setJavaIfOK "$DEBUG" "$bitness" "$FOUNDJREHOME" "JRE"
264      if [ "$?" == "0" ]; then javaset="true"; fi
265  fi
266 
267  # 3. check the bitness of any bundled JRE, and use if appropriate
268  # For linux, the bundled JRE ought to be of a bitness matching this OS.
269  if [ "$javaset" != "true" ] && [ -d "$BUNDLED_JRE" ]; then
270      setJavaIfOK "$DEBUG" "$bitness" "$BUNDLED_JRE" "bundled JRE"
271      if [ "$?" == "0" ]; then javaset="true"; fi
272  fi
273
274
275  # 4. None of the java found so far (via search4j, bundled_jre), if any, may have matched bitness wise
276  # So, fall back to using whichever is available in sequence anyway.
277  # We'll print a warning of bitness mismatch later
278
279  if [ "$javaset" != "true" ]; then
280      # go with any JAVA_HOME else JRE_HOME that search4j found, else with any bundled JRE if present
281      if [ "$javahome_retval" == "0" ]; then
282      setupJavaAt "$FOUNDJAVAHOME" "JDK"
283      javaset=true
284      elif [ "$jrehome_retval" == "0" ]; then
285      setupJavaAt "$FOUNDJREHOME" "JRE"
286      javaset=true
287      elif [ -d "$BUNDLED_JRE" ]; then
288      # bundled JRE should be >= than minimum version of java required
289      setupJavaAt "$BUNDLED_JRE" "JRE"
290      javaset=true
291      fi
292  fi
293
294  # 5. lastly, manually check if java already setup. Could be the case if search4j didn't exist
295  if [ "$javaset" != "true" ]; then
296
297    if [ -x bin/search4j ]; then
298
299      # no suitable java could be found by search4j
300      echo "  - ERROR: Failed to locate java $java_min_version or greater"   
301      echo "           Please set JAVA_HOME or JRE_HOME to point to an appropriate java"
302      echo "           And add JAVA_HOME/bin or JRE_HOME/bin to your PATH"
303
304    else
305      # search4j wasn't present, and no bundled JRE, so check JAVA_HOME or JRE_HOME manually
306      echo "*** Could not find an appropriate JDK or JRE java"
307      echo "*** Attempting to use JAVA_HOME else JRE_HOME in the environment"
308
309      if [ "$JAVA_HOME" != "" ] && [ "`which java`" == "$JAVA_HOME/bin/java" ]; then
310      echo "  - Using java at $JAVA_HOME"
311      echo "  - WARNING: Greenstone has not checked the version number of this java installation"
312      echo "             The source distribution of Greenstone3 requires java 1.5 or greater"
313      echo "             (SVN users may still use java 1.4)"
314      elif [ "$JRE_HOME" != "" ] && [ "`which java`" == "$JRE_HOME/bin/java" ]; then
315      echo "  - Using java at $JRE_HOME"
316      echo "  - WARNING: Greenstone has not checked the version number of this java installation"
317      echo "             The source distribution of Greenstone3 requires java 1.5 or greater"
318      echo "             (SVN users may still use java 1.4)"
319     
320      #failing all that, print a warning
321      else
322          #no suitable java exists
323      echo "  - ERROR: Failed to locate java"
324      echo "           Please set JAVA_HOME or JRE_HOME to point to an appropriate java"
325      echo "           And add JAVA_HOME/bin or JRE_HOME/bin to your PATH"
326      return
327      fi
328    fi
329  fi
330
331  # If we know the bitness of this GS3 installation, then warn if there's a mismatch
332  # with the bitness of the Java found
333
334  if [ "$bitness" != "-1" ]; then
335      if [ "$JAVA_HOME" != "" ]; then
336      JAVA_FOUND=$JAVA_HOME
337      elif [ "$JRE_HOME" != "" ]; then
338      JAVA_FOUND=$JRE_HOME
339      fi
340      checkJavaBitnessAgainstGSBitness "$JAVA_FOUND" "$bitness"
341      if [ "$?" == "1" ]; then
342      echo "*** WARNING: Detected mismatch between the bit-ness of your GS installation ($bitness bit)"
343      echo "*** and the Java found at $JAVA_FOUND/bin/java"
344      echo "*** Continuing with this Java anyway:"
345      echo "*** This will only affect MG/MGPP collections for searching, and GDBM database collections"
346      echo "*** Else set JAVA_HOME or JRE_HOME to point to an appropriate $bitness bit Java"
347      echo "*** Or recompile GS with your system Java:"
348      if [ "$JAVA_HOME" != "" ]; then
349          echo "*** JAVA_HOME at $JAVA_HOME"
350      else
351          echo "*** JRE_HOME at $JRE_HOME"
352      fi
353      fi
354  fi
355
356  if [ "$DEBUG" == "true" ]; then echo "**********************************************"; fi
357
358}
359
360# http://www.linuxjournal.com/content/return-values-bash-functions
361function setJavaIfOK {
362
363    if [ "$DEBUG" == "true" ]; then echo "Testing java at $3"; fi
364
365    DEBUG=$1
366    bitness=$2
367    PATHTOJAVA=$3
368    JDKorJRE=$4
369
370    checkJavaBitnessAgainstGSBitness "$PATHTOJAVA" "$bitness"
371   
372    isjavaset=false
373
374    if [ "$?" == "0" ]; then
375    # http://tldp.org/LDP/abs/html/comparison-ops.html
376    if [ "$bitness" != "-1" ] && [ "$DEBUG" == "true" ]; then
377        # java matches GS bitness
378        if [[ "$JDKorJRE" == *"bundled"* ]]; then
379        echo "*** Changing to use Greenstone's $bitness-bit $JDKorJRE at $PATHTOJAVA"
380        else
381        echo "    The detected $JDKorJRE at $PATHTOJAVA is a matching $bitness bit"
382        fi
383    fi
384    setupJavaAt "$PATHTOJAVA" "$JDKorJRE"
385    isjavaset=true
386   
387    elif [ "$bitness" != "-1" ] && [ "$DEBUG" == "true" ]; then
388    if [[ "$JDKorJRE" == *"bundled"* ]]; then
389        echo "    The $JDKorJRE java is an incompatible bit architecture"
390    else
391        echo "    The detected $JDKorJRE java is an incompatible bit architecture"
392    fi
393    fi
394   
395    if [ "$isjavaset" == "true" ]; then
396    return 0 # success
397    else
398    return 1
399    fi
400}
401
402# if bitness (parameter #2) is -1, then this function returns 0 (generally meaning success).
403function checkJavaBitnessAgainstGSBitness() {
404#    if [ "$DEBUG" == "true" ]; then echo "Testing bitness of java found at $java_installation"; fi
405    java_installation="$1"
406    bitness="$2"
407
408    # bitness can be -1 if the 'file' utility could not be found to determine bitness
409    # or if its output no longer prints "32-bit" or "64-bit". Should continue gracefully
410    if [ "$bitness" == "-1" ]; then
411    return 0
412    fi
413
414    # now we can actually work out if the java install's bitness matches that of GS ($bitness)
415    # java -d32 -version should return 0 if the Java is 32 bit, and 1 (failure) if the Java is 64 bit.
416    # Likewise, java -d64 -version will return 0 if the Java is 64 bit, and 1 (failure) if the Java is 32 bit.
417    `$java_installation/bin/java -d$bitness -version 2> /dev/null`
418
419    if [ "$?" == "0" ]; then
420    return 0
421    elif [ "$?" == "1" ]; then
422    return 1
423    else
424    echo "*** Problem determining bitness of java using java at $java_installation"
425    return $?
426    fi
427}
428
429
430function setupJavaAt() {
431
432  # check the second parameter if non-null
433  if [ -n "$2" ] && [ "$2" == "JRE" ]; then
434    export JRE_HOME="$1"
435    addtopath PATH "$JRE_HOME/bin"
436
437    BUNDLED_JRE="`pwd`/packages/jre"   
438    if [[ "$JRE_HOME" == *"$BUNDLED_JRE"* ]]; then
439    msg="the bundled"
440    fi
441
442    echo "  - Exported JRE_HOME to $msg $JRE_HOME"
443  else
444    export JAVA_HOME="$1"
445    addtopath PATH "$JAVA_HOME/bin"
446    echo "  - Exported JAVA_HOME to $JAVA_HOME"
447  fi
448
449 
450}
451
452function pauseAndExit(){
453 echo -n "Please press any key to continue... "
454 read
455}
456
457function isinpath() {
458  for file in `echo $1 | sed 's/:/ /g'`; do
459    if [ "$file" == "$2" ]; then
460      echo true
461      return
462    fi
463  done
464  echo false
465}
466
467function addtopath() {
468  eval "PV=\$$1"
469  #echo "$1 += $2"
470  if [ "$PV" == "" ]; then
471    cmd="$1=\"$2\""
472  else
473    cmd="$1=\"$2:\$$1\""
474  fi
475  eval $cmd
476  eval "export $1"
477}
478
479# Note: use return not exit from a sourced script otherwise it kills the shell
480echo
481testSource
482if [ "$?" == "1" ]; then
483return
484fi
485testAlreadySourced
486if [ "$?" == "1" ]; then
487return
488fi
489setGS3ENV
490
491if test -e gs2build/setup.bash ; then
492  echo ""
493  echo "Sourcing gs2build/setup.bash"
494  cd gs2build ; source setup.bash ; cd ..
495fi
496
497
498if test "x$gsopt_noexts" != "x1" ; then
499    if test -e ext ; then
500    for gsdl_ext in ext/* ; do
501        if [ -d $gsdl_ext ] ; then
502        cd $gsdl_ext > /dev/null
503        if test -e gs3-setup.sh ; then
504            source ./gs3-setup.sh
505        elif test -e setup.bash ; then
506            source ./setup.bash
507        fi
508        cd ../.. 
509        fi
510    done
511    fi
512fi
513
514if test -e local ; then
515  if test -e local/gs3-setup.sh ; then
516    echo ""
517    echo "Sourcing local/gs3-setup.sh"
518    cd local ; source gs3-setup.sh ; cd ..
519  fi
520fi
521
522
523checkJava
524echo ""
Note: See TracBrowser for help on using the browser.