#!/bin/sh
# -*- Shell-script -*-

setup_vars() {
  MAXIMA_VERSION=5.9.0.1cvs
  prefix=/usr
  exec_prefix=${prefix}
  PACKAGE=maxima
  VERSION=5.9.0.1cvs
  MAXIMA_DEFAULT_VERPKGLIBEXECDIR=/usr/lib/maxima/5.9.0.1cvs
  MAXIMA_DEFAULT_VERPKGLIBDIR=/usr/lib/maxima/5.9.0.1cvs
  if [ -z "$MAXIMA_VERPKGLIBEXECDIR" ]; then
    if [ -n "$MAXIMA_PREFIX" ]; then
      MAXIMA_VERPKGLIBEXECDIR="$MAXIMA_PREFIX/libexec/$PACKAGE/$VERSION"
    else
      MAXIMA_VERPKGLIBEXECDIR="$MAXIMA_DEFAULT_VERPKGLIBEXECDIR"
    fi
  fi
  if [ -z "$MAXIMA_VERPKGLIBDIR" ]; then
    if [ -n "$MAXIMA_PREFIX" ]; then
      MAXIMA_VERPKGLIBDIR="$MAXIMA_PREFIX/lib/$PACKAGE/$VERSION"
    else
      MAXIMA_VERPKGLIBDIR="$MAXIMA_DEFAULT_VERPKGLIBDIR"
    fi
  fi
  MAXIMA_DEFAULT_LISP=gcl
  # If the the binary directory for the default lisp is not present,
  # choose the first one we find.
  if [ ! -d "$MAXIMA_VERPKGLIBDIR/binary-$MAXIMA_DEFAULT_LISP" ]; then
    MAXIMA_DEFAULT_LISP=`ls -1 $MAXIMA_VERPKGLIBDIR | head -n 1 | sed 's/binary-//'`
  fi
  if [ -z "$MAXIMA_LISP" ]; then
    MAXIMA_LISP=$MAXIMA_DEFAULT_LISP
  fi
}

unsetup_vars () {
  unset MAXIMA_VERPKGLIBEXECDIR
  unset MAXIMA_VERPKGLIBDIR
  unset MAXIMA_LISP
}


if [ -z "$MAXIMA_USERDIR" ]; then
  maximarc_path="$HOME/.maxima/maximarc"
else
  maximarc_path="$MAXIMA_USERDIR/maximarc"
fi
if [ -f "$maximarc_path" ]; then
  . "$maximarc_path"
fi

# For some reason TeXmacs sets MAXIMA_DIRECTORY to the empty string,
# which breaks maxima's internal path logic. This is a workaround.
if [ -z "$MAXIMA_DIRECTORY" ]; then
  unset MAXIMA_DIRECTORY
fi

arg1=$1
arg2=$2
arg3=$3
arg4=$4
arg5=$5
arg6=$6
arg7=$7
arg8=$8
arg9=$9
while [ -n "$1" ]; do
    case $1 in 
	-l ) MAXIMA_LISP=$2 ; shift;;
	--lisp ) MAXIMA_LISP=$2 ; shift;;
	--lisp=*) MAXIMA_LISP=`echo "$1" | sed 's/--lisp=//'` ;;
	-u ) MAXIMA_VERSION=$2 ; shift;;
	--use-version ) MAXIMA_VERSION=$2 ; shift;;
	--use-version=*) MAXIMA_VERSION=`echo "$1" | sed 's/--use-version=//'` 
	    ;;
	-v ) verbose=true;;
	--verbose ) verbose=true;;
	*) ;;
    esac
    shift
done

setup_vars

if [ ! -d "$MAXIMA_VERPKGLIBEXECDIR" ]; then
# Have we been moved?
  MAXIMA_PREFIX="`dirname \`dirname $0\``"
  unsetup_vars
  setup_vars
  if [ ! -d "$MAXIMA_VERPKGLIBEXECDIR" ]; then
    echo "$0: unable to determine MAXIMA_PREFIX" 1>&2
    exit 1
  fi
fi

maxima_image_base="$MAXIMA_VERPKGLIBDIR/binary-$MAXIMA_LISP/maxima"

if [ "$verbose" = "true" ]; then
    set -x
fi
if [ "$MAXIMA_LISP" = "clisp" ]; then
    if [ "$in_maxima_local" = "true" ]; then
	exec "clisp" -q -M "$maxima_image_base.mem" "" -- "$arg1" "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" "$arg8" "$arg9"
    else
	exec "$MAXIMA_VERPKGLIBDIR/binary-$MAXIMA_LISP/lisp.run" -q -M "$maxima_image_base.mem" "" -- "$arg1" "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" "$arg8" "$arg9"
    fi
elif [ "$MAXIMA_LISP" = "cmucl" ]; then
    if [ "$in_maxima_local" = "true" ]; then
	exec "lisp" -quiet -core "$maxima_image_base.core" -eval '(user::run)' -- "$arg1" "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" "$arg8" "$arg9"
    else
	exec "$MAXIMA_VERPKGLIBDIR/binary-$MAXIMA_LISP/" -quiet -core "$maxima_image_base.core" -eval '(user::run)' -- "$arg1" "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" "$arg8" "$arg9"
    fi
elif [ "$MAXIMA_LISP" = "gcl" ]; then
    exec "$maxima_image_base" -eval '(user::run)' -f -- "$arg1" "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" "$arg8" "$arg9"
elif [ "$MAXIMA_LISP" = "acl6" ]; then
# FIXME: arguments need in a manner consistent with the way they are extracted
#        in the function get-application-args in command-line.lisp
    exec "lisp" -I "$maxima_image_base.dxl" -e '(user::run)' "$arg1" "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" "$arg8" "$arg9"
elif [ "$MAXIMA_LISP" = "openmcl" ]; then
# FIXME: arguments need in a manner consistent with the way they are extracted
#        in the function get-application-args in command-line.lisp
    exec "openmcl" -I "$maxima_image_base.image" "$arg1" "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" "$arg8" "$arg9"
elif [ "$MAXIMA_LISP" = "sbcl" ]; then
    exec "sbcl" --core "$maxima_image_base.core" --noinform --end-runtime-options --eval '(user::run)' --end-toplevel-options "$arg1" "$arg2" "$arg3" "$arg4" "$arg5" "$arg6" "$arg7" "$arg8" "$arg9"
else
    echo "$0: lisp=\"$MAXIMA_LISP\" not known. Use --list-avail to see possible options." >&2
    exit 1
fi
