#!/bin/sh
# Help function
###############
help () {
    cat <<EOF
Usage: factor-run IMAGE [ OPTIONS, FILES ]

Launch the factor runtime using an initial image IMAGE, taking
input from each FILE in turn and then starting a listener.

If IMAGE is -b or --basic, the basic image that is distributed
with factor will be used.  If IMAGE is -e or --extended, the
extended image that is distributed with factor will be used.

Library options:

  -resource-path=PATH
        Specify the path where factor libraries

  -shell={tty, ui, telnet}
        Specify what sort of listener to use.

  -port=N
  	When using the "telnet" listener, specify the port
  	to listen on.

Runtime options (n is a number):

  +Dn   Data stack size, kilobytes
  +Cn   Call stack size, kilobytes
  +Gn   Number of generations, must be >= 2
  +Yn   Size of n-1 youngest generations, megabytes
  +An   Size of tenured and semi-spaces, megabytes
  +Xn   Code heap size, megabytes
EOF
    exit 0
}

# Default values
################
BINARY_PATH='/usr/lib/factor'
RESOURCE_PATH='/usr/share/factor'
IMAGE="$BINARY_PATH/basic.image"
RUNTIME="$BINARY_PATH/runtime"

# Logic
#######
if test 0 -eq $#
then
    # As a 'quick start' behaviour, so that new users can
    # get something to play with immediately.
    echo "Launching the Factor runtime with a read-only basic image."
    echo "Use \`\"filename\" save-image' to write an editable image."
    exec "$RUNTIME" "$IMAGE" -resource-path="$RESOURCE_PATH"
else
    case "$1" in
        -h|--help)     
            help
            ;;
        -b|--basic)    
            IMAGE="$BINARY_PATH/basic.image"
            ;;
        -e|--extended) 
            IMAGE="$BINARY_PATH/extended.image"
            ;;
         *)            
            IMAGE="$1"
            ;;
    esac
    shift
    exec "$RUNTIME" "$IMAGE" -resource-path="$RESOURCE_PATH" "$@"
fi
