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