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

ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$ROOT_DIR/bsphp.java.car"

# 优先使用 Homebrew OpenJDK，避免 macOS /usr/bin/java 占位程序
# shellcheck source=setup-java-env.sh
source "$ROOT_DIR/setup-java-env.sh"

if ! command -v java >/dev/null 2>&1 || ! java -version >/dev/null 2>&1; then
  echo "ERROR: 未找到可用的 Java 运行时（JDK）。"
  echo "请安装 OpenJDK，例如："
  echo "  brew install openjdk@17"
  echo "然后重开终端，再执行: ./run-car.sh"
  exit 1
fi

if ! command -v mvn >/dev/null 2>&1; then
  echo "ERROR: 未找到 mvn 命令。"
  echo "请先安装 Maven: brew install maven"
  echo "安装后建议用: ./run-car.sh（不要用 sh run-car.sh）"
  exit 1
fi

echo "==> 构建 bsphp.java.car ..."
cd "$PROJECT_DIR"
mvn -DskipTests package dependency:copy-dependencies

echo "==> 启动 bsphp.java.car ..."
exec java -cp "target/classes:target/dependency/*" com.bsphp.login.BsphpLoginApp

