#!/usr/bin/env bash
# =====================================================================
#  uninstall.sh — usuwa stronę ślubną W&R z serwera
#  Uruchom:  sudo bash uninstall.sh
#  Pyta, czy skasować też pliki + bazę (zdjęcia/wyniki/ankiety).
# =====================================================================
set -uo pipefail

DOMAIN="weronikairafal.pl"
APP_DIR="/opt/weronikairafal"

echo ">> Zatrzymuję i wyłączam usługi..."
systemctl disable --now weronikairafal-forwarder 2>/dev/null || true
systemctl disable --now weronikairafal 2>/dev/null || true
rm -f /etc/systemd/system/weronikairafal.service \
      /etc/systemd/system/weronikairafal-forwarder.service
systemctl daemon-reload

echo ">> Usuwam konfigurację nginx..."
rm -f /etc/nginx/conf.d/weronikairafal.conf
if nginx -t 2>/dev/null; then systemctl reload nginx 2>/dev/null || true; fi

echo ">> Usuwam certyfikat Let's Encrypt (jeśli istnieje)..."
if command -v certbot >/dev/null 2>&1; then
  certbot delete --cert-name "$DOMAIN" --non-interactive 2>/dev/null || true
fi

echo
read -r -p ">> Usunąć też pliki aplikacji i BAZĘ ($APP_DIR — zdjęcia, wyniki, ankiety)? [t/N] " ans
if [ "$(printf '%s' "${ans:-}" | tr '[:upper:]' '[:lower:]')" = "t" ]; then
  rm -rf "$APP_DIR"
  echo "   Usunięto $APP_DIR (razem z bazą i miniaturami)."
else
  echo "   Zostawiam $APP_DIR nietknięte (pliki i baza zostają)."
fi

echo
echo "======================================================"
echo " Odinstalowano usługi, konfigurację nginx i certyfikat."
echo " Pakiety systemowe (nginx, certbot, python3-venv) NIE zostały"
echo " usunięte — gdybyś chciał, usuń ręcznie:"
echo "   sudo apt-get remove --purge nginx certbot python3-certbot-nginx"
echo "======================================================"
