#!/bin/bash
# Publish "chess.local" as an mDNS alias for this host's primary IPv4 address.
# Invoked by chess-mdns-alias.service (blind_chess local deploy).
#
# avahi-daemon already advertises the host's own <hostname>.local; this adds
# the friendly "chess.local" name pointing at the same machine. Runs in the
# foreground holding the registration until the service is stopped.
set -euo pipefail

IP="$(hostname -I | awk '{print $1}')"
if [ -z "$IP" ]; then
  echo "chess-mdns-alias: no IPv4 address found" >&2
  exit 1
fi

echo "chess-mdns-alias: publishing chess.local -> $IP"
# -R/--no-reverse: skip the reverse (PTR) record. avahi-daemon already owns the
# PTR for this IP via the host's own <hostname>.local, so publishing chess.local
# for the same address *with* a reverse entry collides ("Local name collision").
# Clients only need the forward A record, which -a still publishes.
exec avahi-publish -a -R chess.local "$IP"
