#!/usr/bin/env bash
# 
# Run the Rosie CLI

ROSIE_COMMAND=$1
shift
ROSIE_HOME=$1

if [[ -z ROSIE_HOME ]]; then
    echo "Internal error: missing required first argument (Rosie installation directory)"
    echo "This script should NOT be invoked directly.  It is exec\'d by bin/rosie."
    exit -1
fi

shift
executable="$ROSIE_HOME/bin/lua"

# Test to see if $ROSIE_HOME is a Rosie installation directory
if [[ ! -d "$ROSIE_HOME" ]]; then
    echo Error: $ROSIE_HOME is not a directory
    echo "This value is named in the executable script that launches rosie,"
    echo "and an invalid value suggests the rosie installation failed."
    echo "Maybe try uninstalling and reinstalling rosie?"
    exit -1
elif [[ ! -x "${executable}" ]]; then
    echo Error: ${executable} not found or not executable
    echo "The directory $ROSIE_HOME does not appear to contain a rosie installation"
    echo "because the file ${executable} is either missing or not executable."
    exit -1
fi


# -D is an 'undocumented' command line option that launches Rosie in development mode

i=""
dev="false"
if [[ "$1" = "-D" ]]; then
   i="-i"
   dev="true"
   shift
fi

export HOSTNAME
export HOSTTYPE
export OSTYPE
export PWD

CLI="$ROSIE_HOME/lib/cli.luac"
if [[ ! -r $CLI ]]; then
    CLI="$ROSIE_HOME/src/core/cli.lua"
    >&2 echo "Loading rosie from source"
fi

${executable} $i "$CLI" "$ROSIE_COMMAND" "$ROSIE_HOME" "$dev" "$@"


