#!/bin/sh
commandExists () {
    command -v "$1" > /dev/null
}

# Obtain $COMMAND
while getopts ":l:" optchar; do
  case $optchar in
    l)
      COMMAND="$OPTARG"
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

if commandExists $COMMAND ; then
  COMMAND=$COMMAND;
elif commandExists luajit ; then
  COMMAND=luajit;
elif commandExists lua ; then
  COMMAND=lua;
else
  echo >&2 "Busted requires that a valid execution environment be specified(or that you have lua or luajit accessible in your PATH). Aborting.";
  exit 1
fi

# Obtain $BOOSTRAP_PATH
DIR="$( dirname "$0" )"
BOOTSTRAP_PATH="$DIR/busted_bootstrap"

if commandExists cygpath ; then
  BOOTSTRAP_PATH=`cygpath -ms "${BOOTSTRAP_PATH}"`
fi

# Execution
$COMMAND $BOOTSTRAP_PATH $*
