# Sourced by the android/ scripts: picks adb and the target device. # ADB adb binary (default: $ANDROID_HOME/platform-tools/adb, else adb on PATH) # ANDROID_SERIAL device to use when several are attached (adb honours it itself) # An adb server that cannot bind :5037 ("Address already in use" with nothing listening, seen on this # Mac after sleep) is retried on :5038 through ANDROID_ADB_SERVER_PORT. SDK="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-$HOME/Library/Android/sdk}}" PKG=org.metin2port.client if [ -z "${ADB:-}" ]; then if [ -x "$SDK/platform-tools/adb" ]; then ADB="$SDK/platform-tools/adb"; else ADB="$(command -v adb || true)"; fi fi [ -n "$ADB" ] || { echo "adb not found (install Android platform-tools or set ADB)" >&2; return 1 2>/dev/null || exit 1; } if ! "$ADB" start-server >/dev/null 2>&1; then export ANDROID_ADB_SERVER_PORT=5038 "$ADB" start-server >/dev/null 2>&1 || { echo "adb server does not start on :5037 or :5038" >&2; return 1 2>/dev/null || exit 1; } echo "adb: :5037 unusable, using :5038" fi export ADB # adb_device: sets DEVICE to the one ready device (or ANDROID_SERIAL) and returns 0; returns 1 with a # message when none is ready (unauthorized / offline devices are named so the user can fix them). adb_device() { local ready others ready=$("$ADB" devices | awk 'NR > 1 && $2 == "device" { print $1 }') others=$("$ADB" devices | awk 'NR > 1 && NF >= 2 && $2 != "device" { print $1 " (" $2 ")" }') if [ -n "${ANDROID_SERIAL:-}" ]; then if printf '%s\n' "$ready" | grep -qxF "$ANDROID_SERIAL"; then DEVICE="$ANDROID_SERIAL"; return 0; fi echo "device $ANDROID_SERIAL is not ready" >&2 return 1 fi case $(printf '%s' "$ready" | grep -c .) in 1) DEVICE="$ready"; export ANDROID_SERIAL="$DEVICE"; return 0 ;; 0) if [ -n "$others" ]; then echo "device attached but not ready: $others (unlock the phone and allow USB debugging)" >&2 else echo "no Android device attached" >&2 fi return 1 ;; *) echo "several devices attached, pick one with ANDROID_SERIAL:" $ready >&2; return 1 ;; esac }