#!/usr/bin/env bash
# suppress-maintenance-noise.sh — Reduce guest maintenance output noise.
#
# Suppresses on Debian/Ubuntu guests:
#   - APT periodic background updates
#   - MOTD dynamic scripts (package counts, landscape-sysinfo, news)
#   - PAM motd modules (dynamic MOTD printed at login)
#   - "X updates can be applied immediately" login banner
#
# Suppresses on Arch guests:
#   - pkgfile update timer (if present)
#   - quiet-mode marker for game to detect

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DRY_RUN=false
DOMAIN="${1:-}"

if [ -z "$DOMAIN" ]; then
    echo "Usage: bash tools/vm/suppress-maintenance-noise.sh <domain> [--dry-run]"
    exit 1
fi

if [[ "${2:-}" == "--dry-run" ]]; then
    DRY_RUN=true
fi

source "$SCRIPT_DIR/lib/common.sh"
ensure_vm_tooling

tmp_script="$(mktemp)"
cat > "$tmp_script" <<'EOF'
# --- Debian/Ubuntu ---
if command -v apt-get >/dev/null 2>&1; then
  # Disable APT background periodic tasks
  mkdir -p /etc/apt/apt.conf.d
  cat > /etc/apt/apt.conf.d/99sysadmin-chronicles-quiet <<'APT'
APT::Periodic::Enable "0";
APT::Periodic::Update-Package-Lists "0";
APT::Periodic::Unattended-Upgrade "0";
APT::Periodic::Download-Upgradeable-Packages "0";
Acquire::Languages "none";
APT

  # Disable dynamic MOTD scripts that show update counts, ads, news
  if [ -d /etc/update-motd.d ]; then
    chmod -x /etc/update-motd.d/* 2>/dev/null || true
    # Preserve a minimal placeholder so PAM doesn't error
    printf '#!/bin/sh\n' > /etc/update-motd.d/00-sysadmin-chronicles
    chmod +x /etc/update-motd.d/00-sysadmin-chronicles
  fi

  # Remove static /etc/motd content if present
  if [ -f /etc/motd ]; then
    printf '' > /etc/motd
  fi

  # Disable PAM dynamic motd in sshd PAM config (suppresses update counts at login)
  for pam_file in /etc/pam.d/sshd /etc/pam.d/login; do
    if [ -f "$pam_file" ]; then
      sed -i 's/^session\s\+optional\s\+pam_motd\.so/#&/' "$pam_file"
    fi
  done

  # Suppress "X updates can be applied" from landscape-sysinfo / update-notifier
  if [ -f /etc/landscape/client.conf ]; then
    sed -i '/sysinfo/d' /etc/landscape/client.conf 2>/dev/null || true
  fi
  # Disable landscape-sysinfo if installed
  if command -v landscape-sysinfo >/dev/null 2>&1; then
    if [ -f /etc/landscape/client.conf ]; then
      grep -q 'include_sysinfo_plugins' /etc/landscape/client.conf || \
        printf '[sysinfo]\ninclude_sysinfo_plugins =\n' >> /etc/landscape/client.conf
    else
      mkdir -p /etc/landscape
      printf '[sysinfo]\ninclude_sysinfo_plugins =\n' > /etc/landscape/client.conf
    fi
  fi

  # Disable update-notifier login hint (Debian/Ubuntu)
  if [ -d /etc/profile.d ]; then
    for f in /etc/profile.d/update-notifier.sh /etc/profile.d/motd-news.sh; do
      [ -f "$f" ] && chmod -x "$f" 2>/dev/null || true
    done
  fi
fi

# --- Arch Linux ---
if command -v pacman >/dev/null 2>&1; then
  # Disable pkgfile update timer if present (produces periodic output)
  if systemctl list-unit-files pkgfile-update.timer &>/dev/null; then
    systemctl disable --now pkgfile-update.timer 2>/dev/null || true
  fi

  # Quiet-mode marker for game to detect
  mkdir -p /etc/sysadmin-chronicles
  printf 'managed=true\n' > /etc/sysadmin-chronicles/quiet-mode.conf
fi
EOF

info "Suppressing maintenance noise on ${DOMAIN}"
guest_run_sudo_script "$DOMAIN" "$tmp_script"
rm -f "$tmp_script"
ok "${DOMAIN}: maintenance noise suppressed"
