#! /bin/sh
# Determine which Scheme implementation should be started to run an Icslas
# subsystem (a Scheme to C compiler, a byte-code compiler, etc.)

SYNTAX="\
cxla [ + [ SCHEME=YourPreferredScheme ] [ TARGET=dir ]] ...
YourPreferredScheme may also be specified with the CXLA_SCHEME variable
or found from an built-in list of well known Scheme implementations.
The TARGET directory tells where is the runtime for the compiler.
NOTE: You should use absolute pathnames.
"

# This allows to interact with the Scheme interpreter right after its
# work. You may trigger it if saying + as first option to cxla. 

INTERACT=false

# First, we must determine which Scheme to use.  It may be specified
# with the CXLA_SCHEME environment variable or it may be specified by
# the first argument if this first argument has the shape
# SCHEME=bigloo or SCHEME=sci. By default, we look for a well known
# Scheme system among the following list.

WELL_KNOWN_SCHEMES="bigloo++ bigloo oscheme scc++ sci+ sci scc scm elk stk gsi umb-scheme fools mit-scheme"

while [ $# -gt 1 ] 
do
     case "X$1" in
       XSCHEME=*)       SCHEME=` expr $1 : "SCHEME=\(.*\)" `
                        shift
                        ;;
       XTARGET=*)       TARGET=` expr $1 : "TARGET=\(.*\)" `
                        shift
                        ;;
       X+)      INTERACT=true
                shift 
                ;;
       *)  if [ -n "${SCHEME}" ]
           then break
           else if [ -n "${CXLA_SCHEME}" ]
             then SCHEME=${CXLA_SCHEME}
                  break
             else
                for s in ${WELL_KNOWN_SCHEMES}
                   do f=`which $s` 2>/dev/null
                      # 'which' is tricky wrt csh, sh
                      if [ -f "$f" ]
                      then SCHEME=$s
                           break
                      fi
                   done
             fi
           fi
           if [ -z "${SCHEME}" ]
           then 
                echo "I can't determine which Scheme you want to use."
                echo ${SYNTAX}
                exit 1
           else
                break
           fi
           ;;
     esac
done

# It is necessary to know where is the runtime library.
# Normally this directory also contains some config.{sh,scm,mkf} files.
# To be able to load the compilers, we jump to the TOP directory so
# files must be given with their absolute pathname.

if [ -f ${TARGET}/config.scm ]
then    
        . ${TARGET}/config.sh
        cd ${TOP}
else
        echo "[Missing/Wrong TARGET variable] I need to know where is the compiled runtime."
        exit 11
fi

# Now pass the rest of the options to a Scheme function that will
# determine what to do exactly. Options are passed as strings to
# respect case sensitivity.

( echo "(begin" 
  cat ${TARGET}/config.scm
  cat icslas.scm 
  cat Bind/runxla.scm
  echo "(runxla (quote ${SCHEME}) (quote ( "
  for arg in "$@" ;
   do echo " \"$arg\"" ;
   done ;
  echo ")) ) )" ;
  # Allow the user to add its own programs
  if ${INTERACT} ; then tee ; else true ; fi ;
  echo "(begin (display \";;; End of Icslas multi-system.\")(newline))" ;
  ) | ${SCHEME}

#end of cxla
