#!/usr/bin/env bash
# Apache Spark-compatible example launcher backed by native spark-rust ports.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
RUNNER="${SPARK_EXAMPLE_RUNNER:-$ROOT/bin/spark-example-runner}"
if [[ ! -x "$RUNNER" ]]; then
  release="$ROOT/target/release/spark-example-runner"
  debug="$ROOT/target/debug/spark-example-runner"
  if [[ -x "$debug" && ( ! -x "$release" || "$debug" -nt "$release" ) ]]; then
    RUNNER="$debug"
  else
    RUNNER="$release"
  fi
fi

usage() {
  cat <<'EOF'
Usage: bin/run-example [options] example-class [example args]

Options accepted for Apache Spark CLI compatibility:
  --master MASTER          accepted; native ports run in-process
  --deploy-mode MODE       only client mode is supported
  --name NAME              accepted
  --conf KEY=VALUE         exported for the embedded session
  --driver-memory VALUE    accepted for compatibility
  --executor-memory VALUE  accepted for compatibility
  --executor-cores N       accepted for compatibility
  --num-executors N        accepted for compatibility
  --jars/--packages VALUE  accepted but not used by native ports
  --list                   list supported example classes
  --describe CLASS         describe one example

Examples:
  bin/run-example SparkPi 10
  bin/run-example org.apache.spark.examples.SparkPageRank \
    examples/run-example/data/pagerank_data.txt 10
EOF
}

RUNNER_ARGS=()
while (($#)); do
  case "$1" in
    --help|-h) usage; exit 0 ;;
    --list) RUNNER_ARGS+=(--list); shift ;;
    --describe) RUNNER_ARGS+=(--describe "${2:?--describe requires a class}"); shift 2 ;;
    --master|--name|--driver-memory|--executor-memory|--executor-cores|--num-executors|--jars|--packages)
      [[ $# -ge 2 ]] || { echo "run-example: $1 requires a value" >&2; exit 2; }
      [[ "$1" == --master ]] && export SPARK_MASTER="$2"
      shift 2 ;;
    --deploy-mode)
      [[ $# -ge 2 ]] || { echo 'run-example: --deploy-mode requires a value' >&2; exit 2; }
      [[ "$2" == client ]] || { echo 'run-example: native examples support only --deploy-mode client' >&2; exit 2; }
      shift 2 ;;
    --conf)
      [[ $# -ge 2 && "$2" == *=* ]] || { echo 'run-example: --conf requires KEY=VALUE' >&2; exit 2; }
      key="${2%%=*}"; value="${2#*=}"
      # Preserve conf for child code without inventing shell variable names.
      export "SPARK_CONF_${key//[^A-Za-z0-9_]/_}=$value"
      shift 2 ;;
    --*) echo "run-example: unsupported option: $1" >&2; exit 2 ;;
    *) break ;;
  esac
done

if [[ ${#RUNNER_ARGS[@]} -eq 0 ]]; then
  [[ $# -gt 0 ]] || { usage >&2; exit 2; }
  RUNNER_ARGS+=("$@")
fi

if [[ ! -x "$RUNNER" ]]; then
  if [[ -f "$ROOT/Cargo.toml" ]]; then
    echo '==> building spark-example-runner (one-time)' >&2
    (cd "$ROOT" && cargo build -q -p spark-cli --bin spark-example-runner) >&2
    RUNNER="$ROOT/target/debug/spark-example-runner"
  else
    echo 'run-example: spark-example-runner is missing; reinstall the administrator archive' >&2
    exit 1
  fi
fi
exec "$RUNNER" "${RUNNER_ARGS[@]}"
