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

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
LOCAL_ADB_BIN="$ROOT_DIR/.tools/android-platform-tools/adb"
ADB_BIN="${TDC114_HOST_ADB_BIN:-}"

if [ -z "$ADB_BIN" ]; then
  if [ -x "$LOCAL_ADB_BIN" ]; then
    ADB_BIN="$LOCAL_ADB_BIN"
  elif command -v adb >/dev/null 2>&1; then
    ADB_BIN="$(command -v adb)"
  fi
fi

if [ -z "$ADB_BIN" ] || [ ! -x "$ADB_BIN" ]; then
  cat >&2 <<EOF
Host adb is not available.

Expected one of:
  - $LOCAL_ADB_BIN
  - adb in PATH
  - TDC114_HOST_ADB_BIN=/path/to/adb

Run scripts/install-host-adb-from-docker.sh once to extract adb from the Flutter Docker image.
EOF
  exit 127
fi

exec "$ADB_BIN" "$@"
