#!/bin/sh
set -e

GERBIL_GSI="gsi"

if [ -z "$GERBIL_HOME" ]; then
    # The path below is set by configure --prefix=...
    GERBIL_HOME=$(cd ${0%/*}; echo "${PWD%/*}") # Won't work if a symlink; use configure --prefix then.
    export GERBIL_HOME
fi

if [ $# -gt 0 ]; then
    case $1 in
        -h)
            echo "gxi [-v|-h] | [-:<runtime-options>] [[-] [-e expr>] [file]] ..."
            exit 0
            ;;
        -v)
            exec "$GERBIL_GSI" "$GERBIL_HOME/lib/gxi-init" -e '(displayln gerbil-greeting)'
            ;;
     esac
fi

if [ $# -gt 0 ]; then
    case $1 in
        -:*)
            GSIOPTIONS=$1
            shift
            ;;
    esac
fi

if [ $# -gt 0 ]; then
    case $1 in
        --lang)
            export GERBIL_LANG=$2
            shift 2
            ;;
    esac
fi

if [ $# -eq 0 ]; then
    exec "$GERBIL_GSI" $GSIOPTIONS "$GERBIL_HOME/lib/gxi-init" "$GERBIL_HOME/lib/gxi-interactive" -
else
    exec "$GERBIL_GSI" $GSIOPTIONS "$GERBIL_HOME/lib/gxi-init" "$@"
fi
