#!/bin/sh
# Unified ping, native output preserved.
#
# macOS still ships separate ping/ping6 binaries, so this picks one the way
# Linux's ping does: IPv6 when the target has an AAAA record *and* this machine
# can actually reach over IPv6, IPv4 otherwise.

v6_usable() {
    # A default route on its own is not enough. A Mac with IPv6 switched off
    # can still show a default v6 route while holding only ::1 and fe80::
    # link-local addresses, and ping6 then dies with "No route to host".
    # Global unicast is 2000::/3, so require an address starting with 2 or 3.
    route -n get -inet6 default >/dev/null 2>&1 || return 1
    ifconfig 2>/dev/null | awk '/inet6 /{ print $2 }' | grep -qE '^[23]'
}

case "$1" in
    -6)
        shift
        exec /sbin/ping6 "$@"
        ;;
    -4)
        shift
        exec /sbin/ping "$@"
        ;;
    *:*)
        # An explicit IPv6 literal: honour it and let ping6 report any problem.
        exec /sbin/ping6 "$@"
        ;;
esac

# First non-option argument is the target.
for a in "$@"; do
    case "$a" in
        -*) ;;
        *)
            t="$a"
            break
            ;;
    esac
done

if [ -n "$t" ] && v6_usable &&
    dscacheutil -q host -a name "$t" 2>/dev/null | grep -q ipv6_address; then
    exec /sbin/ping6 "$@"
fi

exec /sbin/ping "$@"
