#!/usr/bin/env bash
# Removes the custom syncservice.groovestats.com hosts redirect.
# Usage: sudo ./disable-sync-redirect.sh
set -euo pipefail

ALIAS="${ALIAS:-syncservice.groovestats.com}"
MARKER="${MARKER:-# itgmania-custom-sync}"
HOSTS="${HOSTS:-/etc/hosts}"

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run with sudo." >&2
  exit 1
fi

TMP="$(mktemp)"
trap 'rm -f "$TMP"' EXIT

grep -v -e "$ALIAS" -e "$MARKER" "$HOSTS" >"$TMP" || true
cat "$TMP" >"$HOSTS"

if command -v resolvectl >/dev/null 2>&1; then
  resolvectl flush-caches >/dev/null 2>&1 || true
elif command -v systemd-resolve >/dev/null 2>&1; then
  systemd-resolve --flush-caches >/dev/null 2>&1 || true
fi

echo "Disabled: removed $ALIAS redirect"
