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