#!/bin/sh
# Start a CMU CL lisp.
# This script was suppled by David Axmark.  It supports starting various
# different versions of CMU CL, and is used by his build-and-install script.
# It may contain Sun dependencies.

MY_BASEDIR=${MY_BASEDIR:-/my}
BASE_DIR=$MY_BASEDIR/cmucl

# We require all arguments to this script to come before other
# arguments.  This is so we dont have to try to pass on arguments
# with spaces, quotes and so on in them.

VERSION=
VARIANT=

# Default version/variant may be changed in the environment.
if test -n "$CMUCL_VERSION"; then VERSION=-$CMUCL_VERSION; fi
if test -n "$CMUCL_VARIANT"; then VARIANT=-$CMUCL_VARIANT; fi

while test -n "$1" -a -z "$END_LOOP"
do
	case $1
	in
		-version)	VERSION=-$2; shift; shift;;
		-variant)	VARIANT=-$2; shift; shift;;
		-prod)	    	PROD=t; shift;;
		*)		END_LOOP=t;;
	esac
done

if test -n "$VERSION" -a ! -d "$BASE_DIR/release$VERSION"
then
	echo "Could not find lisp version $VERSION."
	if test -n "LISP_MUST_HAVE_RIGHT_CORE"
	then
		exit 1
	fi
	echo "Trying to use default version"
	VERSION=
fi

if test -n "$PROD"
then
	BASE=$MY_BASEDIR/production/interview
else
	BASE=$BASE_DIR/release$VERSION
fi

LISP=$BASE/bin/lisp
CORE=$BASE/lib/lisp.core
CMUCLLIB=$BASE/lib

PATH=$BASE/bin:$PATH

# Avoid tmpfs filesystems for CMUCL_EMPTYFILE
CMUCL_EMPTYFILE=/var/tmp/empty

export PATH CMUCLLIB CMUCL_EMPTYFILE

# Use this systems version as default if it exists. Ex if we are running
# under /my/ok we will use the -ok core.
MY_VERSION=`basename ${MY_BASEDIR_VERSION:-/}`
if test -n "$MY_VERSION" -a -f $CORE$VARIANT-$MY_VERSION
then
	VARIANT="$VARIANT-$MY_VERSION"
fi

if test -n "$VARIANT" -a ! -f $CORE$VARIANT
then
	echo "Could not find lisp variant $CORE$VARIANT."
	if test -n "LISP_MUST_HAVE_RIGHT_CORE"
	then
		exit 1
	fi
	echo "Trying to use default variant"
	VARIANT=
else
	CORE="$CORE$VARIANT"
fi

exec $LISP -core $CORE "$@"
