specific path for 10.11 Mac libraries
[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 # Copyright (C) 2012 - 2016 - Scilab Enterprises
9 #
10 # This file is hereby licensed under the terms of the GNU GPL v2.0,
11 # pursuant to article 5.3.4 of the CeCILL v.2.1.
12 # This file was originally licensed under the terms of the CeCILL v2.1,
13 # and continues to be available under such terms.
14 # For more information, see the COPYING file which you should have received
15 # along with this program.
16 # === LICENSE_END ===
17
18 ####
19 #
20 # Scilab can be installed/used in different ways :
21 # * ./configure; make install
22 #    => will install Scilab into /usr/local/ and split files to
23 #    /usr/local/bin, /usr/local/share/scilab/, /usr/local/lib/scilab/,
24 #    /usr/local/include/scilab/
25 # * ./configure --prefix=/my/dir/; make install
26 #    => will install Scilab into /my/dir/ and split files to
27 #    /my/dir/bin/, /my/dir/share/scilab/, /my/dir/lib/scilab/
28 #    /my/dir/include/scilab/
29 #    Scilab official binary uses this arch
30 # * ./configure; make install prefix=$(DESTDIR)/usr \
31 #                   bindir=$(DESTDIR)/usr/bin \
32 #                   datadir=$(DESTDIR)/usr/share/scilab ...
33 #    Solution used mainly by Linux packagers
34 #
35 ## First, this script will look for scilab-bin in :
36 # * If specified, in the $SCI variable
37 # * Into the scilab/bin/ directory
38 # * Into the scilab/ directory (if the user is using a source version and
39 # scilab has been compiled in this directory)
40 # * if this file is a symlink to the real one. We browse symlink(s) until we
41 # find the real "scilab" script file and consider that it is the right
42 # directory
43 # * That is all. Don't hesitate to contact us if you see one missing.
44 #
45 ###
46
47 if test ! -z "$SCIVERBOSE"; then
48     SCIVERBOSE=1
49 else
50     SCIVERBOSE=0
51 fi
52
53 if test -n "$SCI"; then
54     if test ! -d "$SCI/modules"; then
55         echo "Cannot find directory $SCI/modules/ !"
56         echo "SCI should point to the root data directory of Scilab"
57         exit 255
58     fi
59 fi
60
61 DISABLE_JAVA_DETECTION=0
62 IS_SCILAB_BINARY=0
63
64 start_debug() {
65     if test -n "$DEVENV"; then
66         echo "Running debug of Scilab [gdb]  : " "$SCI"/libtool --mode=execute gdb $SCILAB_GDB_OPT --args "$SCILABBIN" "$@"
67         "$SCI"/libtool --mode=execute gdb $SCILAB_GDB_OPT --args "$SCILABBIN" "$@"
68     else
69         echo "Running debug of Scilab [gdb]  : " gdb $SCILAB_GDB_OPT --args "$SCILABBIN" "$@"
70         gdb $SCILAB_GDB_OPT --args "$SCILABBIN" "$@"
71     fi
72 }
73
74 start_debug_kdbg() {
75     if test -n "$DEVENV"; then
76         echo "Running debug of Scilab [kdbg]  : " "$SCI"/libtool --mode=execute kdbg "$SCILABBIN"
77         "$SCI"/libtool --mode=execute kdbg "$SCILABBIN"
78     else
79         echo "Running debug of Scilab [kdbg]  : " kdbg "$SCILABBIN"
80         kdbg "$SCILABBIN"
81     fi
82 }
83
84 start_profiling() {
85     VALGRIND_OPTIONS=" --show-below-main=yes --num-callers=16 --demangle=yes --leak-check=full --show-reachable=yes --smc-check=all --show-below-main=yes --track-origins=yes --suppressions=$SCI/tools/profiling/valgrind.supp $SCILAB_VALGRIND_OPT"
86     if test -n "$DEVENV"; then
87         echo "Running profiling of Scilab  : " "$SCI"/libtool --mode=execute valgrind $VALGRIND_OPTIONS "$SCILABBIN" "$@"
88         "$SCI"/libtool --mode=execute valgrind $VALGRIND_OPTIONS "$SCILABBIN" "$@"
89     else
90         echo "Running profiling of Scilab  : " valgrind $VALGRIND_OPTIONS "$SCILABBIN" "$@"
91         valgrind $VALGRIND_OPTIONS "$SCILABBIN" "$@"
92     fi
93
94 }
95
96 start_profiling_visu() {
97     VALGRIND_VISU_OPTIONS=" --tool=callgrind --trace-children=yes --smc-check=all --trace-jump=yes --dump-line=yes --separate-threads=yes $SCILAB_VALGRIND_OPT"
98 #--dump-instr=yes
99     if test -n "$DEVENV"; then
100         echo "Running profiling visualization of Scilab  : " "$SCI"/libtool --mode=execute valgrind $VALGRIND_VISU_OPTIONS "$SCILABBIN" "$@"
101         "$SCI"/libtool --mode=execute valgrind $VALGRIND_VISU_OPTIONS "$SCILABBIN" "$@"
102     else
103         echo "Running profiling visualization of Scilab  : " valgrind $VALGRIND_VISU_OPTIONS "$SCILABBIN" "$@"
104         valgrind $VALGRIND_VISU_OPTIONS "$SCILABBIN" "$@"
105     fi
106 }
107
108 find_docbook_path() {
109     if test -z "$DOCBOOK_ROOT"; then
110         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-*/ /usr/share/sgml/docbook/xsl-stylesheets /usr/share/sgml/docbook/xsl-ns-stylesheets-*/ /usr/share/xml/docbook/stylesheet/docbook-xsl /opt/local/share/xsl/docbook-xsl; do
111             if test -r "$dir/fo/docbook.xsl"; then
112                 if test -z "$DOCBOOK_ROOT"; then
113                     DOCBOOK_ROOT=$dir
114                     export DOCBOOK_ROOT
115                 fi
116             fi
117         done
118     fi
119     if test $SCIVERBOSE -ne 0; then
120         echo "DOCBOOK : $DOCBOOK_ROOT"
121     fi
122 }
123
124 detect_type_of_scilab(){
125
126     progname="$0"
127     SCILAB_MODE="gui"
128     SCILABBIN="scilab-bin"
129
130     if (echo "$progname"|grep "scilab-adv-cli$" 2>&1 > /dev/null;) || (echo "$@"|grep "\-nw" 2>&1 > /dev/null;) then
131         SCILAB_MODE="nw"
132         # We are using scilab-bin here because we are going to force the arg -nw
133         SCILABBIN="scilab-bin"
134     fi
135
136     if (echo "$progname"|grep "scilab-cli$" 2>&1 > /dev/null;) || (echo "$@"|grep "\-nwni" 2>&1 > /dev/null;) then
137         SCILAB_MODE="nwni"
138         SCILABBIN="scilab-cli-bin"
139         DISABLE_JAVA_DETECTION=1
140     fi
141
142     if (echo "$progname"|grep "scinotes$" 2>&1 > /dev/null;) then
143         SCILAB_MODE="scinotes"
144         SCILABBIN="scilab-bin"
145         extraarg=1
146     fi
147
148     if (echo "$progname"|grep "xcos$" 2>&1 > /dev/null;) then
149         SCILAB_MODE="xcos"
150         SCILABBIN="scilab-bin"
151         extraarg=1
152     fi
153
154     if test $SCIVERBOSE -ne 0; then
155         echo "SCILABBIN : $SCILABBIN"
156         echo "DISABLE_JAVA_DETECTION : $DISABLE_JAVA_DETECTION"
157         echo "SCILAB_MODE : $SCILAB_MODE"
158     fi
159 }
160
161 detect_os() {
162
163     if test "${OS}" = ""; then
164         OS=$(uname -s) # Retrieve the Operating System
165     fi
166     if test "${MODEL}" = ""; then
167         MODEL=$(uname -m) # Retrieve the model
168     fi
169
170     if test $SCIVERBOSE -ne 0; then
171         echo "OS : $OS"
172         echo "MODEL : $MODEL"
173     fi
174 }
175
176 detect_type_of_scilab $@
177
178 detect_os
179
180 detect_scilab_path() {
181
182     LOCALOLDPWD=$(pwd)
183
184     # If we are running Scilab binary, it is trivial to set paths.
185     curdir=$(dirname "$progname")
186
187     # If the script is a symlink, it might be a symlink to the Scilab binary
188     # We could run into the bug #9125
189     if test -h $progname; then
190         while test -h "$progname"; do
191             bn=$(basename "$progname")
192             cd $(dirname "$progname")
193
194             # Browse the symlink to the real one
195             progname=$(/bin/ls -l "$bn" | sed -e 's/^.* -> //' )
196
197             if test ! -x "$progname"; then
198                 break
199             fi
200         done
201         cd $(dirname "$progname")
202         curdir=$(pwd)
203     fi
204
205     if test -d $curdir/../thirdparty/ -a -d $curdir/../share/scilab; then
206         # We are in the Scilab binary
207         IS_SCILAB_BINARY=1
208         # export the bin directory to the PATH first (avoid to take the packaged version)
209         PATH="$curdir:$PATH"
210         cd "$curdir/../"
211         SCIBINARYBASE=$(pwd)
212         SCI=$SCIBINARYBASE/share/scilab
213         export SCI
214         return
215     fi
216
217     # Use scilab-bin in the current dir if it exists
218     # If not, then start resolving symlinks until we find scilab-bin
219     found=0
220     cd $(dirname "$progname")
221     curdir=$(pwd -P)
222     PATH="$curdir:$PATH"
223
224     progbase=$(basename "$progname")
225     run_sci="$curdir/$SCILABBIN"
226     if test -x "$run_sci"; then # scilab-bin is in the /bin/ directory
227         cd "$curdir/../"
228         SCI=$(pwd)
229         SCIBIN=$run_sci
230         found=1
231     else
232         run_sci="$curdir/../$SCILABBIN"
233         if test -x "$run_sci"; then # scilab-bin is in the / directory
234         # which is the case in a dev env
235             cd "$curdir/../"
236             SCI=$(pwd)
237             SCIBIN=$run_sci
238             found=1
239         else
240             here=$(/bin/pwd)
241             while test -h "$progname"; do
242                 bn=$(basename "$progname")
243                 cd $(dirname "$progname")
244             # Browse the symlink to the real one
245                 progname=$(/bin/ls -l "$bn" | sed -e 's/^.* -> //' )
246                 if test ! -x "$progname"; then
247                     break
248                 fi
249                 curdir=$(dirname "$progname")
250                 if test -x "$curdir"; then
251                     cd "$curdir/../"
252                     SCI=$(pwd)
253                     SCIBIN="$curdir/$SCILABBIN"
254                     found=1
255
256                     break
257                 fi
258             # The current file ($0), probably scilab is a symlink to the
259             # real one and here, it is the dev env
260                 run_sci="$curdir/../$SCILABBIN"
261                 if test -x "$run_sci"; then
262                     cd "$curdir/../"
263                     SCI=$(pwd)
264                     SCIBIN=$run_sci
265                     found=1
266                     break
267                 fi
268             done
269             cd "$here"
270         fi
271         if test $SCIVERBOSE -ne 0; then
272             echo "I think I found Scilab here : $SCI"
273         fi
274     fi
275
276
277     if test -x "$SCI/bin/$SCILABBIN"; then
278 # real env
279         SCILABBIN=$SCI/bin/$SCILABBIN
280         SCI=$SCI/share/scilab/
281     else
282         if test -x "$SCI/$SCILABBIN"; then
283         # dev env
284             SCILABBIN=$SCI/$SCILABBIN
285             DEVENV=1
286         # add modelica in the path
287             PATH="$PATH:$SCI/modules/scicos/"
288         else
289             echo "Cannot find $SCILABBIN ..."
290             exit 1
291         fi
292     fi
293
294     export SCI
295 }
296
297
298 set_macosx_java_vm () {
299     # For Mac OS X, Since path are different from the JVM normal tree,
300     # don't need to detect the CPU
301     JAVA_SHLIB="libjava.jnilib"
302     if test -z "$JAVA_HOME"; then
303       JAVA_HOME=$(/usr/libexec/java_home --arch x86_64 --failfast --version 1.8+)
304     fi
305     JAVA_JVM_VERSION=$JAVA_HOME
306     export JAVA_JVM_VERSION
307     DYLD_LIBRARY_PATH="$JAVA_HOME/../Libraries${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}"
308
309 }
310
311 set_embedded_java_vm () {
312
313        # Scilab binary => no Java detection since we are using the embeded JRE
314         # Here, we are handling 3 cases: GNU/Linux 32bit, GNU/Linux 64bit,
315         # Mac OS X
316         case $OS in
317             "Linux")
318                 case $MODEL in
319                     "x86_64")
320                         proc="amd64"
321                         ;;
322                     i?86)
323                         # We embed java/lib/i386 with Scilab Binary
324                         # force proc to i386 even if systems answer i686
325                         proc="i386"
326                         ;;
327                     *)
328                         echo "Could not find the Java configuration for the model <${MODEL}>. Please contact us on http://bugzilla.scilab.org"
329                         ;;
330                 esac
331                 ;;
332             *darwin* | *Darwin*) # MacOS X
333                 # JRE is now embedded in Mac OS X binary version
334                 #set_macosx_java_vm
335                 JAVA_HOME=$SCIBINARYBASE/thirdparty/java/Contents/Home/
336                 JAVA_JVM_VERSION=$JAVA_HOME
337                 export JAVA_JVM_VERSION
338                 DYLD_LIBRARY_PATH="$JAVA_HOME/lib/server:${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}"
339                 ;;
340             *)
341             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/"
342             echo "Fallback on the model <${MODEL}> found by uname -m"
343             proc=$MODEL
344             ;;
345     esac
346     JRE_HOME="$SCIBINARYBASE/thirdparty/java/"
347
348 }
349
350 # If enabled, disable se_linux
351 check_and_disable_selinux() {
352     if test -x /usr/sbin/selinuxenabled -a $IS_SCILAB_BINARY -eq 1; then
353         /usr/sbin/selinuxenabled
354         if test $? = 0; then
355             if test $SCIVERBOSE -ne 0; then
356                 echo "Disabling SELinux on Scilab"
357             fi
358
359             JVMLIB_32=$SCIBINARYBASE/java/lib/i386/server/libjvm.so
360             if test -f $JVMLIB_32; then
361                 /usr/bin/chcon -t textrel_shlib_t "$JVMLIB_32"
362                 if test $? != 0; then
363                     echo "Error: Cannot chcon $JVMLIB_32"
364                 fi
365             fi
366             JVMLIB_64=$SCIBINARYBASE/java/lib/amd64/server/libjvm.so
367             if test -f $JVMLIB_64; then
368                 /usr/bin/chcon -t textrel_shlib_t "$JVMLIB_64"
369                 if test $? != 0; then
370                     echo "Error: Cannot chcon $JVMLIB_64"
371                 fi
372             fi
373             /usr/bin/chcon -t execmem_exec_t "$SCIBINARYBASE/bin/scilab-bin"
374             if test $? != 0; then
375                 echo "Error: Cannot chcon 'scilab-bin'"
376             fi
377
378             /usr/bin/chcon -t execmem_exec_t "$SCIBINARYBASE/bin/scilab-cli-bin"
379             if test $? != 0; then
380                 echo "Error: Cannot chcon 'scilab-cli-bin'"
381             fi
382
383         fi
384     fi
385 }
386
387 detect_java_vm() {
388
389     JAVA_SHLIB="libjava.so"
390
391     if test $IS_SCILAB_BINARY -eq 1; then
392         set_embedded_java_vm
393     else  # NOT IS_SCILAB_BINARY
394
395 ############# JAVA DETECTION ##############
396 # Basic inspiration from Eclipse
397 #
398 # Scilab is looking for Java in various places with different ways :
399 # * if JAVA_HOME is specificed, use it
400 # * Check in 'standard' places we know Java could be installed
401 # if some are missing, please contact us
402 # * look in the PATH if there is a java binary
403 # if it is the case, resolve the symlink and set the JAVA_HOME from it
404 #
405 # Once we have JAVA_HOME, we check that native libraries (libjava.so and
406 # 2 others) are available
407 ###
408
409 # Get the proc name matching to the one of the JVM
410 # 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
411 # For Mac OS X, we will get stuff like that:
412 # uname -s Power Macintosh
413 # uname -m Darwin
414
415 # If the user has the variable JAVA_HOME in the ENV
416         if test -n "$JAVA_HOME" ; then
417                 if test $SCIVERBOSE -ne 0; then
418                     echo "Using specified vm: $JAVA_HOME"
419                 fi
420                 if test ! -x "$JAVA_HOME/bin/java" ; then
421                     echo "Cannot find $JAVA_HOME/bin/java"
422                     exit 1
423                 fi
424         else
425
426             case $OS in
427                 *darwin* | *Darwin*)
428                     set_macosx_java_vm
429                     if test $SCIVERBOSE -ne 0; then
430                         echo "Using default Mac OS X vm: $JAVA_HOME"
431                         echo "And LD_LIBRARY_PATH: $LD_LIBRARY_PATH"
432                         echo "And DYLD_LIBRARY_PATH: $DYLD_LIBRARY_PATH"
433                     fi
434                     ;;
435                 *)
436                     ;;
437             esac
438
439 # Browse "default directory" (if it means anything under Unix/Linux) of Java
440 # to find where it could be !
441             if test -z "$JAVA_HOME"; then
442                 JAVA_DEFAULT_DIR="/usr/java/jdk1.8.*/ /usr/lib/jvm/java-1.8.0-*/ /etc/alternatives/java_sdk /etc/alternatives/jre /usr/java/jdk*/ /usr/lib/jvm/java-*/"
443                 for DIR in $JAVA_DEFAULT_DIR ; do
444                     if test $SCIVERBOSE -ne 0; then
445                         echo "Trying to find Java in $DIR "
446                     fi
447                     if test -x "${DIR}/bin/java" ; then
448                         # resolve symlinked dir
449                         cd $DIR
450                         JAVA_HOME=$(pwd -P)
451                         cd $OLDPWD
452
453                         export JAVA_HOME
454                         if test $SCIVERBOSE -ne 0; then
455                             echo "Java found in $JAVA_HOME"
456                         fi
457                         break
458                     fi
459                 done
460             fi
461
462 # if JAVA_HOME still haven't been found. Well, we can now try to find it in the path
463             if test ! -d "$JAVA_HOME"; then
464
465                 PATH_SEPARATOR=:
466                 as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
467                 if test $SCIVERBOSE -ne 0; then
468                     echo "Look for the java binary in the PATH. If it is a symlink, try to resolve it"
469                 fi
470                 for DIR in $PATH; do
471                     if test -f $DIR/java; then
472
473                         _cur=$DIR/java
474                 # resolve symlink
475                 # Will probably bug if a file contains -> in the name
476                         while ls -ld "$_cur" 2>/dev/null | grep " -> " >/dev/null; do
477                             _cur=$(ls -ld "$_cur" | sed 's/.* -> //')
478                         done
479                 # symlink resolved to the real file
480
481                 # Now, create JAVA_HOME
482                         TMP=$(dirname $_cur)
483                         JAVA_HOME=$(dirname $TMP)
484                         if test ! -z "$JAVA_HOME"; then
485                             break;
486                         fi
487                     fi
488                 done
489                 IFS=$as_save_IFS
490             fi
491         fi
492         if test ! -z "$JAVA_HOME"; then
493             if test $SCIVERBOSE -ne 0; then
494                 echo "JAVA_HOME : $JAVA_HOME"
495             fi
496 # Manage JRE & JDK because libjava.so and others are stored in the JRE
497             if test -d $JAVA_HOME/jre; then
498                 JRE_HOME=$JAVA_HOME/jre
499             else
500                 JRE_HOME=$JAVA_HOME
501             fi
502 # Configuring LD_LIBRARY_PATH for libjava.so libhpi.so and libxxxx.so
503
504             case $OS in
505                 "Linux")
506                     case $MODEL in
507                         "x86_64")
508                             proc="amd64"
509                             ;;
510                         i?86)
511
512                             if test -d ${JRE_HOME}/lib/$(uname -m) ; then
513                                 proc=$(uname -m)
514                             else
515                                 proc="i386"
516                             fi
517                             ;;
518                         "alpha")
519                             proc="alpha"
520                             ;;
521                         "sparc"|"sparc64")
522                             proc="sparc"
523                             ;;
524                         sh*)
525                             proc="sh"
526                             ;;
527                         "ia64")
528                             proc="ia64"
529                             ;;
530                         mips*) # under mipsel, uname -m is
531                     # usually returning mips or mips64...
532                     # but binary are in mipsel
533                             if test -d ${JRE_HOME}/lib/mipsel ; then
534                                 proc="mipsel"
535                             else
536                                 proc="mips"
537                             fi
538                             ;;
539                         "ppc"|"powerpc")
540                             proc="ppc"
541                             ;;
542                         "ppc64"|"ppc64el")
543                             proc="ppc64"
544                             ;;
545                         "s390")
546                             proc="s390"
547                             ;;
548                         "s390x")
549                             proc="s390x"
550                             ;;
551                         armv*)
552                             proc="arm"
553                             ;;
554                         aarch64)
555                             proc="aarch64"
556                             ;;
557                         *)
558                             echo "Could not find the Java configuration for the model <${MODEL}>. Please contact us on http://bugzilla.scilab.org"
559                             ;;
560                     esac
561                     ;;
562                 "HP-UX")
563                     JAVA_SHLIB="libjava.sl"
564                     proc="lib" # TODO : check if it is right or not ... seems not !
565                     ;;
566                 "IRIX")
567                     proc="lib32"
568                     ;;
569                 "SunOS")
570                     case $MODEL in
571                         "i86pc") # solaris x86
572                             proc="i386"
573                             ;;
574                 # check for the 64 bits syntax
575                         "sparc"|"sparc64")
576                             proc="sparc"
577                             ;;
578                         *)
579                             echo "Could not find the Java configuration for the model <${MODEL}>. Please contact us."
580                             ;;
581                     esac
582                     ;;
583
584                 *win32* | *WIN32* | *CYGWIN_NT*)
585                     proc="win32"
586                     JAVA_SHLIB="jvm.dll"
587                     ;;
588                 *darwin* | *Darwin*) # MacOS X
589                     set_macosx_java_vm
590                     ;;
591                 *FreeBSD*)
592                     proc=$MODEL
593                     ;;
594                 *)
595                     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/"
596                     echo "Fallback on the model <${MODEL}> found by uname -m"
597                     proc=$MODEL
598                     ;;
599             esac
600         fi # JAVA_HOME provided or found
601     fi # IS_SCILAB_BINARY
602
603     # Check if the lib exists
604     LIBJAVA="$JRE_HOME/lib/$proc/$JAVA_SHLIB"
605     if test ! -f $LIBJAVA; then
606         if test $SCIVERBOSE -ne 0; then
607             echo "Cannot find $LIBJAVA"
608         fi
609     else
610         CLASSPATH="${CLASSPATH:+:$CLASSPATH}$SCI/modules/core/jar/org.scilab.modules.core.jar"
611         # @TODO This is the default path of the official sun JVM.
612         # It is different for other implementation of the JVM....
613         LD_LIBRARY_PATH="$JRE_HOME/lib/$proc/:$JRE_HOME/lib/$proc/server/:$JRE_HOME/lib/$proc/native_threads/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
614     fi
615
616     # Bug http://bugzilla.scilab.org/show_bug.cgi?id=14290 -
617     # Never run headless on Mac (unless explicitly asked through
618     # the SCI_JAVA_ENABLE_HEADLESS option -- which will not work anyway)
619     case $OS in
620         *darwin* | *Darwin*) # MacOS X
621             export _JAVA_OPTIONS="-Djava.awt.headless=false"
622     ;;        
623     esac
624
625 ####### END OF JAVA ENV DETECTION/SETTING ######
626 }
627
628 detect_scilab_path
629 if test $SCIVERBOSE -ne 0; then
630     echo "IS_SCILAB_BINARY : $IS_SCILAB_BINARY"
631 fi
632
633
634 # If we are calling scilab-cli, no need to detect java.
635 if test $DISABLE_JAVA_DETECTION -ne 1; then
636     detect_java_vm
637 fi
638
639 if test $IS_SCILAB_BINARY -eq 1; then
640
641     if test ! -d "$SCIBINARYBASE/thirdparty/tcl8.5"; then
642         # New packaging path (similar to Windows one)
643         TCL_LIBRARY="$SCIBINARYBASE/share/scilab/modules/tclsci/tcl/tcl8.5"
644         TK_LIBRARY="$SCIBINARYBASE/share/scilab/modules/tclsci/tcl/tk8.5"
645     else
646         # Old packaging path
647         TCL_LIBRARY="$SCIBINARYBASE/thirdparty/tcl8.5"
648         TK_LIBRARY="$SCIBINARYBASE/thirdparty/tk8.5"
649     fi
650     LD_LIBRARY_PATH=$SCIBINARYBASE/lib/scilab:$SCIBINARYBASE/lib/thirdparty${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}:$SCIBINARYBASE/lib/thirdparty/redist
651     DOCBOOK_ROOT="$SCI/../../thirdparty/docbook"
652     PATH="$PATH:$SCI/modules/scicos/"
653
654     export DOCBOOK_ROOT
655     export PATH
656     export LD_LIBRARY_PATH
657     export TCL_LIBRARY
658     export TK_LIBRARY
659
660     case $OS in
661         *darwin* | *Darwin*)
662             DYLD_LIBRARY_PATH=$SCIBINARYBASE/lib/scilab:$SCIBINARYBASE/lib/thirdparty${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}
663             export DYLD_LIBRARY_PATH
664             PATH="/sw/bin:/sw64/bin:$PATH"
665             export PATH
666             ;;
667     esac
668
669 else
670     # Scilab compiled using prerequirements
671     if test -d $curdir/../usr; then
672         TCL_LIBRARY="$SCI/usr/lib/tcl8.5"
673         TK_LIBRARY="$SCI/usr/lib/tk8.5"
674         export TCL_LIBRARY
675         export TK_LIBRARY
676     fi
677     PATH="$SCI:$SCI/bin:$PATH"
678 fi
679
680
681 # See http://bugzilla.scilab.org/show_bug.cgi?id=3486
682 # Some Java stuff raises spurious SIGSEGV. The problem is that when bug-buddy
683 # is enabled, it catches the signal and terminate Scilab. We have to
684 # desactivate bug-buddy.
685 # See also https://bugzilla.novell.com/show_bug.cgi?id=331618
686 # and: http://bugzilla.gnome.org/show_bug.cgi?id=479929
687 GTK_MODULES=$(echo $GTK_MODULES | sed s/gnomebreakpad//g)
688 export GTK_MODULES
689
690 # libEGL may be partially supported by the closed-source NVIDIA driver.
691 # Until they release an EGL driver, we force the egl_glx driver.
692 # http://bugzilla.scilab.org/show_bug.cgi?id=12940
693 if test $SCILAB_MODE != "nwni" -a -x /usr/bin/glxinfo; then
694     NVIDIA_GLX_VENDOR=$(/usr/bin/glxinfo |grep "NVIDIA Corporation")
695     if test ! -z "$NVIDIA_GLX_VENDOR"; then
696         EGL_DRIVER=egl_glx
697         export EGL_DRIVER
698     fi
699 fi
700
701 if test $SCIVERBOSE -ne 0; then
702     echo "SCI : $SCI"
703     echo "SCIBIN : $SCIBIN"
704     echo "PATH : $PATH"
705     echo "CLASSPATH : $CLASSPATH"
706     echo "EGL_DRIVER : $EGL_DRIVER"
707 fi
708
709 # Returns to the original path
710 cd "$LOCALOLDPWD"
711
712 #if test "$DISPLAY" = ""; then
713 #  DISPLAY="unix:0.0"
714 #fi
715 #export DISPLAY
716
717
718 detect_macosx_version()
719 {
720     macosxVersion=`sw_vers | grep '^ProductVersion:' | grep -o '[0-9]*\.[0-9]*\.[0-9]*'`
721     if test "${macosxVersion}" = ""; then
722       macosxVersion=`sw_vers | grep '^ProductVersion:' | grep -o '[0-9]*\.[0-9]*'`
723     fi
724     macosxMajorVersion=`echo $macosxVersion | cut -d . -f 1`
725     macosxMinorVersion=`echo $macosxVersion | cut -d . -f 2`
726     macosxBuildVersion=`echo $macosxVersion | cut -d . -f 3`
727
728     if test $SCIVERBOSE -ne 0; then
729         echo "Found MacOSX Version: $macosxVersion"
730     fi
731
732 }
733
734 set_macox_specific_path()
735 {
736
737     PATH_TO_SPECIFIC_10_11=$SCIBINARYBASE/lib/thirdparty/10.11/
738     PATH_TO_SPECIFIC_10_10=$SCIBINARYBASE/lib/thirdparty/10.10/
739     PATH_TO_SPECIFIC_10_6=$SCIBINARYBASE/lib/thirdparty/10.6/
740
741     case $macosxMajorVersion in
742         10)
743             case $macosxMinorVersion in
744                 11)
745                     # not used yet
746                     LD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_11${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
747                     DYLD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_11${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}
748                     ;;
749                 10)
750                     # Specific libBLAS.dylib & libLAPACK.dylib version for 10.10
751                     LD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_10${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
752                     DYLD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_10${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}
753                     ;;
754                 6)
755                     # the iconv lib fails under 10.7, just declare it for 10.6
756                     LD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_6${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
757                     DYLD_LIBRARY_PATH=$PATH_TO_SPECIFIC_10_6${DYLD_LIBRARY_PATH+:$DYLD_LIBRARY_PATH}
758                     ;;
759             esac
760             ;;
761     esac
762
763 }
764
765 #
766 # Version checks for Mac
767 #
768 dialog_try_or_exit() 
769 {
770     osascript $BINPATH/checkmacosx.applescript "$1" || exit 128
771 }
772
773 fail_macosx_too_old()
774 {
775     dialog_try_or_exit "This version of Scilab will probably fail on this system ($macosxVersion): Scilab requires 10.8.3 (Mountain Lion) or newer system."
776 }
777 check_macosx_version()
778 {
779     case $macosxMajorVersion in
780         10)
781             case $macosxMinorVersion in
782                 9)
783                    # We force to install JRE on Mavericks
784                    /usr/libexec/java_home --request > /dev/null
785                    ;;
786                 10|11)
787                    # ... but not on Yosemite or El Capitan:
788                    # there will be a (better) error at launch
789                    ;;
790                 8)
791                     if test $macosxBuildVersion -le 3; then
792                         # Version <= 10.8.3
793                         fail_macosx_too_old
794                     fi
795                     ;;
796                 *)
797                     fail_macosx_too_old
798                     ;;
799             esac
800             ;;
801         *)
802             fail_macosx_too_old
803             ;;
804     esac
805 }
806
807 # . is used by the dynamic link. The build lib are copied into the PWD
808 SCILIB=$SCI/bin:$SCI/lib/scilab/:$SCI/lib64/scilab/:.
809 if test -d $SCI/lib/thirdparty; then
810     SCILIB=$SCI/lib/thirdparty:$SCILIB;
811 fi
812 LD_LIBRARY_PATH=$SCILIB${LD_LIBRARY_PATH+:$LD_LIBRARY_PATH}
813
814 case $OS in
815     *darwin* | *Darwin*) # MacOS X
816         # When using some packaging tools like fink which are storing the info in /sw/lib
817         LD_LIBRARY_PATH="/sw/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
818     # Export the path to the Scilab lib when Scilab has been install ...
819     # to a Mac os x specific variable used by the linker before LD_LIBRARY_PATH
820         DYLD_LIBRARY_PATH=$SCI/../../lib/scilab/${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}:
821         # Crappy workaround for the dynamic load of sundials under Mac OS X
822         # in the source tree version. This does not occur in the binary
823         # thanks to the previous declaration on ../../lib/scilab
824         # See bug #4949
825         if test -d "$SCI/modules/scicos_blocks/.libs/"; then
826             DYLD_LIBRARY_PATH=$SCI/modules/scicos_blocks/.libs/${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}
827         fi
828         if test -d "$SCI/modules/scicos/.libs/"; then
829             DYLD_LIBRARY_PATH=$SCI/modules/scicos/.libs/${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}
830         fi
831         export DYLD_LIBRARY_PATH
832
833         # Now, check that I can run Scilab on this version of Mac OS X and
834         # display a standard window
835         BINPATH=$(dirname $0)
836         # Do not perform the check with Scilab is started in CLI mode
837
838         detect_macosx_version
839         set_macox_specific_path
840
841         if test "$SCILAB_MODE" = "gui"; then
842             check_macosx_version
843         fi
844
845         # When started as an Application, the default path was "/"
846         # It is not really useful. Changing to HOME.
847         # See bugs 8339 and 10575 for rational
848         if test "$(pwd)" = "/"; then
849             cd $HOME
850         fi
851
852         ;;
853     *)
854 esac
855
856
857 if test $SCIVERBOSE -ne 0; then
858     echo "LD_LIBRARY_PATH : $LD_LIBRARY_PATH"
859     if test ! -z "$DYLD_LIBRARY_PATH"; then
860         echo "DYLD_LIBRARY_PATH: $DYLD_LIBRARY_PATH"
861     fi
862 fi
863 export LD_LIBRARY_PATH
864 export PATH
865
866 # Scilab does not support the LANGUAGE variable.
867 # Unset to avoid some weird issue. CF bug #10032
868 unset LANGUAGE
869
870 if test $IS_SCILAB_BINARY -ne 1; then
871 # We are not using the Scilab binary, find out where docbook is.
872     find_docbook_path
873 fi
874
875 # Check if SELINUX is enable. If it is the case, disable it for Scilab
876 check_and_disable_selinux
877
878 do_scinotes()
879 {
880     INDENT=""
881     if test $# -gt 0; then
882     # scinotes is called. We process the arguments
883     ARGS="["
884
885     for SCIARG in "$@"; do
886         if test "$SCIARG" = "-indent"; then
887             INDENT="['indent','trailing','quote']"
888         else
889             ARGS="$ARGS '$SCIARG', "
890         fi
891     done
892     ARGS="$ARGS]"
893     fi
894
895     if test -n "$INDENT"; then
896     # Call scinotes with the arguments
897         if test $SCIVERBOSE -ne 0; then
898             echo "Will start scinotes with: $SCILABBIN -nb -nw -e scinotes($ARGS, $INDENT);exit()"
899         fi
900         "$SCILABBIN" -nb -nw -e "scinotes($ARGS, $INDENT);exit()"
901     else
902         if test $SCIVERBOSE -ne 0; then
903             echo "Will start scinotes with: $SCILABBIN -e scinotes($ARGS)"
904         fi
905
906     # Call scinotes with no argument
907         "$SCILABBIN" -e "scinotes($ARGS)"
908         exit $?
909     fi
910 }
911
912 do_xcos()
913 {
914     SIMULATE=0
915     if test $# -gt 0; then
916     # xcos is called. We process the arguments
917         ARGS="["
918         for SCIARG in "$@"; do
919             if test "$SCIARG" = "-simulate"; then
920                 if test $# -ne 2; then
921                     echo "Wrong syntax: When -simulate is used, only one file must be provided."
922                     exit 43
923                 fi
924                 SIMULATE=1
925             else
926                 ARGS="$ARGS '$SCIARG', "
927             fi
928         done
929         ARGS="$ARGS ]"
930     fi
931
932     if test $SIMULATE -eq 1; then
933         if test $SCIVERBOSE -ne 0; then
934             echo "Will start Xcos with: $SCILABBIN -e 'importXcosDiagram($ARGS);xcos_simulate(scs_m, 4)'"
935         fi
936
937     # Call xcos with the arguments to simulate
938         "$SCILABBIN" -e "importXcosDiagram($ARGS);xcos_simulate(scs_m, 4)"
939     else
940         if test $SCIVERBOSE -ne 0; then
941             echo "Will start Xcos with: $SCILABBIN -e xcos($ARGS)"
942         fi
943
944     # Call xcos with the arguments
945         "$SCILABBIN" -e "xcos($ARGS)"
946         exit $?
947     fi
948
949 }
950
951 do_scilex()
952 {
953     if test "$SCILAB_MODE" = "xcos"; then
954         do_xcos "$@"
955     else
956         if test "$SCILAB_MODE" = "scinotes"; then
957             do_scinotes "$@"
958         else
959             "$SCILABBIN" "$@"
960             exit $?
961         fi
962     fi
963 }
964
965 do_help()
966 {
967     if test "$SCILAB_MODE" = "xcos"; then
968         echo "Start Xcos, the hybrid simulator"
969         echo "Usage:"
970         echo "xcos [ file1.xcos, [ file2.xcos ]]"
971         echo     " "
972         echo     "  Possible arguments are:"
973         echo     "  -simulate : Start the simulation. Only one file will be accepted."
974         exit
975     fi
976     if test "$SCILAB_MODE" = "scinotes"; then
977         echo "Start Scinotes, the Scilab text editor"
978         echo "Usage:"
979         echo     "scinotes [ file1.sci, [ file2.sce ]]"
980         echo     " "
981         echo     "  Possible arguments are:"
982         echo     "  -indent : format (indent, trailing spaces, quote mismatch) the files"
983         exit
984     fi
985
986     echo "Usage:"
987     echo     "scilab <arguments>"
988     echo     "scilab-cli <arguments>"
989     echo     "scilab-adv-cli <arguments>"
990     echo     " "
991     echo     "  Possible arguments are:"
992     echo     "  -display Display: for use under Xwindow systems to set a specific X server display."
993     echo     "  -d Display      : equivalent to -display Display."
994     echo     "  -e Instruction  : execute the scilab instruction given in Instruction argument."
995     echo     "                    -e and -f arguments are mutually exclusive."
996     echo     "  -f File         : execute the scilab script given in File argument."
997     echo     "                    -e and -f arguments are mutually exclusive."
998     echo     "  -quit           : force scilab exit after execution of script from -e or -f argument."
999     echo     "                    this flag is ignored if it is not used with -e or -f argument."
1000     echo     "  -l Lang         : set the current language. Lang can be equal to fr or en."
1001     echo     "  -nb             : do not display Scilab loading on start."
1002     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)."
1003     echo     "  -nouserstartup  : do not execute the user startup files SCIHOME/.scilab or SCIHOME/scilab.ini."
1004     echo     "  -noatomsautoload: do not load ATOMS installed module."
1005     echo     "  -nw             : start Scilab without dedicated Scilab Window."
1006     echo     "  -nwni           : start Scilab without the GUI, graphic and help features (batch mode). This argument disables the need of Java."
1007     echo     "  -nogui          : See -nwni"
1008     echo     "  -nocolor        : Remove the color in the cli and adv-cli modes"
1009     echo     "  -args           : accept all extra arguments and make them available through sciargs"
1010     echo     "  -version        : print product version and exit."
1011     echo     "  -h/--help       : display help about this command."
1012     echo     "  --texmacs       : reserved for TeXMacs."
1013     echo     " "
1014
1015     echo     "Developer arguments:"
1016     echo     "  -debug          : Start Scilab under gdb (Unix/linux only)."
1017     echo     "                    define the variable SCILAB_GDB_OPT to add custom arguments to gdb."
1018     echo     "  -debug-kdbg     : Start Scilab under kdbg (Unix/linux only)."
1019     echo     "  -profiling      : Start Scilab under valgrind (Unix/linux only)."
1020     echo     "                    define the variable SCILAB_VALGRIND_OPT to add custom arguments to"
1021     echo     "                    valgrind (and override the existing valgrind arguments)."
1022     echo     "  -profiling-visu : Start Scilab under callgrind (Unix/linux only)."
1023     echo     "                    define the variable SCILAB_VALGRIND_OPT to add custom arguments to"
1024     echo     "                    callgrind (and override the existing callgrind arguments)."
1025     echo     "  -disable-exception-catching : Disable Scilab exception catching system."
1026     echo     " "
1027     echo     "  --parse-file File : Only parse File argument without execution and exit."
1028     echo     "  --parse-trace     : Display bison state machine evolution."
1029     echo     "  --AST-trace       : Display ASCII-art AST to be human readable."
1030     echo     "  --pretty-print    : Display pretty-printed code, standard Scilab syntax."
1031     echo     " "
1032     echo     "Developer Timer arguments:"
1033     echo     "  --AST-timed      : Time each AST node."
1034     echo     "  --timed          : Time global execution."
1035     echo     " "
1036     echo     "Developer Debug arguments:"
1037     echo     "  --no-exec        : Only do Lexing/parsing do not execute instructions."
1038     echo     "  --context-dump   : Display context status."
1039     echo     "  --exec-verbose   : Display command before execute it."
1040     echo     " "
1041     echo     "      All these arguments can be retrieved by the Scilab function sciargs."
1042     echo     " "
1043
1044     echo     "  Several environment variables can be declared:"
1045     echo     "  SCIVERBOSE               Provides debugging information of the startup"
1046     echo     "  JAVA_HOME                Declares which Java Virtual Machine to use"
1047     echo     "  SCI_DISABLE_TK           Disables Tk (but not Tcl) features"
1048     echo     "  SCI_JAVA_ENABLE_HEADLESS Runs Java Headless VM (without GUI; Windows and Linux only)"
1049     echo     "  SCI_DISABLE_EXCEPTION_CATCHING Disable the catch by Scilab of exception (segfault, ...)"
1050
1051     exit
1052 }
1053
1054 MORE_OPT=""
1055 if test "$SCILAB_MODE" = "nw"; then
1056     MORE_OPT="-nw"
1057 fi
1058
1059 # calling Scilab with no argument or special cases of calling Scilab
1060 rest="no"
1061 case $# in
1062     0) # No arg, run Scilab
1063         do_scilex $MORE_OPT
1064         ;;
1065     *)
1066         case $1 in
1067             *)
1068                 rest="yes"
1069                 ;;
1070         esac
1071         ;;
1072 esac
1073
1074 # really calling Scilab with arguments
1075
1076 if test "$rest" = "yes"; then
1077     debug=
1078     debug_kdbg=
1079     profiling=
1080     profiling_visu=
1081     prevarg=
1082     for sciarg in "$@"
1083     do
1084
1085     # If the previous argument needs an argument, skip current one.
1086         if test -n "$prevarg"; then
1087             prevarg=
1088             continue
1089         fi
1090
1091         case $sciarg in
1092             -nogui|-version|-ns|-nb|-nouserstartup|-noatomsautoload|-nocolor|--texmacs)
1093                 ;;
1094             -nw)
1095                 if test "$SCILAB_MODE" = "nwni"; then
1096                     echo "Cannot use scilab-cli with option -nw"
1097                     exit 5
1098                 fi
1099                 ;;
1100             -nwni)
1101                 if test "$SCILAB_MODE" = "nw"; then
1102                     echo "Cannot use scilab-adv-cli with option -nwni"
1103                     exit 5
1104                 fi
1105                 ;;
1106             --parse-trace|--pretty-print|--help|--AST-trace|--no-exec|--context-dump|--exec-verbose|--timed|--AST-timed|--serialize|-quit)
1107             #This are Scilab6 dedicated options.
1108                     ;;
1109             -psn_*)
1110             # Under Mac OS X, .app is adding the arg -psn_0_xxxx
1111             # (psn = ProcessSerialNumber). It is not used (but could be in the future)
1112                 ;;
1113             -display|-f|-l|-e|-d|--parse-file)
1114                 prevarg=1
1115                 ;;
1116             -args) # Accept extra arguments
1117                 extraarg=1
1118                 ;;
1119             -debug)
1120                 DEBUG="-debug"
1121                 ;;
1122             -debug-kdbg)
1123                 DEBUG_KDBG="-debug-kdbg"
1124                 ;;
1125             -profiling)
1126                 PROFILING="-profiling"
1127                 ;;
1128             -profiling-visu)
1129                 PROFILING_VISU="-profiling-visu"
1130                 ;;
1131             -disable-exception-catching)
1132                 SCI_DISABLE_EXCEPTION_CATCHING=1
1133                 export SCI_DISABLE_EXCEPTION_CATCHING
1134                 ;;
1135             -h|--help)
1136                 do_help
1137                 ;;
1138             *)
1139                 foundUnknownArg=1
1140                 wrongArg=$sciarg
1141                 # Do nothing. Gives unknown args to Scilab binaries if -args is provided
1142                 ;;
1143         esac
1144     done
1145
1146
1147 # If the argument -a is not provided (extraarg) and we found unknown args
1148 # fail and provide help
1149     if test -z "$extraarg" -a -n "$foundUnknownArg"; then
1150         echo "Unknown argument: $wrongArg"
1151         echo ""
1152         do_help
1153     fi
1154
1155     if test -n "$DEBUG"; then
1156         start_debug "$@" $MORE_OPT
1157     else
1158         if test -n "$DEBUG_KDBG"; then
1159             start_debug_kdbg "$@" $MORE_OPT
1160         else
1161             if test -n "$PROFILING"; then
1162                 start_profiling "$@" $MORE_OPT
1163             else
1164                 if test -n "$PROFILING_VISU"; then
1165                     start_profiling_visu "$@" $MORE_OPT
1166                 else
1167                     do_scilex "$@" $MORE_OPT
1168                 fi # not profiling visu
1169             fi # not profiling
1170         fi # not debug kdbg
1171     fi # not debug
1172 fi # if rest