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

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

JAR="$DIR/wurst-compiler/wurstscript.jar"
if [[ ! -f "$JAR" ]]; then
  JAR="$DIR/../wurst-compiler/wurstscript.jar"
fi

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

if [[ ! -x "$RUNTIME" ]]; then
  echo "[wurstscript] 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 -jar "$JAR" "$@"
