#!/bin/sh

usage () {
	cat << EOF
usage: maxima [options]
options:
    -h, --help: Display this usage message.
    -l <lisp>, --lisp=<lisp>: Use lisp implementation <lisp>. Use --list-avail
                              to see the list of possible values.
    -u <version>, --use-version=<version>: Launch maxima version <version>.
                                           Use --list-avail to see the list
                                           of possible values.
    --list-avail: List the installed versions and lisp implementations.
    -b <file>, --batch=<file>: Process <file> in maxima batch mode.
    --batch-lisp=<file>: Process lisp file <file> in batch mode.
    --batch-string=<string>: Process <string> in maxima batch mode.
    -r <string>, --run-string=<string>: Process <string> in maxima interactive
                                        mode.
    -p <lisp_file>, --preload-lisp=<lisp_file>: Preload <lisp_file>
    -v, --verbose: Print extra information from the maxima wrapper script.
    --version: Print the (default) installed version.
EOF
}

setup_vars() {
  # Tell maxima-run-lisp what binaries to run for the various Lisps.
  CLISP=clisp
  CMUCL=lisp
  GCL=gcl
  export CLISP CMUCL GCL
  MAXIMA_VERSION=5.9.0
  prefix=/usr/local
  exec_prefix=${prefix}
  PACKAGE=maxima
  VERSION=5.9.0
  MAXIMA_DEFAULT_VERPKGLIBEXECDIR=${exec_prefix}/libexec/maxima/5.9.0
  MAXIMA_DEFAULT_VERPKGLIBDIR=${exec_prefix}/lib/maxima/5.9.0
  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=clisp
  # 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
}

list_lisps () {
  echo "The code to list lisp implementations needs to be written"
}

list_versions () {
  echo "The code to list versions needs to be written"
}

process_args () {
  batch_file=
  batch_lisp_file=
  batch_string=
  run_string=
  preload_lisp_file=
  list_avail=
  show_version=
  while getopts "hl:u:b:r:p:v-:" opt; do
    if [ "$opt" = "-" ]; then
      opt=$OPTARG
    fi
    case $opt in
      h | help)
		usage
		exit
		;;
      l | lisp=*)
		  if [ "$opt" = "l" ]; then
		    MAXIMA_LISP=$OPTARG
		  else
		    MAXIMA_LISP=`echo "$OPTARG" | sed 's/lisp=//'`
		  fi
		  if [ -z "$MAXIMA_LISP" ]; then
		    list_lisps
		    exit
		  fi
		  ;;
      u | use-version=*)
		  if [ "$opt" = "u" ]; then
		    MAXIMA_VERSION=$OPTARG
		  else
		    MAXIMA_VERSION=`echo "$OPTARG" | sed 's/use-version=//'`
		  fi
		  if [ -z "$MAXIMA_VERSION" ]; then
		    list_versions
		    exit
		  fi
		  ;;
      list-avail)
		  list_avail=true
		  ;;
      b | batch=*)
		   if [ "$opt" = "b" ]; then
		     batch_file=$OPTARG
		   else
		     batch_file=`echo "$OPTARG" | sed 's/batch=//'`
		   fi
		   ;;
      batch-lisp=*)
		    batch_lisp_file=`echo "$OPTARG" | sed 's/batch-lisp=//'`
		    ;;
      batch-string=*)
		      batch_string=`echo "$OPTARG" | sed 's/batch-string=//'`
		      ;;
      r | run-string=*)
			if [ "$opt" = "r" ]; then
			  run_string=$OPTARG
			else
			  run_string=`echo "$OPTARG" | sed 's/run-string=//'`
			fi
			;;
      p | preload-lisp=*)
			if [ "$opt" = "p" ]; then
			  preload_lisp_file=$OPTARG
			else
			  preload_lisp_file=`echo "$OPTARG" | sed 's/preload-lisp=//'`
			fi
			;;
      v | verbose)
	       verbose=true
	       ;;
      version)
	       show_version=true
	       ;;
      *)
	 echo "Option $1 not recognized" 1>&2
	 usage
	 exit 1
	 ;;
    esac
  done
}

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

process_args "$@"
setup_vars

if [ -n "$show_version" ]; then
  echo "Maxima $VERSION"
  exit 0
fi

if [ -n "$list_avail" ]; then
  if [ -n "$in_maxima_local" ]; then
    echo "The --list-avail option is only available once maxima is installed."
    exit 1
  fi
  echo "Available versions:"
  versions=`ls -1 \`dirname "$MAXIMA_VERPKGLIBDIR"\``
  for version in $versions
  do
    binary_lisps=`ls -1 \`dirname "$MAXIMA_VERPKGLIBDIR"\`/$version`
    for binary_lisp in $binary_lisps
    do
      lisp=`echo $binary_lisp | sed 's/binary-//'`
      echo "version $version, lisp $lisp"
    done
  done
  exit 0
fi

LISPTYPE=$MAXIMA_LISP
export LISPTYPE

if [ ! -x "$MAXIMA_VERPKGLIBEXECDIR/maxima-run-lisp" ]; then
# Have we been moved?
  MAXIMA_PREFIX="`dirname \`dirname $0\``"
  unsetup_vars
  setup_vars
  if [ ! -x "$MAXIMA_VERPKGLIBEXECDIR/maxima-run-lisp" ]; then
    echo "$0: unable to determine MAXIMA_PREFIX" 1>&2
    exit 1
  fi
fi
  
unset MAXIMA_INT_LISP_PRELOAD
unset MAXIMA_INT_INPUT_STRING
unset MAXIMA_INT_BATCH_FLAG
if [ -n "$batch_lisp_file" ]; then
  MAXIMA_INT_INPUT_STRING=":lisp (load \"$batch_lisp_file\")"
  MAXIMA_INT_BATCH_FLAG=":batch"
fi
if [ -n "$batch_file" ]; then
  MAXIMA_INT_INPUT_STRING="batch(\"$batch_file\");"
  MAXIMA_INT_BATCH_FLAG=":batch"
fi
if [ -n "$batch_string" ]; then
  MAXIMA_INT_INPUT_STRING="$batch_string"
  MAXIMA_INT_BATCH_FLAG=":batch"
fi
if [ -n "$run_string" ]; then
  MAXIMA_INT_INPUT_STRING="$run_string"
fi
if [ -n "$preload_lisp_file" ]; then
  MAXIMA_INT_LISP_PRELOAD="$preload_lisp_file"
fi
export MAXIMA_INT_LISP_PRELOAD
export MAXIMA_INT_INPUT_STRING
export MAXIMA_INT_BATCH_FLAG

command="$MAXIMA_VERPKGLIBEXECDIR/maxima-run-lisp -run $MAXIMA_VERPKGLIBDIR/binary-$LISPTYPE/maxima"

if [ "$verbose" = "true" ]; then
  echo "MAXIMA_INT_LISP_PRELOAD=$MAXIMA_INT_LISP_PRELOAD"
  echo "MAXIMA_INT_INPUT_STRING=$MAXIMA_INT_INPUT_STRING"
  echo "MAXIMA_INT_BATCH_FLAG=$MAXIMA_INT_BATCH_FLAG"
  echo "command=$command"
fi
exec $command
