#! /bin/sh
JACALDIR=/usr/local/lib/jacal/
VERSION=1b5

##"jacal" sh script; Find a Scheme implementation and run JACAL in it.
# Copyright (C) 2001, 2003, 2004 Aubrey Jaffer
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

usage="Usage: jacal [--version | -v]

  Display version information and exit successfully.

Usage: jacal SCHEME

  Start JACAL using Scheme implementation SCHEME.

Usage: jacal

  Start JACAL using executable 'scheme', 'scm', 'mzscheme', 'guile',
  'gsi' or 'slib48'."

case "$1" in
    -v | --ver*) echo jacal "$VERSION"; exit 0;;
    "") if type scheme>/dev/null 2>&1; then
	  command=scheme
	fi;;
    -*) echo "$usage"; exit 1;;
    *) command="$1"
    shift
esac
# If more arguments are supplied, then err out.
if [ ! -z "$1" ]; then
    echo "$usage"; exit 1
fi

if [ -z "$command" ]; then
    if type scm>/dev/null 2>&1; then
	command=scm; implementation=scm
    elif type gsi>/dev/null 2>&1; then
	command=gsi; implementation=gam
    elif type mzscheme>/dev/null 2>&1; then
	command=mzscheme; implementation=plt
    elif type guile>/dev/null 2>&1; then
	command=guile; implementation=gui
    elif type slib48>/dev/null 2>&1; then
	echo "do 'cd ${JACALDIR}; make jacal48'"
    elif type scheme48>/dev/null 2>&1; then
	echo "do 'cd ${SCHEME_LIBRARY_PATH}; make slib48'"
	echo "do 'cd ${JACALDIR}; make jacal48'"
    elif type scmlit>/dev/null 2>&1; then
	command=scmlit; implementation=scm
    elif type elk>/dev/null 2>&1; then
	command=elk; implementation=elk
    else
	echo No Scheme implementation found.
	exit 1
    fi
# Gambit 4.0 doesn't allow input redirection; foils --version test.
elif [ "$command" == "gsi" ]; then implementation=gam
elif type $command>/dev/null 2>&1; then
  SPEW="`$command --version < /dev/null 2>&1`"
  if   echo ${SPEW} | grep 'Initialize load-path (colon-list of directories)'\
				       >/dev/null 2>&1; then implementation=elk
  elif echo ${SPEW} | grep 'MIT'       >/dev/null 2>&1; then implementation=mit
  elif echo ${SPEW} | grep 'UMB Scheme'>/dev/null 2>&1; then implementation=umb
  elif echo ${SPEW} | grep 'scheme48'  >/dev/null 2>&1; then implementation=s48
  elif echo ${SPEW} | grep 'MzScheme'  >/dev/null 2>&1; then implementation=plt
  elif echo ${SPEW} | grep 'Guile'     >/dev/null 2>&1; then implementation=gui
  elif echo ${SPEW} | grep 'SCM'       >/dev/null 2>&1; then implementation=scm
  else implementation=
  fi
else
  echo "Program '$command' not found."
  exit 1
fi

case $implementation in
  scm);;
  s48);;
  *) if [ -z "${SCHEME_LIBRARY_PATH}" ]; then
	if type rpm>/dev/null 2>&1; then
	  SCHEME_LIBRARY_PATH=`rpm -ql slib 2>/dev/null \
	     | grep require.scm | sed 's%require.scm%%'`
	fi
     fi
     if [ -z "${SCHEME_LIBRARY_PATH}" ]; then
       if [ -d /usr/local/lib/slib/ ]; then
	  SCHEME_LIBRARY_PATH=/usr/local/lib/slib/
       elif [ -d /usr/share/slib/ ]; then
	  SCHEME_LIBRARY_PATH=/usr/share/slib/
       fi
     export SCHEME_LIBRARY_PATH
     fi;;
esac

# for gambit
case $implementation in
  gam) if [ -z "${LD_LIBRARY_PATH}" ]; then
	LD_LIBRARY_PATH=/usr/local/lib
	export LD_LIBRARY_PATH
	fi;;
esac

case $implementation in
  scm) echo $command -ip1 -l ${JACALDIR}go "$@"
       exec $command -ip1 -l ${JACALDIR}go "$@";;
  elk) echo $command -i -l ${JACALDIR}elk.scm
       exec $command -i -l ${JACALDIR}elk.scm;;
  gam) echo $command -:s ${SCHEME_LIBRARY_PATH}gambit.init ${JACALDIR}go.scm "$@"
       exec $command -:s ${SCHEME_LIBRARY_PATH}gambit.init ${JACALDIR}go.scm "$@";;
  gui) echo $command -l ${SCHEME_LIBRARY_PATH}guile.init -l ${JACALDIR}guile.scm "$@"
       exec $command -l ${SCHEME_LIBRARY_PATH}guile.init -l ${JACALDIR}guile.scm "$@";;
  plt) echo $command -f ${SCHEME_LIBRARY_PATH}DrScheme.init -f ${JACALDIR}go.scm "$@"
       exec $command -f ${SCHEME_LIBRARY_PATH}DrScheme.init -f ${JACALDIR}go.scm "$@";;
  mit) echo $command -load ${SCHEME_LIBRARY_PATH}mitscheme.init -load ${JACALDIR}go "$@"
       exec $command -load ${SCHEME_LIBRARY_PATH}mitscheme.init -load ${JACALDIR}go "$@";;
  umb) echo umb-scheme does not run jacal; exit 1;;
  s48) if [ -f "${JACALDIR}scheme48.image" ]; then
	 echo ";;; Type (math) to begin."
	 echo scheme48 -h 4500000 -i ${JACALDIR}scheme48.image
	 exec scheme48 -h 4500000 -i ${JACALDIR}scheme48.image
       else
	 echo "scheme48 found; do: 'cd ${JACALDIR}; make jacal48'";
       fi
       exit 1;;
  *)   echo Program '$command' ??
       exit 1;;
esac
