#!/usr/bin/env bash
set -euo pipefail

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUNTIME="$DIR/wurst-runtime/bin/java"

JAR="$DIR/grill-cli/grill.jar"
if [[ ! -f "$JAR" ]]; then
  JAR="$DIR/../grill-cli/grill.jar"
fi

if [[ ! -f "$JAR" ]]; then
  echo "[grill] ERROR: Missing jar. Searched:"
  echo "  $DIR/grill-cli/grill.jar"
  echo "  $DIR/../grill-cli/grill.jar"
  exit 2
fi

if [[ ! -x "$RUNTIME" ]]; then
  echo "[grill] ERROR: Bundled runtime not found or not executable at:"
  echo "  $RUNTIME"
  echo "Please reinstall wurstscript via the VSCode extension."
  exit 3
fi

exec "$RUNTIME" -Dfile.encoding=UTF-8 --enable-native-access=ALL-UNNAMED -jar "$JAR" "$@"
