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

# Minimal Android SDK bootstrap for local debug builds
# - Installs command-line tools via Homebrew if missing
# - Installs platform-tools, build-tools 34.0.0, and platform android-34
# - Prints the env vars to export in your shell config

command -v brew >/dev/null 2>&1 || { echo "Homebrew est requis (https://brew.sh)"; exit 1; }

if ! brew list --cask android-commandlinetools >/dev/null 2>&1; then
  echo "→ Installation des Android command-line tools via Homebrew..."
  brew install --cask android-commandlinetools
fi

if ! brew list --cask temurin17 >/dev/null 2>&1; then
  echo "→ Installation de Java 17 (Temurin)..."
  brew install --cask temurin17
fi

ANDROID_SDK_ROOT="${HOME}/Library/Android/sdk"
mkdir -p "${ANDROID_SDK_ROOT}"

TOOLS_DIR="$(brew --prefix)/share/android-commandlinetools"
yes | "${TOOLS_DIR}/bin/sdkmanager" --sdk_root="${ANDROID_SDK_ROOT}" --licenses >/dev/null

echo "→ Installation des composants SDK (platform-tools, build-tools 34.0.0, platform android-34)..."
yes | "${TOOLS_DIR}/bin/sdkmanager" --sdk_root="${ANDROID_SDK_ROOT}" \
  "platform-tools" "build-tools;34.0.0" "platforms;android-34"

cat <<EOF

✅ SDK installé dans ${ANDROID_SDK_ROOT}

Ajoute ces lignes à ton shell (~/.zshrc ou ~/.bashrc) :
  export ANDROID_SDK_ROOT="${ANDROID_SDK_ROOT}"
  export ANDROID_HOME="${ANDROID_SDK_ROOT}"
  export PATH="${ANDROID_SDK_ROOT}/platform-tools:\$PATH"

Ensuite branche ton appareil et exécute :
  adb devices

EOF
