43647578fe5145454c6f9349587f86409f29dc72
[scilab.git] / scilab / bin / scilab
1 #!/bin/sh
2 #
3 # Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 # Copyright (C) INRIA - 2006-2008 - Sylvestre Ledru
5 # Copyright (C) INRIA - 2008 - Simon Lipp
6 # Copyright (C) DIGITEO - 2009-2011 - Sylvestre Ledru
7 # Copyright (C) DIGITEO - 2010-2011 - Bruno JOFRET
8 # This file must be used under the terms of the CeCILL.
9 # This source file is licensed as described in the file COPYING, which
10 # you should have received as part of this distribution.  The terms
11 # are also available at
12 # http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13
14 ####
15 #
16 # Scilab can be installed/used in different ways :
17 # * ./configure; make install
18 #    => will install Scilab into /usr/local/ and split files to
19 #    /usr/local/bin, /usr/local/share/scilab/, /usr/local/lib/scilab/,
20 #    /usr/local/include/scilab/
21 # * ./configure --prefix=/my/dir/; make install
22 #    => will install Scilab into /my/dir/ and split files to
23 #    /my/dir/bin/, /my/dir/share/scilab/, /my/dir/lib/scilab/
24 #    /my/dir/include/scilab/
25 #    Scilab official binary uses this arch
26 # * ./configure; make install prefix=$(DESTDIR)/usr \
27 #                   bindir=$(DESTDIR)/usr/bin \
28 #                   datadir=$(DESTDIR)/usr/share/scilab ...
29 #    Solution used mainly by Linux packagers
30 #
31 ## First, this script will look for scilab-bin in :
32 # * If specified, in the $SCI variable
33 # * Into the scilab/bin/ directory
34 # * Into the scilab/ directory (if the user is using a source version and
35 # scilab has been compiled in this directory)
36 # * if this file is a symlink to the real one. We browse symlink(s) until we
37 # find the real "scilab" script file and consider that it is the right
38 # directory
39 # * That is all. Don't hesitate to contact us if you see one missing.
40 #
41 ###
42
43 if test ! -z "$SCIVERBOSE"; then
44     SCIVERBOSE=1
45 else
46     SCIVERBOSE=0
47 fi
48
49 if test -n "$SCI"; then
50     if test ! -d "$SCI/modules"; then
51         echo "Cannot find directory $SCI/modules/ !"
52         echo "SCI should point to the root data directory of Scilab"
53         exit -1
54     fi
55 fi
56
57 DISABLE_JAVA_DETECTION=0
58 IS_SCILAB_BINARY=0
59
60 # Electric fence is a tool to check problem of memory
61 start_electric_fence() {
62     echo "Running Scilab with Electric Fence : "  LD_PRELOAD=libefence.so.0.0 "$SCILABBIN" "$@"
63     LD_PRELOAD=libefence.so.0.0 "$SCILABBIN" "$@"
64 }
65
66 start_debug() {
67     if test -n "$DEVENV"; then
68         echo "Running debug of Scilab [gdb]  : " "$SCI"/libtool --mode=execute gdb $SCILAB_GDB_OPT --args "$SCILABBIN" "$@"
69         "$SCI"/libtool --mode=execute gdb $SCILAB_GDB_OPT --args "$SCILABBIN" "$@"
70     else
71         echo "Running debug of Scilab [gdb]  : " gdb $SCILAB_GDB_OPT --args "$SCILABBIN" "$@"
72         gdb $SCILAB_GDB_OPT --args "$SCILABBIN" "$@"
73     fi
74 }
75
76 start_debug_kdbg() {
77     if test -n "$DEVENV"; then
78         echo "Running debug of Scilab [kdbg]  : " "$SCI"/libtool --mode=execute kdbg "$SCILABBIN"
79         "$SCI"/libtool --mode=execute kdbg "$SCILABBIN"
80     else
81         echo "Running debug of Scilab [kdbg]  : " kdbg "$SCILABBIN"
82         kdbg "$SCILABBIN"
83     fi
84 }
85
86 start_profiling() {
87     VALGRIND_OPTIONS=" --show-below-main=yes --num-callers=12 --demangle=yes --leak-check=full --show-reachable=yes --smc-check=all --db-attach=yes --gen-suppressions=all -v --trace-redir=yes --show-below-main=yes --track-origins=yes --suppressions=$SCI/tools/profiling/valgrind.supp $SCILAB_VALGRIND_OPT"
88     if test -n "$DEVENV"; then
89         echo "Running profiling of Scilab  : " "$SCI"/libtool --mode=execute valgrind $VALGRIND_OPTIONS "$SCILABBIN" "$@"
90         "$SCI"/libtool --mode=execute valgrind $VALGRIND_OPTIONS "$SCILABBIN" "$@"
91     else
92         echo "Running profiling of Scilab  : " valgrind $VALGRIND_OPTIONS "$SCILABBIN" "$@"
93         valgrind $VALGRIND_OPTIONS "$SCILABBIN" "$@"
94     fi
95
96 }
97
98 start_profiling_visu() {
99     VALGRIND_VISU_OPTIONS=" --tool=callgrind --trace-children=yes --smc-check=all --trace-jump=yes --dump-line=yes --separate-threads=yes $SCILAB_VALGRIND_OPT"
100 #--dump-instr=yes
101     if test -n "$DEVENV"; then
102         echo "Running profiling visualization of Scilab  : " "$SCI"/libtool --mode=execute valgrind $VALGRIND_VISU_OPTIONS "$SCILABBIN" "$@"
103         "$SCI"/libtool --mode=execute valgrind $VALGRIND_VISU_OPTIONS "$SCILABBIN" "$@"
104     else
105         echo "Running profiling visualization of Scilab  : " valgrind $VALGRIND_VISU_OPTIONS "$SCILABBIN" "$@"
106         valgrind $VALGRIND_VISU_OPTIONS "$SCILABBIN" "$@"
107     fi
108 }
109
110 find_docbook_path() {
111     if test -z "$DOCBOOK_ROOT"; then
112         for dir in $SCI/thirdparty/docbook /usr/share/sgml/docbook/stylesheet/xsl/nwalsh /usr/share/docbook2X/xslt/man/ /usr/share/xml/docbook/stylesheet/nwalsh/ /usr/share/xml/docbook/stylesheet/nwalsh/current/ /sw/share/xml/xsl/docbook-xsl /usr/share/xml/docbook/xsl-stylesheets-*/ /usr/share/sgml/docbook/xsl-stylesheets-*/; do
113             if test -r "$dir/javahelp/javahelp.xsl"; then
114                 if test -z "$DOCBOOK_ROOT"; then
115                     DOCBOOK_ROOT=$dir
116                     export DOCBOOK_ROOT
117                 fi
118             fi
119         done
120     fi
121     if test $SCIVERBOSE -ne 0; then
122         echo "DOCBOOK : $DOCBOOK_ROOT"
123     fi
124 }
125
126 detect_type_of_scilab(){
127
128     progname="$0"
129     SCILAB_MODE="gui"
130     SCILABBIN="scilab-bin"
131
132     if (echo "$progname"|grep "scilab-adv-cli$" 2>&1 > /dev/null;) || (echo "$@"|grep "\-nw" 2>&1 > /dev/null;) then
133         SCILAB_MODE="nw"
134         # We are using scilab-bin here because we are going to force the arg -nw
135         SCILABBIN="scilab-bin"
136     fi
137
138     if (echo "$progname"|grep "scilab-cli$" 2>&1 > /dev/null;) || (echo "$@"|grep "\-nwni" 2>&1 > /dev/null;) then
139         SCILAB_MODE="nwni"
140         SCILABBIN="scilab-cli-bin"
141         DISABLE_JAVA_DETECTION=1
142     fi
143
144     if test $SCIVERBOSE -ne 0; then
145         echo "SCILABBIN : $SCILABBIN"
146         echo "DISABLE_JAVA_DETECTION : $DISABLE_JAVA_DETECTION"
147         echo "SCILAB_MODE : $SCILAB_MODE"
148     fi
149 }
150
151 detect_os() {
152
153     if test "${OS}" = ""; then
154         OS=$(uname -s) # Retrieve the Operating System
155     fi
156     if test "${MODEL}" = ""; then
157         MODEL=$(uname -m) # Retrieve the model
158     fi
159
160     if test $SCIVERBOSE -ne 0; then
161         echo "OS : $OS"
162         echo "MODEL : $MODEL"
163     fi
164 }
165
166 detect_type_of_scilab $@
167
168 detect_os
169
170 detect_scilab_path() {
171
172     LOCALOLDPWD=$(pwd)
173
174     # If we are running Scilab binary, it is trivial to set paths.
175     curdir=$(dirname "$progname")
176
177     # If the script is a symlink, it might be a symlink to the Scilab binary
178     # We could run into the bug #9125
179     if test -h $progname; then
180         while test -h "$progname"; do
181             bn=$(basename "$progname")
182             cd $(dirname "$progname")
183
184             # Browse the symlink to the real one
185             progname=$(/bin/ls -l "$bn" | sed -e 's/^.* -> //' )
186
187             if test ! -x "$progname"; then
188                 break
189             fi
190         done
191         cd $(dirname "$progname")
192         curdir=$(pwd)
193     fi
194
195     if test -d $curdir/../thirdparty/ -a -d $curdir/../share/scilab; then
196         # We are in the Scilab binary
197         IS_SCILAB_BINARY=1
198         # export the bin directory to the PATH first (avoid to take the packaged version)
199         PATH="$curdir:$PATH"
200         cd "$curdir/../"
201         SCIBINARYBASE=$(pwd)
202         SCI=$SCIBINARYBASE/share/scilab
203         export SCI
204         return
205     fi
206
207     # Use scilab-bin in the current dir if it exists
208     # If not, then start resolving symlinks until we find scilab-bin
209     found=0
210     curdir=$(dirname "$progname")
211     PATH="$curdir:$PATH"
212
213     progbase=$(basename "$progname")
214     run_sci="$curdir/$SCILABBIN"
215     if test -x "$run_sci"; then # scilab-bin is in the /bin/ directory
216         cd "$curdir/../"
217         SCI=$(pwd)
218         SCIBIN=$run_sci
219         found=1
220     else
221         run_sci="$curdir/../$SCILABBIN"
222         if test -x "$run_sci"; then # scilab-bin is in the / directory
223         # which is the case in a dev env
224             cd "$curdir/../"
225             SCI=$(pwd)
226             SCIBIN=$run_sci
227             found=1
228         else
229             here=$(/bin/pwd)
230             while test -h "$progname"; do
231                 bn=$(basename "$progname")
232                 cd $(dirname "$progname")
233             # Browse the symlink to the real one
234                 progname=$(/bin/ls -l "$bn" | sed -e 's/^.* -> //' )
235                 if test ! -x "$progname"; then
236                     break
237                 fi
238                 curdir=$(dirname "$progname")
239                 if test -x "$curdir"; then
240                     cd "$curdir/../"
241                     SCI=$(pwd)
242                     SCIBIN="$curdir/$SCILABBIN"
243                     found=1
244
245                     break
246                 fi
247             # The current file ($0), probably scilab is a symlink to the
248             # real one and here, it is the dev env
249                 run_sci="$curdir/../$SCILABBIN"
250                 if test -x "$run_sci"; then
251                     cd "$curdir/../"
252                     SCI=$(pwd)
253                     SCIBIN=$run_sci
254                     found=1
255                     break
256                 fi
257             done
258             cd "$here"
259         fi
260         if test $SCIVERBOSE -ne 0; then
261             echo "I think I found Scilab here : $SCI"
262         fi
263     fi
264
265
266     if test -x "$SCI/bin/$SCILABBIN"; then
267 # real env
268         SCILABBIN=$SCI/bin/$SCILABBIN
269         SCI=$SCI/share/scilab/
270     else
271         if test -x "$SCI/$SCILABBIN"; then
272         # dev env
273             SCILABBIN=$SCI/$SCILABBIN
274             DEVENV=1
275         # add modelica in the path
276             PATH="$PATH:$SCI/modules/scicos/"
277         else
278             echo "Cannot find $SCILABBIN ..."
279             exit 1
280         fi
281     fi
282
283     export SCI
284 }
285
286
287 set_macosx_java_vm () {
288     # For Mac OS X, Since path are different from the JVM normal tree,
289     # don't need to detect the CPU
290     JAVA_SHLIB="libjava.jnilib"
291     JAVA_HOME=$(/usr/libexec/java_home --arch x86_64 --failfast --version 1.6+)
292     DYLD_LIBRARY_PATH="$JAVA_HOME/../Libraries${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}"
293
294 }
295
296 set_embedded_java_vm () {
297
298        # Scilab binary => no Java detection since we are using the embeded JRE
299         # Here, we are handling 3 cases: GNU/Linux 32bit, GNU/Linux 64bit,
300         # Mac OS X
301         case $OS in
302             "Linux")
303                 case $MODEL in
304                     "x86_64")
305                         proc="amd64"
306                         ;;
307                     i?86)
308                         # We embed java/lib/i386 with Scilab Binary
309                         # force proc to i386 even if systems answer i686
310                         proc="i386"
311                         ;;
312                     *)
313                         echo "Could not find the Java configuration for the model <${MODEL}>. Please contact us on http://bugzilla.scilab.org"
314                         ;;
315                 esac
316                 ;;
317             *darwin* | *Darwin*) # MacOS X
318                 set_macosx_java_vm
319                 ;;
320             *)
321             echo "Could not find the Java configuration for the OS <${OS}>. Please contact us or submit a bug report with your detailed configuration http://bugzilla.scilab.org/"
322             echo "Fallback on the model <${MODEL}> found by uname -m"
323             proc=$MODEL
324             ;;
325     esac
326     JRE_HOME="$SCIBINARYBASE/thirdparty/java/"
327
328 }
329
330 # If enabled, disable se_linux
331 check_and_disable_selinux() {
332     if test -x /usr/sbin/selinuxenabled -a $IS_SCILAB_BINARY -eq 1; then
333         /usr/sbin/selinuxenabled
334         if test $? = 0; then
335             if test $SCIVERBOSE -ne 0; then
336                 echo "Disabling SELinux on Scilab"
337             fi
338             
339             JVMLIB_32=$SCIBINARYBASE/java/lib/i386/server/libjvm.so
340             if test -f $JVMLIB_32; then
341                 /usr/bin/chcon -t textrel_shlib_t "$JVMLIB_32"
342                 if test $? != 0; then
343                     echo "Error: Cannot chcon $JVMLIB_32"
344                 fi
345             fi
346             JVMLIB_64=$SCIBINARYBASE/java/lib/amd64/server/libjvm.so
347             if test -f $JVMLIB_64; then
348                 /usr/bin/chcon -t textrel_shlib_t "$JVMLIB_64"
349                 if test $? != 0; then
350                     echo "Error: Cannot chcon $JVMLIB_64"
351                 fi
352             fi
353             /usr/bin/chcon -t execmem_exec_t "$SCIBINARYBASE/bin/scilab-bin"
354             if test $? != 0; then
355                 echo "Error: Cannot chcon 'scilab-bin'"
356             fi
357
358             /usr/bin/chcon -t execmem_exec_t "$SCIBINARYBASE/bin/scilab-cli-bin"
359             if test $? != 0; then
360                 echo "Error: Cannot chcon 'scilab-cli-bin'"
361             fi
362
363         fi
364     fi
365 }
366
367 detect_java_vm() {
368
369     JAVA_SHLIB="libjava.so"
370
371     if test $IS_SCILAB_BINARY -eq 1; then
372         set_embedded_java_vm
373     else  # NOT IS_SCILAB_BINARY
374
375 ############# JAVA DETECTION ##############
376 # Basic inspiration from Eclipse
377 #
378 # Scilab is looking for Java in various places with different ways :
379 # * if JAVA_HOME is specificed, use it
380 # * Check in 'standard' places we know Java could be installed
381 # if some are missing, please contact us
382 # * look in the PATH if there is a java binary
383 # if it is the case, resolve the symlink and set the JAVA_HOME from it
384 #
385 # Once we have JAVA_HOME, we check that native libraries (libjava.so and
386 # 2 others) are available
387 ###
388
389 # Get the proc name matching to the one of the JVM
390 # Inspiration and data are from http://www.google.com/codesearch?hl=en&q=+java+ppc+libjava.so+show:a78sz0SP41k:zxNozD-vc0A:FWiIy7Hinmg&sa=N&cd=2&ct=rc&cs_p=http://fresh.t-systems-sfr.com/unix/src/misc/tclBlendSrc1.2.5.tar.gz&cs_f=tclBlend1.2.5/configure.in#a0
391 # For Mac OS X, we will get stuff like that:
392 # uname -s Power Macintosh
393 # uname -m Darwin
394
395 # If the user has the variable JAVA_HOME in the ENV
396         if test -n "$JAVA_HOME" ; then
397                 if test $SCIVERBOSE -ne 0; then
398                     echo "Using specified vm: $JAVA_HOME"
399                 fi
400                 if test ! -x "$JAVA_HOME/bin/java" ; then
401                     echo "Cannot find $JAVA_HOME/bin/java"
402                     exit 1
403                 fi
404         else
405
406             case $OS in
407                 *darwin* | *Darwin*)
408                     set_macosx_java_vm
409                     if test $SCIVERBOSE -ne 0; then
410                         echo "Using default Mac OS X vm: $JAVA_HOME"
411                         echo "And LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
412                         echo "And DYLD_LIBRARY_PATH: $DYLD_LIBRARY_PATH"
413                     fi
414                     ;;
415                 *)
416                     ;;
417             esac
418
419 # Browse "default directory" (if it means anything under Unix/Linux) of Java
420 # to find where it could be !
421             if test -z "$JAVA_HOME"; then
422                 JAVA_DEFAULT_DIR="/usr/lib/jvm/default-java /usr/lib/jvm/java-6-openjdk/ /usr/lib/jvm/java-7-openjdk/ /usr/lib/jvm/java/ /usr/lib/jvm/java-6-sun/ /usr/lib/j2se/1.6 /usr/java/jdk1.6.0*/ /usr/java/jdk1.5.0*/ /usr/lib/j2sdk1.6-ibm /usr/lib/jvm/java-1.5.0-sun /usr/java/ /usr/lib/j2se/1.5 /usr/lib/j2se/1.4 /usr/java/jre1.6.0*/ /usr/java/jre1.5.0*/ /usr/lib/j2sdk1.5-ibm /usr/lib/j2sdk1.4-ibm /usr/lib/j2sdk1.5-sun /usr/lib/j2sdk1.4-sun /usr/lib/jvm/java-gcj /usr/lib/jvm/java-1.5.0-gcj/ /usr/lib/kaffe/pthreads /usr/lib/jvm/java-1.7.0-icedtea-*/ /usr/lib/jvm/jre/ /usr/local/diablo-jdk1.6.0/"
423                 for DIR in $JAVA_DEFAULT_DIR ; do
424                     if test $SCIVERBOSE -ne 0; then
425                         echo "Trying to find Java in $DIR "
426                     fi
427                     if test -x "${DIR}/bin/java" ; then
428                         # resolve symlinked dir
429                         cd $DIR
430                         JAVA_HOME=$(pwd -P)
431                         cd $OLDPWD
432
433                         export JAVA_HOME
434                         if test $SCIVERBOSE -ne 0; then
435                             echo "Java found in $JAVA_HOME"
436                         fi
437                         break
438                     fi
439                 done
440             fi
441
442 # if JAVA_HOME still haven't been found. Well, we can now try to find it in the path
443             if test ! -d "$JAVA_HOME"; then
444
445                 PATH_SEPARATOR=:
446                 as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
447                 if test $SCIVERBOSE -ne 0; then
448                     echo "Look for the java binary in the PATH. If it is a symlink, try to resolve it"
449                 fi
450                 for DIR in $PATH; do
451                     if test -f $DIR/java; then
452
453                         _cur=$DIR/java
454                 # resolve symlink
455                 # Will probably bug if a file contains -> in the name
456                         while ls -ld "$_cur" 2>/dev/null | grep " -> " >/dev/null; do
457                             _cur=$(ls -ld "$_cur" | sed 's/.* -> //')
458                         done
459                 # symlink resolved to the real file
460
461                 # Now, create JAVA_HOME
462                         TMP=$(dirname $_cur)
463                         JAVA_HOME=$(dirname $TMP)
464                         if test ! -z "$JAVA_HOME"; then
465                             break;
466                         fi
467                     fi
468                 done
469                 IFS=$as_save_IFS
470             fi
471         fi
472         if test ! -z "$JAVA_HOME"; then
473             if test $SCIVERBOSE -ne 0; then
474                 echo "JAVA_HOME : $JAVA_HOME"
475             fi
476 # Manage JRE & JDK because libjava.so and others are stored in the JRE
477             if test -d $JAVA_HOME/jre; then
478                 JRE_HOME=$JAVA_HOME/jre
479             else
480                 JRE_HOME=$JAVA_HOME
481             fi
482 # Configuring LD_LIBRARY_PATH for libjava.so libhpi.so and libxxxx.so
483
484             case $OS in
485                 "Linux")
486                     case $MODEL in
487                         "x86_64")
488                             proc="amd64"
489                             ;;
490                         i?86)
491
492                             if test -d ${JRE_HOME}/lib/$(uname -m) ; then
493                                 proc=$(uname -m)
494                             else
495                                 proc="i386"
496                             fi
497                             ;;
498                         "alpha")
499                             proc="alpha"
500                             ;;
501                         "sparc"|"sparc64")
502                             proc="sparc"
503                             ;;
504                         sh*)
505                             proc="sh"
506                             ;;
507                         "ia64")
508                             proc="ia64"
509                             ;;
510                         mips*) # under mipsel, uname -m is
511                     # usually returning mips or mips64...
512                     # but binary are in mipsel
513                             if test -d ${JRE_HOME}/lib/mipsel ; then
514                                 proc="mipsel"
515                             else
516                                 proc="mips"
517                             fi
518                             ;;
519                         "ppc"|"powerpc"|"ppc64")
520                             proc="ppc"
521                             ;;
522                         "s390")
523                             proc="s390"
524                             ;;
525                         "s390x")
526                             proc="s390x"
527                             ;;
528                         armv*)
529                             proc="arm"
530                             ;;
531                         *)
532                             echo "Could not find the Java configuration for the model <${MODEL}>. Please contact us on http://bugzilla.scilab.org"
533                             ;;
534                     esac
535                     ;;
536                 "HP-UX")
537                     JAVA_SHLIB="libjava.sl"
538                     proc="lib" # TODO : check if it is right or not ... seems not !
539                     ;;
540                 "IRIX")
541                     proc="lib32"
542                     ;;
543                 "SunOS")
544                     case $MODEL in
545                         "i86pc") # solaris x86
546                             proc="i386"
547                             ;;
548                 # check for the 64 bits syntax
549                         "sparc"|"sparc64")
550                             proc="sparc"
551                             ;;
552                         *)
553                             echo "Could not find the Java configuration for the model <${MODEL}>. Please contact us."
554                             ;;
555                     esac
556                     ;;
557
558                 *win32* | *WIN32* | *CYGWIN_NT*)
559                     proc="win32"
560                     JAVA_SHLIB="jvm.dll"
561                     ;;
562                 *darwin* | *Darwin*) # MacOS X
563                     set_macosx_java_vm
564                     ;;
565                 *FreeBSD*)
566                     proc=$MODEL
567                     ;;
568                 *)
569                     echo "Could not find the Java configuration for the OS <${OS}>. Please contact us or submit a bug report with your detailed configuration http://bugzilla.scilab.org/"
570                     echo "Fallback on the model <${MODEL}> found by uname -m"
571                     proc=$MODEL
572                     ;;
573             esac
574         fi # JAVA_HOME provided or found
575     fi # IS_SCILAB_BINARY
576
577 # Check if the lib exists or not
578     LIBJAVA="$JRE_HOME/lib/$proc/$JAVA_SHLIB"
579     if test ! -f $LIBJAVA; then
580         if test $SCIVERBOSE -ne 0; then
581             echo "Cannot find $LIBJAVA"
582         fi
583     else
584         CLASSPATH="${CLASSPATH:+:$CLASSPATH}$SCI/modules/core/jar/org.scilab.modules.core.jar"
585         # @TODO This is the default path of the official sun JVM.
586         # It is different for other implementation of the JVM....
587         LD_LIBRARY_PATH="$JRE_HOME/lib/$proc/:$JRE_HOME/lib/$proc/server/:$JRE_HOME/lib/$proc/native_threads/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
588     fi
589
590 ####### END OF JAVA ENV DETECTION/SETTING ######
591 }
592
593 detect_scilab_path
594 if test $SCIVERBOSE -ne 0; then
595     echo "IS_SCILAB_BINARY : $IS_SCILAB_BINARY"
596 fi
597
598
599 # If we are calling scilab-cli, no need to detect java.
600 if test $DISABLE_JAVA_DETECTION -ne 1; then
601     detect_java_vm
602 fi
603
604 if test $IS_SCILAB_BINARY -eq 1; then
605
606     TCL_LIBRARY="$SCIBINARYBASE/thirdparty/tcl8.5"
607     Tk_LIBRARY="$SCIBINARYBASE/thirdparty/tk8.5"
608     LD_LIBRARY_PATH=$SCIBINARYBASE/lib/scilab:$SCIBINARYBASE/lib/thirdparty${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
609     DOCBOOK_ROOT="$SCI/../../thirdparty/docbook"
610     PATH="$PATH:$SCI/modules/scicos/"
611
612     export DOCBOOK_ROOT
613     export PATH
614     export LD_LIBRARY_PATH
615     export TCL_LIBRARY
616     export Tk_LIBRARY
617
618     case $OS in
619         *darwin* | *Darwin*)
620             DYLD_LIBRARY_PATH=$SCIBINARYBASE/lib/scilab:$SCIBINARYBASE/lib/thirdparty${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}
621             export DYLD_LIBRARY_PATH
622             PATH="/sw/bin:/sw64/bin:$PATH"
623             export PATH
624             ;;
625     esac
626
627 else
628     PATH="$SCI:$SCI/bin:$PATH"
629 fi
630
631
632 # See http://bugzilla.scilab.org/show_bug.cgi?id=3486
633 # Some Java stuff raises spurious SIGSEGV. The problem is that when bug-buddy
634 # is enabled, it catches the signal and terminate Scilab. We have to
635 # desactivate bug-buddy.
636 # See also https://bugzilla.novell.com/show_bug.cgi?id=331618
637 # and: http://bugzilla.gnome.org/show_bug.cgi?id=479929
638 GTK_MODULES=$(echo $GTK_MODULES | sed s/gnomebreakpad//g)
639 export GTK_MODULES
640
641 if test $SCIVERBOSE -ne 0; then
642     echo "SCI : $SCI"
643     echo "SCIBIN : $SCIBIN"
644     echo "PATH : $PATH"
645     echo "CLASSPATH : $CLASSPATH"
646 fi
647
648 # Returns to the original path
649 cd "$LOCALOLDPWD"
650
651 #if test "$DISPLAY" = ""; then
652 #  DISPLAY="unix:0.0"
653 #fi
654 #export DISPLAY
655
656
657 detect_macosx_version()
658 {
659     macosxVersion=`sw_vers | grep '^ProductVersion:' | grep -o '[0-9]*\.[0-9]*\.[0-9]*'`
660     if test "${macosxVersion}" = ""; then
661       macosxVersion=`sw_vers | grep '^ProductVersion:' | grep -o '[0-9]*\.[0-9]*'`
662     fi
663     macosxMajorVersion=`echo $macosxVersion | cut -d . -f 1`
664     macosxMinorVersion=`echo $macosxVersion | cut -d . -f 2`
665     macosxBuildVersion=`echo $macosxVersion | cut -d . -f 3`
666
667     if test $SCIVERBOSE -ne 0; then
668         echo "Found MacOSX Version: $macosxVersion"
669     fi
670
671 }
672
673 set_macox_specific_path()
674 {
675
676     PATH_TO_SPECIFIC_10_6=$SCIBINARYBASE/lib/thirdparty/10.6/
677     PATH_TO_SPECIFIC_10_5=$SCIBINARYBASE/lib/thirdparty/10.5/
678
679     case $macosxMajorVersion in
680         10)
681             case $macosxMinorVersion in
682                 6)
683                     # the iconv lib fails under 10.7, just declare it for 10.5 & 10.6
684                     LD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_6${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
685                     DYLD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_6${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}
686                     ;;
687                  5)
688                     # the iconv lib fails under 10.7, just declare it for 10.5 & 10.6
689                     LD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_6${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
690                     DYLD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_6${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}
691                     LD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_5${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
692                     DYLD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_5${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}
693                     ;;
694             esac
695             ;;
696     esac
697
698 }
699
700 #
701 # Scilab will not work on some Mac OSX version
702 # Check version number and ask user if he wants to go on.
703 #
704 check_macosx_version()
705 {
706
707     case $macosxMajorVersion in
708         10)
709             case $macosxMinorVersion in
710                 7|8)
711                    # We force to install JRE on Lion
712                    /usr/libexec/java_home --request
713                    ;;
714                 6)
715
716                     if test $macosxBuildVersion -le 4; then
717                         # Version <= 10.6.4
718                         osascript $BINPATH/checkmacosx.applescript $macosxVersion "10.6.5 (Snow Leopard) or newer system" || exit 128
719                     fi
720                     ;;
721                  5)
722                      if test $macosxBuildVersion -le 7; then
723                          # Version <= 10.5.8
724                          osascript $BINPATH/checkmacosx.applescript $macosxVersion "10.5.8 (Leopard) or newer system" || exit 128
725                      fi
726                      # Check if the default java is not 1.5 (magic)
727                      VERSION=$(/usr/libexec/java_home -v 1.5|grep 1.5.0)
728                      if test "$VERSION"; then
729                          open /Applications/Utilities/Java\ Preferences.app/ &
730                          osascript $BINPATH/checkmacosx.applescript $macosxVersion "Java 6 or newer. Please disable any version of Java 1.5 in the Java Preferences menu. An other workaround is to rename /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Libraries/libjava.jnilib to libjava.jnilib_" || exit 128
731                      fi
732
733                      ;;
734                 *)
735                     osascript $BINPATH/checkmacosx.applescript $macosxVersion "10.6.5 (Snow Leopard) or newer system" || exit 128
736                     ;;
737             esac
738             ;;
739         *)
740             osascript $BINPATH/checkmacosx.applescript $macosxVersion "10.6.5 (Snow Leopard) or newer system" || exit 128
741             ;;
742     esac
743 }
744
745 # . is used by the dynamic link. The build lib are copied into the PWD
746 SCILIB=$SCI/bin:$SCI/lib/scilab/:$SCI/lib64/scilab/:.
747 LD_LIBRARY_PATH=$SCILIB${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
748
749 case $OS in
750     *darwin* | *Darwin*) # MacOS X
751         # When using some packaging tools like fink which are storing the info in /sw/lib
752         LD_LIBRARY_PATH="/sw/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
753     # Export the path to the Scilab lib when Scilab has been install ...
754     # to a Mac os x specific variable used by the linker before LD_LIBRARY_PATH
755         DYLD_LIBRARY_PATH=$SCI/../../lib/scilab/${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}:
756         # Crappy workaround for the dynamic load of sundials under Mac OS X
757         # in the source tree version. This does not occur in the binary
758         # thanks to the previous declaration on ../../lib/scilab
759         # See bug #4949
760         if test -d "$SCI/modules/scicos_blocks/.libs/"; then
761             DYLD_LIBRARY_PATH=$SCI/modules/scicos_blocks/.libs/${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}
762         fi
763         if test -d "$SCI/modules/scicos/.libs/"; then
764             DYLD_LIBRARY_PATH=$SCI/modules/scicos/.libs/${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}
765         fi
766         export DYLD_LIBRARY_PATH
767
768         # Now, check that I can run Scilab on this version of Mac OS X and
769         # display a standard window
770         BINPATH=$(dirname $0)
771         # Do not perform the check with Scilab is started in CLI mode
772
773         detect_macosx_version
774         set_macox_specific_path
775
776         if test "$SCILAB_MODE" = "gui"; then
777             check_macosx_version
778         fi
779
780         # When started as an Application, the default path was "/"
781         # It is not really useful. Changing to HOME.
782         # See bugs 8339 and 10575 for rational
783         if test "$(pwd)" == "/"; then
784             cd $HOME
785         fi
786
787         ;;
788     *)
789 esac
790
791
792 if test $SCIVERBOSE -ne 0; then
793     echo "LD_LIBRARY_PATH : $LD_LIBRARY_PATH"
794     if test ! -z "$DYLD_LIBRARY_PATH"; then
795         echo "DYLD_LIBRARY_PATH: $DYLD_LIBRARY_PATH"
796     fi
797 fi
798 export LD_LIBRARY_PATH
799 export PATH
800
801 # Scilab does not support the LANGUAGE variable.
802 # Unset to avoid some weird issue. CF bug #10032
803 unset LANGUAGE
804
805 if test $IS_SCILAB_BINARY -ne 1; then
806 # We are not using the Scilab binary, find out where docbook is.
807     find_docbook_path
808 fi
809
810 # Check if SELINUX is enable. If it is the case, disable it for Scilab
811 check_and_disable_selinux
812
813 do_scilex()
814 {
815     "$SCILABBIN" "$@"
816 }
817
818 do_help()
819 {
820     echo "Usage:"
821     echo     "scilab <arguments>"
822     echo     "scilab-cli <arguments>"
823     echo     "scilab-adv-cli <arguments>"
824     echo     " "
825     echo     "  Possible arguments are:"
826     echo     "  -display Display: for use under Xwindow systems to set a specific X server display."
827     echo     "  -d Display      : equivalent to -display Display."
828     echo     "  -e Instruction  : execute the scilab instruction given in Instruction argument."
829     echo     "                    -e and -f arguments are mutually exclusive."
830     echo     "  -f File         : execute the scilab script given in File argument."
831     echo     "                    -e and -f arguments are mutually exclusive."
832     echo     "  -l Lang         : set the current language. Lang can be equal to fr or en."
833     echo     "  -mem N          : set the initial stacksize, for use with -ns argument."
834     echo     "  -nb             : do not display Scilab loading message at starting time."
835     echo     "  -ns             : do not execute scilab.start startup file. This argument will disable many features in Scilab (Only use if you know what you are doing)."
836     echo     "  -nouserstartup  : do not execute the user startup files SCIHOME/.scilab or SCIHOME/scilab.ini."
837     echo     "  -noatomsautoload: do not load ATOMS installed module."
838     echo     "  -nw             : start Scilab without dedicated Scilab Window."
839     echo     "  -nwni           : start Scilab without the GUI, graphic and help features (batch mode). This argument disables the need of Java."
840     echo     "  -nogui          : See -nwni"
841     echo     "  -nocolor        : Remove the color in the cli and adv-cli modes"
842     echo     "  -args           : accept all extra arguments and make them available through sciargs"
843     echo     "  -version        : print product version and exit."
844     echo     "  -h/--help       : display help about this command."
845     echo     "  --texmacs       : reserved for TeXMacs."
846     echo     " "
847
848     echo     "Developer arguments:"
849     echo     "  -debug          : Start Scilab under gdb (Unix/linux only)."
850     echo     "                    define the variable SCILAB_GDB_OPT to add custom arguments to gdb."
851     echo     "  -debug-kdbg     : Start Scilab under kdbg (Unix/linux only)."
852     echo     "  -profiling      : Start Scilab under valgrind (Unix/linux only)."
853     echo     "                    define the variable SCILAB_VALGRIND_OPT to add custom arguments to"
854     echo     "                    valgrind (and override the existing valgrind arguments)."
855     echo     "  -profiling-visu : Start Scilab under callgrind (Unix/linux only)."
856     echo     "                    define the variable SCILAB_VALGRIND_OPT to add custom arguments to"
857     echo     "                    callgrind (and override the existing callgrind arguments)."
858     echo     "  -electric-fence : Start Scilab with the Electric Fence (Unix/linux only)."
859     echo     "  -disable-exception-catching : Disable Scilab exception catching system."
860     echo     " "
861     echo     "  All these arguments can be retrieved by the Scilab function sciargs."
862     echo     " "
863
864     echo     "  Several environment variables can be declared:"
865     echo     "  SCIVERBOSE               Provides debugging information of the startup"
866     echo     "  JAVA_HOME                Declares which Java Virtual Machine to use"
867     echo     "  SCI_DISABLE_TK           Disable Tk (but not Tcl) features"
868     echo     "  SCI_JAVA_ENABLE_HEADLESS Enable Java Headless VM (ie without GUI features)"
869     echo     "  SCI_DISABLE_EXCEPTION_CATCHING Disable the catch by Scilab of exception (segfault, ...)"
870
871     exit
872 }
873
874 MORE_OPT=""
875 if test "$SCILAB_MODE" = "nw"; then
876     MORE_OPT="-nw"
877 fi
878
879 # calling Scilab with no argument or special cases of calling Scilab
880 rest="no"
881 case $# in
882     0) # No arg, run Scilab
883         do_scilex $MORE_OPT
884         ;;
885     *)
886         case $1 in
887             *)
888                 rest="yes"
889                 ;;
890         esac
891         ;;
892 esac
893
894 # really calling Scilab with arguments
895
896 if test "$rest" = "yes"; then
897     debug=
898     debug_kdbg=
899     profiling=
900     profiling_visu=
901     electric_fence=
902     prevarg=
903     for sciarg in "$@"
904     do
905
906     # If the previous argument needs an argument, skip current one.
907         if test -n "$prevarg"; then
908             prevarg=
909             continue
910         fi
911
912         case $sciarg in
913             -nogui|-version|-ns|-nb|-nouserstartup|-noatomsautoload|-nocolor|--texmacs)
914                 ;;
915             -nw)
916                 if test "$SCILAB_MODE" = "nwni"; then
917                     echo "Cannot use scilab-cli with option -nw"
918                     exit 5
919                 fi
920                 ;;
921             -nwni)
922                 if test "$SCILAB_MODE" = "nw"; then
923                     echo "Cannot use scilab-adv-cli with option -nwni"
924                     exit 5
925                 fi
926                 ;;
927             -psn_*)
928             # Under Mac OS X, .app is adding the arg -psn_0_xxxx
929             # (psn = ProcessSerialNumber). It is not used (but could be in the future)
930                 ;;
931             -display|-f|-l|-e|-d|-mem)
932                 prevarg=1
933                 ;;
934             -args) # Accept extra arguments
935                 extraarg=1
936                 ;;
937             -debug)
938                 DEBUG="-debug"
939                 ;;
940             -debug-kdbg)
941                 DEBUG_KDBG="-debug-kdbg"
942                 ;;
943             -profiling)
944                 PROFILING="-profiling"
945                 ;;
946             -profiling-visu)
947                 PROFILING_VISU="-profiling-visu"
948                 ;;
949             -electric-fence)
950                 ELECTRIC_FENCE="-electric-fence"
951                 ;;
952             -disable-exception-catching)
953                 SCI_DISABLE_EXCEPTION_CATCHING=1
954                 export SCI_DISABLE_EXCEPTION_CATCHING
955                 ;;
956             -h|--help)
957                 do_help
958                 ;;
959             *)
960                 foundUnknownArg=1
961           # Do nothing. Gives unknown args to Scilab binaries if -a is provided
962                 ;;
963         esac
964     done
965
966
967 # If the argument -a is not provided (extraarg) and we found unknown args
968 # fail and provide help
969     if test -z "$extraarg" -a -n "$foundUnknownArg"; then
970         do_help
971     fi
972
973     if test -n "$DEBUG"; then
974         start_debug "$@" $MORE_OPT
975     else
976         if test -n "$DEBUG_KDBG"; then
977             start_debug_kdbg "$@" $MORE_OPT
978         else
979             if test -n "$PROFILING"; then
980                 start_profiling "$@" $MORE_OPT
981             else
982                 if test -n "$PROFILING_VISU"; then
983                     start_profiling_visu "$@" $MORE_OPT
984                 else
985                     if test -n "$ELECTRIC_FENCE"; then
986                         start_electric_fence "$@" $MORE_OPT
987                     else
988                         do_scilex "$@" $MORE_OPT
989                     fi # not electric fense
990                 fi # not profiling visu
991             fi # not profiling
992         fi # not debug kdbg
993     fi # not debug
994 fi # if rest