#!/usr/bin/env bash # Wait until an Android emulator is fully booted (adb is source of truth). # Usage: scripts/wait-emulator.sh [adb-path] set -euo pipefail ADB="${1:-adb}" LOG="${CRKL_EMU_LOG:-/tmp/crkl-emulator.log}" MAX_TRIES="${CRKL_EMU_WAIT_TRIES:-90}" echo "Waiting for emulator (adb)..." "$ADB" start-server >/dev/null 2>&1 || true for ((i = 1; i <= MAX_TRIES; i++)); do state="$("$ADB" devices 2>/dev/null | awk '/emulator-/{print $2; exit}')" if [[ "$state" == "device" ]]; then boot="$("$ADB" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r' || true)" if [[ "$boot" == "1" ]]; then echo "✓ emulator booted" exit 0 fi elif [[ "$state" == "offline" ]]; then "$ADB" kill-server >/dev/null 2>&1 || true "$ADB" start-server >/dev/null 2>&1 || true fi if ((i % 10 == 0)); then echo " still waiting… (${i}/${MAX_TRIES}) adb=${state:-none}" fi sleep 2 done echo "Emulator did not boot in time — see $LOG" tail -50 "$LOG" 2>/dev/null || true exit 1