#!/bin/sh

# Try to connect via scrcpy first
if ! scrcpy; then
    # If scrcpy fails, try the alternative method
    echo "If scrcpy fails, try the alternative method"
    adb tcpip 5555 >/dev/null 2>&1 || true
    MAC="0c:52:03:1e:89:9a"
    for i in $(seq 1 254); do ping -c1 -W1 192.168.1.$i >/dev/null 2>&1 & done; wait
    line=$(ip neigh | grep -i "$MAC" | head -n1 || true)
    ip=$(printf "%s" "$line" | awk '{print $1}')
    echo "${ip}:5555"
    adb connect "${ip}:5555"
    scrcpy
fi
