source: main/trunk/greenstone3/gs3-setup.sh@ 34317

Last change on this file since 34317 was 34317, checked in by davidb, 4 years ago

Fixed silly typo in test for existance of

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