1#!@POSIX_SHELL@
2# -*- Shell-script -*-
3
4unixize () {
5    echo "$1" | sed -e 's%\\\\%/%g' | sed -e 's%^\([A-Za-z]\):%/\1%'
6}
7
8setup_vars() {
9  if [ -z "$MAXIMA_VERSION" ]; then
10    MAXIMA_VERSION="@VERSION@"
11  fi
12  prefix=`unixize "@prefix@"`
13  exec_prefix=`unixize "@exec_prefix@"`
14  PACKAGE=@PACKAGE@
15  top_srcdir=`unixize "@expanded_top_srcdir@"`
16  libdir=`unixize "@libdir@"`
17  if [ -n "$MAXIMA_LAYOUT_AUTOTOOLS" ]; then
18      layout_autotools="$MAXIMA_LAYOUT_AUTOTOOLS"
19  else
20      layout_autotools=@default_layout_autotools@
21  fi
22  if [ "$layout_autotools" = "true" ]; then
23      MAXIMA_DEFAULT_IMAGESDIR=$libdir/$PACKAGE/$MAXIMA_VERSION
24  else
25      MAXIMA_DEFAULT_IMAGESDIR=$top_srcdir/src
26  fi
27  if [ -d "$MAXIMA_DEFAULT_IMAGESDIR" ]; then
28      MAXIMA_IMAGESDIR="$MAXIMA_DEFAULT_IMAGESDIR"
29  else
30      if [ "$layout_autotools" = "true" ]; then
31	  MAXIMA_IMAGESDIR=`unixize "$MAXIMA_PREFIX"`/lib/$PACKAGE/$MAXIMA_VERSION
32      else
33	  MAXIMA_IMAGESDIR=`unixize "$MAXIMA_PREFIX"`/src
34      fi
35  fi
36  MAXIMA_DEFAULT_LISP=@DEFAULTLISP@
37  # If the the binary directory for the default lisp is not present,
38  # choose the first one we find.
39  if [ ! -d "$MAXIMA_IMAGESDIR/binary-$MAXIMA_DEFAULT_LISP" ]; then
40    MAXIMA_DEFAULT_LISP=`ls -1 $MAXIMA_IMAGESDIR 2>/dev/null | head -n 1 | sed 's/binary-//'`
41  fi
42  if [ -z "$MAXIMA_LISP" ]; then
43    MAXIMA_LISP=$MAXIMA_DEFAULT_LISP
44  fi
45}
46
47unsetup_vars () {
48  unset MAXIMA_IMAGESDIR
49  unset MAXIMA_LISP
50}
51
52process_userdir_argument() {
53    while [ -n "$1" ]; do
54        case $1 in
55        --userdir ) MAXIMA_USERDIR=$2 ; shift;;
56        --userdir=* ) MAXIMA_USERDIR=`echo "$1" | sed 's/--userdir=//'` ;;
57        esac
58        shift
59    done
60if [ -n "$MAXIMA_USERDIR" ]; then
61    export MAXIMA_USERDIR
62fi
63}
64
65# make a special check for --userdir argument, because it influences location
66# of maximarc, which is sourced before  other command-line options are
67# processed
68process_userdir_argument "$@"
69
70if [ -z "$MAXIMA_USERDIR" ]; then
71  maximarc_path="$HOME/.maxima/maximarc"
72else
73  maximarc_path="`unixize \"$MAXIMA_USERDIR\"`/maximarc"
74fi
75if [ -f "$maximarc_path" ]; then
76  . "$maximarc_path"
77fi
78
79# For some reason TeXmacs sets MAXIMA_DIRECTORY to the empty string,
80# which breaks maxima's internal path logic. This is a workaround.
81if [ -z "$MAXIMA_DIRECTORY" ]; then
82  unset MAXIMA_DIRECTORY
83fi
84
85# GCL in its default settings tends to run out of memory frequently quickly.
86# For example 8 Gigabytes of RAM aren't enough to run the testbench when sbcl
87# and clisp need <0,5 Gigabytes by default.
88# The following lines make GCL's garbage collector more aggressive by default.
89if [ -z "$GCL_GC_PAGE_THRESH" ]; then
90    export GCL_GC_PAGE_THRESH=0.2
91fi;
92if [ -z "$GCL_GC_ALLOC_MIN" ]; then
93    export GCL_GC_ALLOC_MIN=0.01
94fi;
95if [ -z "$GCL_GC_PAGE_MAX" ]; then
96    export GCL_GC_PAGE_MAX=0.05
97fi;
98
99
100# GCL also by default tells the garbage collector to limit the memory usage to
101# allocate half of the physical RAM installed in the current machine.
102# Which works fine in a single-user system and even takes in account the amount
103# of memory the other gcl processes use but is too far in the multitasking versus
104# speed tradeoff for us.
105#
106# See also:
107# https://lists.gnu.org/archive/html/gcl-devel/2017-09/msg00000.html
108if [ -z "$GCL_MEM_MULTIPLE" ]; then
109    export GCL_MEM_MULTIPLE=0.2
110fi;
111
112# Will be promoted to be gcl's default. But for the time being: Allow concurrent
113# gcl processes to communicate about how much memory each of them can use.
114if [ -z "$GCL_DISABLE_MULTIPROCESS_MEMORY_POOL" ]; then
115    export GCL_MULTIPROCESS_MEMORY_POOL=t
116fi;
117
118# process the command line arguments. This must be done in a function, because
119# "shift" should only shift the function parameters, not the global parameters.
120process_commandline_arguments() {
121    while [ -n "$1" ]; do
122	case $1 in
123	    -l|--lisp ) MAXIMA_LISP=$2 ; shift;;
124	    --lisp=*) MAXIMA_LISP=`echo "$1" | sed 's/--lisp=//'` ;;
125	    -u|--use-version ) MAXIMA_VERSION=$2 ; shift;;
126	    --use-version=*) MAXIMA_VERSION=`echo "$1" | sed 's/--use-version=//'` ;;
127	    -X|--lisp-options) MAXIMA_LISP_OPTIONS="$2" ; shift ;;
128	    --lisp-options=*) MAXIMA_LISP_OPTIONS=`echo "$1" | sed 's/--lisp-options=//'` ;;
129	    --userdir ) : ; shift;; # noop; handled by process_userdir_argument
130	    --userdir=*) : ;; # noop; handled by process_userdir_argument
131	    -v|--verbose ) verbose=true;;
132	    *) ;;
133	esac
134	shift
135    done
136}
137
138process_commandline_arguments "$@"
139
140
141# Allow ccl as an alias of openmcl.
142if [ "$MAXIMA_LISP" = "ccl" ]; then
143    MAXIMA_LISP=openmcl
144fi
145
146
147setup_vars
148
149if [ ! -d "$MAXIMA_IMAGESDIR" ]; then
150# Have we been moved?
151  MAXIMA_PREFIX=`(cd "\`dirname "$0"\`" 1>/dev/null 2>/dev/null; dirname "\`pwd\`")`
152  export MAXIMA_PREFIX
153  unsetup_vars
154  setup_vars
155  if [ ! -d "$MAXIMA_IMAGESDIR" ]; then
156    echo "$0: unable to determine MAXIMA_PREFIX" 1>&2
157    exit 1
158  fi
159fi
160
161# This step should only be necessary in order to be in sync with the windows
162# version of maxima.
163if [ -d "$MAXIMA_INITIAL_FOLDER" ]; then
164    cd "$MAXIMA_INITIAL_FOLDER"
165fi
166
167maxima_image_base="$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima"
168maxima_image_base_core="$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima_core"
169
170if [ "$verbose" = "true" ]; then
171    set -x
172fi
173if [ "$MAXIMA_LISP" = "clisp" ]; then
174    if [ "$layout_autotools" = "true" ]; then
175      if [ -x "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" ]; then
176        exec "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" $MAXIMA_LISP_OPTIONS -q "" -- "$@"
177      else
178	exec "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/@CLISP_RUNTIME@" $MAXIMA_LISP_OPTIONS -q -M "$maxima_image_base.mem" "" -- "$@"
179      fi
180    else
181      if [ -x "$maxima_image_base" ]; then
182        exec "$maxima_image_base"  $MAXIMA_LISP_OPTIONS -q  "" -- "$@"
183      else
184	exec "@CLISP_NAME@" $MAXIMA_LISP_OPTIONS -q -M "$maxima_image_base.mem" "" -- "$@"
185      fi
186    fi
187elif [ "$MAXIMA_LISP" = "cmucl" ]; then
188    # CMUCL can use either an executable image or a regular lisp core
189    # file.  Check for the executable image and run that if possible.
190    # Otherwise, fallback and use the lisp core file.
191    if [ "$layout_autotools" = "true" ]; then
192	if [ -x "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" ]; then
193	    exec "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" $MAXIMA_LISP_OPTIONS -quiet -- "$@"
194	else
195	    exec "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/@CMUCL_RUNTIME@" $MAXIMA_LISP_OPTIONS -quiet -core "$maxima_image_base_core" -eval '(cl-user::run)' -- "$@"
196	fi
197    else
198	if [ -x "$maxima_image_base" ]; then
199	    exec "$maxima_image_base" $MAXIMA_LISP_OPTIONS -quiet -- "$@"
200	else
201	    exec "@CMUCL_NAME@" $MAXIMA_LISP_OPTIONS -quiet -core "$maxima_image_base_core" -eval '(cl-user::run)' -- "$@"
202	fi
203    fi
204elif [ "$MAXIMA_LISP" = "scl" ]; then
205    if [ "$layout_autotools" = "true" ]; then
206	exec "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/@SCL_RUNTIME@" $MAXIMA_LISP_OPTIONS -quiet -core "$maxima_image_base_core" -eval '(cl-user::run)' -- "$@"
207    else
208	exec "@SCL_NAME@" $MAXIMA_LISP_OPTIONS -quiet -core "$maxima_image_base_core" -eval '(cl-user::run)' -- "$@"
209    fi
210elif [ "$MAXIMA_LISP" = "gcl" ]; then
211    exec "$maxima_image_base" -eval '(cl-user::run)' $MAXIMA_LISP_OPTIONS -f -- "$@"
212elif [ "$MAXIMA_LISP" = "acl" ]; then
213# FIXME: arguments need in a manner consistent with the way they are extracted
214#        in the function get-application-args in command-line.lisp
215    exec "@ACL_NAME@" -I "$maxima_image_base.dxl" $MAXIMA_LISP_OPTIONS -e '(cl-user::run)' -- "$@"
216elif [ "$MAXIMA_LISP" = "openmcl" ]; then
217# OPENMCL can use either an executable image or a regular lisp core
218# file.  Check for the executable image and run that if possible.
219# Otherwise, fallback and use the lisp core file.
220# FIXME: arguments need in a manner consistent with the way they are extracted
221#        in the function get-application-args in command-line.lisp
222    if [ -x "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" ]; then
223      exec "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" $MAXIMA_LISP_OPTIONS -e '(cl-user::run)' -- "$@"
224    else
225      exec "@OPENMCL_NAME@" -I "$maxima_image_base.image" $MAXIMA_LISP_OPTIONS -e '(cl-user::run)' -- "$@"
226   fi
227elif [ "$MAXIMA_LISP" = "ccl64" ]; then
228# Ccl64 can use either an executable image or a regular lisp core
229# file.  Check for the executable image and run that if possible.
230# Otherwise, fallback and use the lisp core file.
231# FIXME: arguments need in a manner consistent with the way they are extracted
232#        in the function get-application-args in command-line.lisp
233    if [ -x "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" ]; then
234      exec "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" $MAXIMA_LISP_OPTIONS -e '(cl-user::run)' -- "$@"
235    else
236      exec "@CCL64_NAME@" -I "$maxima_image_base.image" $MAXIMA_LISP_OPTIONS -e '(cl-user::run)' -- "$@"
237   fi
238
239elif [ "$MAXIMA_LISP" = "ecl" ]; then
240   exec "$maxima_image_base" --frame-stack 4096 --lisp-stack 65536 $MAXIMA_LISP_OPTIONS -- "$@"
241
242elif [ "$MAXIMA_LISP" = "abcl" ]; then
243    export MAXIMA_LISP_OPTIONS
244    export MAXIMA_IMAGESDIR
245    $MAXIMA_IMAGESDIR/binary-abcl/maxima "$@"
246
247elif [ "$MAXIMA_LISP" = "sbcl" ]; then
248# Use executable image if it exists.
249# Since compiling lapack already needed >1200 Megabytes of RAM in August of 2015
250# one has to extend the amount of memory sbcl will be able to claim by using the
251# switch --dynamic-space-size in order to do so.
252  if [ -x "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" ]; then
253    exec "$MAXIMA_IMAGESDIR/binary-$MAXIMA_LISP/maxima" --noinform $MAXIMA_LISP_OPTIONS --end-runtime-options --eval '(cl-user::run)' --end-toplevel-options "$@"
254  else
255    exec "@SBCL_NAME@" --core "$maxima_image_base_core" --noinform $MAXIMA_LISP_OPTIONS --end-runtime-options --eval '(cl-user::run)' --end-toplevel-options "$@"
256  fi
257
258else
259    echo "$0: lisp=\"$MAXIMA_LISP\" not known. Use --list-avail to see possible options." >&2
260    exit 1
261fi
262