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