#!/usr/bin/env bash

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

# shellcheck source=lib/package.sh
source "$MODULE_MDNS_AVAHI_PROJECT_ROOT/lib/package.sh"
# shellcheck source=modules/network/mdns-avahi/config.sh
source "$MODULE_MDNS_AVAHI_DIR/config.sh"
# shellcheck source=modules/network/mdns-avahi/metadata.conf
source "$MODULE_MDNS_AVAHI_DIR/metadata.conf"

module_mdns_avahi_metadata() {
  printf '%s|%s|%s\n' "$MODULE_ID" "$MODULE_NAME" "$MODULE_DESCRIPTION"
}

module_mdns_avahi_config_path() {
  printf '%s/%s\n' "$MODULE_MDNS_AVAHI_PROJECT_ROOT" "$POSTINSTALL_MDNS_AVAHI_SETTINGS_FILE"
}

module_mdns_avahi_settings() {
  local config_path=""
  local enable_value="$POSTINSTALL_MDNS_AVAHI_ENABLE"
  local publish_workstation="$POSTINSTALL_MDNS_AVAHI_PUBLISH_WORKSTATION"

  config_path="$(module_mdns_avahi_config_path)"
  if [[ -f "$config_path" ]]; then
    while IFS='=' read -r key value; do
      case "$key" in
        enable) enable_value="$value" ;;
        publish_workstation) publish_workstation="$value" ;;
      esac
    done < <(
      awk '
        /^[[:space:]]*enable:/ { print "enable=" $2 }
        /^[[:space:]]*publish_workstation:/ { print "publish_workstation=" $2 }
      ' "$config_path"
    )
  fi

  printf '%s|%s\n' "$enable_value" "$publish_workstation"
}

module_mdns_avahi_require_package() {
  if package_is_installed "avahi-daemon"; then
    ui_info "Paquet avahi-daemon deja installe"
    return 0
  fi

  ui_warn "Paquet avahi-daemon absent, installation en cours"
  package_refresh_indexes
  package_install avahi-daemon avahi-utils libnss-mdns
  log_info "Paquets Avahi installes"
  ui_success "Paquets Avahi installes"
}

module_mdns_avahi_write_config() {
  local enable_value="${1:-$POSTINSTALL_MDNS_AVAHI_ENABLE}"
  local publish_workstation="${2:-$POSTINSTALL_MDNS_AVAHI_PUBLISH_WORKSTATION}"
  local disable_value="yes"

  if [[ "$enable_value" == "yes" ]]; then
    disable_value="no"
  fi

  cp "$POSTINSTALL_MDNS_AVAHI_CONFIG_FILE" "${POSTINSTALL_MDNS_AVAHI_CONFIG_FILE}.bak.postinstall" 2>/dev/null || true

  sed -i \
    -e "s/^#*disable-publishing=.*/disable-publishing=$disable_value/" \
    -e "s/^#*publish-workstation=.*/publish-workstation=$publish_workstation/" \
    "$POSTINSTALL_MDNS_AVAHI_CONFIG_FILE"
}

module_mdns_avahi_check() {
  package_is_installed "avahi-daemon" || return 1
  systemctl is-active --quiet avahi-daemon || return 1
  grep -Eq '^disable-publishing=no$' "$POSTINSTALL_MDNS_AVAHI_CONFIG_FILE" || return 1
}

module_mdns_avahi_install() {
  local settings=""
  local enable_value=""
  local publish_workstation=""

  settings="$(module_mdns_avahi_settings)"
  IFS='|' read -r enable_value publish_workstation <<< "$settings"

  module_mdns_avahi_require_package || return 1
  module_mdns_avahi_write_config "$enable_value" "$publish_workstation"

  systemctl enable --now avahi-daemon
  systemctl restart avahi-daemon

  log_info "Avahi configure, publication=$enable_value workstation=$publish_workstation"
  ui_success "Avahi configure"
}

module_mdns_avahi_test() {
  package_is_installed "avahi-daemon" || return 1
  test -f "$POSTINSTALL_MDNS_AVAHI_CONFIG_FILE" || return 1
  test -f "$(module_mdns_avahi_config_path)" || return 1
  systemctl is-active --quiet avahi-daemon || return 1
  grep -Eq '^disable-publishing=(no|yes)$' "$POSTINSTALL_MDNS_AVAHI_CONFIG_FILE" || return 1
}
