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

# Build + sync + assemble debug APK; install on first connected device if adb is available.

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "${ROOT_DIR}"

pnpm --filter=@my-roadtrip/web build
pnpm cap sync android

cd android
./gradlew assembleDebug

APK="app/build/outputs/apk/debug/app-debug.apk"
if command -v adb >/dev/null 2>&1 && adb devices | grep -q "device$"; then
  echo "→ Appareil détecté, installation de ${APK}"
  adb install -r "${APK}"
else
  echo "⚠️  Aucun appareil ADB détecté; APK disponible dans ${PWD}/${APK}"
fi
