#!/usr/bin/env bash

MODULE_MDNS_AVAHI_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$MODULE_MDNS_AVAHI_DIR/../../.." && pwd)"

# shellcheck source=lib/ui.sh
source "$PROJECT_ROOT/lib/ui.sh"
# shellcheck source=lib/log.sh
source "$PROJECT_ROOT/lib/log.sh"
# shellcheck source=lib/package.sh
source "$PROJECT_ROOT/lib/package.sh"
# shellcheck source=core/runtime.sh
source "$PROJECT_ROOT/core/runtime.sh"
# shellcheck source=modules/network/mdns-avahi/module.sh
source "$MODULE_MDNS_AVAHI_DIR/module.sh"

runtime_init "$PROJECT_ROOT"
log_init

if ! package_is_installed avahi-daemon; then
  printf 'mdns-avahi test SKIPPED: avahi-daemon not installed\n'
  exit 0
fi

if ! test -f "$PROJECT_ROOT/config/mdns-avahi.yaml"; then
  printf 'mdns-avahi test FAILED: missing repository config\n' >&2
  exit 1
fi

if ! systemctl status avahi-daemon >/dev/null 2>&1; then
  printf 'mdns-avahi test SKIPPED: systemd status unavailable in this environment\n'
  exit 0
fi

if module_mdns_avahi_test; then
  printf 'mdns-avahi test OK\n'
else
  printf 'mdns-avahi test FAILED\n' >&2
  exit 1
fi
