#!/bin/bash
# ============================================
# 一键打包 Debug APK
# 用法: bash build.sh
# ============================================
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"

# ---------- 检测 Java 8 ----------
JAVA8_HOME=""
if command -v /usr/libexec/java_home &>/dev/null; then
    JAVA8_HOME=$(/usr/libexec/java_home -v 1.8 2>/dev/null || true)
fi
if [ -z "$JAVA8_HOME" ]; then
    for candidate in \
        /Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home \
        /Library/Java/JavaVirtualMachines/jdk1.8.0_*.jdk/Contents/Home; do
        for dir in $candidate; do
            [ -f "$dir/bin/javac" ] && JAVA8_HOME="$dir" && break 2
        done
    done
fi
if [ -z "$JAVA8_HOME" ]; then
    echo "❌ 未找到 Java 8，请先安装: brew install --cask temurin8"
    exit 1
fi

export JAVA_HOME="$JAVA8_HOME"
echo "JAVA_HOME=$JAVA_HOME"

# ---------- 构建 ----------
echo ""
echo ">>> 开始编译 Debug APK ..."
./gradlew clean assembleDebug

# ---------- 输出路径 ----------
echo ""
echo "==========================================="
echo "  ✅ 构建完成"
echo "  APK: app/build/outputs/apk/debug/app-debug.apk"
echo "==========================================="
