#! /bin/sh
JACALDIR=/usr/local/lib/jacal/

##"jacal"; Find a Scheme implementation and run JACAL in it.
# Copyright 2001 Aubrey Jaffer
# See the file "COPYING" for terms applying to this program

if [ -z "$1" ]; then
    if [ -f "${JACALDIR}scheme48.image" ]; then
	echo ";;; Type (math) to begin."
	echo "scheme48 -h 4000000 -i ${JACALDIR}scheme48.image"
	exec scheme48 -h 4000000 -i ${JACALDIR}scheme48.image
    elif type scheme>/dev/null 2>&1; then
        command=scheme
    fi
    else
    command="$1"
    shift
fi

if type $command>/dev/null 2>&1; then
  SPEW="`$command --version < /dev/null 2>&1`"
  if echo ${SPEW} | grep -q 'Initialize load-path (colon-list of directories)' \
					   ; then implementation=elk
  elif echo ${SPEW} | grep -q 'MIT Scheme' ; then implementation=mit
  elif echo ${SPEW} | grep -q 'UMB Scheme' ; then implementation=umb
  elif echo ${SPEW} | grep -q 'scheme48'   ; then implementation=s48
  elif echo ${SPEW} | grep -q 'MzScheme'   ; then implementation=plt
  elif echo ${SPEW} | grep -q 'Guile'      ; then implementation=gui
  elif echo ${SPEW} | grep -q 'SCM'        ; then implementation=scm
  else command=
  fi
elif [ ! -z "$1" ]; then
  echo "Program '$1' not found."
  exit 1
fi

if [ -z "$command" ];
then
    if type scm>/dev/null 2>&1; then
	command=scm; implementation=scm
    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'"
    else
	echo No Scheme implementation found.
	exit 1
    fi
fi

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

case $implementation in
    scm) echo -n "$command $* -ip1 -l ${JACALDIR}go "
	 exec $command $* -ip1 -l ${JACALDIR}go;;
    plt) echo "$command $* -L init.ss slibinit -f ${JACALDIR}go.scm"
	 exec $command $* -L init.ss slibinit -f ${JACALDIR}go.scm;;
    gui) echo -n "$command $* -l ${SCHEME_LIBRARY_PATH}guile.init -l ${JACALDIR}guile.scm "
	 exec $command $* -l ${SCHEME_LIBRARY_PATH}guile.init -l ${JACALDIR}guile.scm;;
    mit) echo -n "$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 "${JACALDIR}scheme48.image exists; do: 'jacal'";
	 else
	   echo "scheme48 found; do: 'cd ${JACALDIR}; make jacal48'";
	 fi
	 exit 1;;
esac
