#!/usr/bin/env sh
# 供 run-login.sh / run-car.sh source。
# macOS 自带 /usr/bin/java 常为占位，会报 “Unable to locate a Java Runtime”。
# 本脚本优先把 Homebrew 安装的 OpenJDK 放到 PATH 前面，并设置 JAVA_HOME（若存在标准目录）。

export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/local/sbin:$PATH"

for _j in \
  "/opt/homebrew/opt/openjdk@17/bin/java" \
  "/opt/homebrew/opt/openjdk@21/bin/java" \
  "/opt/homebrew/opt/openjdk/bin/java" \
  "/usr/local/opt/openjdk@17/bin/java" \
  "/usr/local/opt/openjdk/bin/java"
do
  if [ -x "$_j" ]; then
    _bindir="$(dirname "$_j")"
    _prefix="$(dirname "$_bindir")"
    export PATH="$_bindir:$PATH"
    if [ -d "$_prefix/libexec/openjdk.jdk/Contents/Home" ]; then
      export JAVA_HOME="$_prefix/libexec/openjdk.jdk/Contents/Home"
    elif [ -d "$_prefix" ]; then
      export JAVA_HOME="$_prefix"
    fi
    break
  fi
done

unset _j _bindir _prefix
