#!/data/data/com.termux/files/usr/bin/python3
"""mdns-resolve: stdlib-only fallback resolver for <host>.local.

Standalone: does NOT require zeroconf. Prefer `mdns resolve` when installed.

Usage: mdns-resolve <hostname>[.local] [timeout_seconds] [--v6]
Exits 0 with IP on stdout on success; exits 1 on failure.
"""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "lib"))
from mdns_tools.resolve import resolve  # noqa: E402


def main() -> int:
    argv = list(sys.argv[1:])
    if not argv:
        print("Usage: mdns-resolve <hostname>[.local] [timeout] [--v6]", file=sys.stderr)
        return 1
    family = "v6" if "--v6" in argv else "v4"
    argv = [a for a in argv if a != "--v6"]
    host = argv[0]
    timeout = float(argv[1]) if len(argv) > 1 else 2.0
    ip = resolve(host, family=family, timeout=timeout, retries=2)
    if ip:
        print(ip)
        return 0
    return 1


if __name__ == "__main__":
    sys.exit(main())
