#!/usr/bin/env bash set -euo pipefail PORT=3100 HOST_URL="http://127.0.0.1:${PORT}" RETRIES=30 SLEEP_INTERVAL=1 print() { echo "[dev-android] $*"; } # 1) Ensure dev server is running (try curl) if curl -sSf "$HOST_URL/api/news-public" >/dev/null 2>&1; then print "Dev server already responding at ${HOST_URL}" else print "Dev server not responding, starting 'npm run dev' in background..." # Start dev server in a new session so we can keep this script interactive (cd "$(dirname "$(realpath "$0")")/.." && nohup npm run dev -- --host 0.0.0.0 --port ${PORT} >/tmp/harheimertc-nuxt.log 2>&1 &) || true print "Waiting for server to become ready (logs: /tmp/harheimertc-nuxt.log)" i=0 until curl -sSf "$HOST_URL/api/news-public" >/dev/null 2>&1; do i=$((i+1)) if [ $i -ge $RETRIES ]; then print "Server did not become ready after ${RETRIES} attempts. Check /tmp/harheimertc-nuxt.log" exit 1 fi sleep $SLEEP_INTERVAL done print "Server is ready" fi # 2) Wait for an adb device/emulator print "Waiting for adb device/emulator (ctrl-c to abort)..." while true; do # list devices and skip header devices=$(adb devices | sed '1d' | awk '{print $1 " " $2}' || true) if echo "$devices" | grep -q "device"; then print "Found device(s):" echo "$devices" break fi sleep 1 done # 3) Try adb reverse with retries print "Setting adb reverse tcp:${PORT} -> tcp:${PORT}" count=0 until adb reverse tcp:${PORT} tcp:${PORT}; do count=$((count+1)) print "adb reverse failed (attempt ${count}). Retrying in 1s..." if [ $count -ge 10 ]; then print "adb reverse failed after ${count} attempts. Listing reverses and exiting with failure." adb reverse --list || true exit 1 fi sleep 1 done print "adb reverse configured:" adb reverse --list || true # 4) Verify from device print "Verifying from device via 127.0.0.1:${PORT}" if adb shell curl -sSf "http://127.0.0.1:${PORT}/api/news-public" >/dev/null 2>&1; then print "Success: emulator/device can reach host dev server via 127.0.0.1:${PORT}" exit 0 else print "Verification failed from device. Try 'adb logcat' or check firewall/VM network settings." adb reverse --list || true exit 2 fi