#!/bin/sh
# Sync actuele NAS-configs → apps/<naam>/ in deze repo.
set -e
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
NAS_DOCKER="${NAS_DOCKER:-/volume1/docker}"
NAS_CONFIGS="${NAS_CONFIGS:-$NAS_DOCKER/Configs}"
NAS_HOMELAB="${NAS_HOMELAB:-/volume1/homes/mo/homelab-command}"

echo "Sync → $ROOT"

# --- Docker compose (live NAS-mappen) ---
for svc in gitea postgres-web excalidraw guacamole wazuh ha-voice-control; do
  case "$svc" in
    postgres-web) app=pgadmin ;;
    *) app="$svc" ;;
  esac
  src="$NAS_DOCKER/$svc/docker-compose.yml"
  dst="$ROOT/apps/$app/docker-compose.yml"
  if [ -f "$src" ]; then
    mkdir -p "$(dirname "$dst")"
    cp -f "$src" "$dst"
    echo "  compose: $app"
  fi
done

# Portainer stacks
mkdir -p "$ROOT/apps/portainer/stacks"
[ -f "$NAS_DOCKER/portainer/compose/2/docker-compose.yml" ] && \
  cp -f "$NAS_DOCKER/portainer/compose/2/docker-compose.yml" "$ROOT/apps/portainer/stacks/excalidraw.yml"
[ -f "$NAS_DOCKER/portainer/compose/6/docker-compose.yml" ] && \
  cp -f "$NAS_DOCKER/portainer/compose/6/docker-compose.yml" "$ROOT/apps/portainer/stacks/guacamole.yml"
echo "  portainer stacks"

# UniFi
mkdir -p "$ROOT/apps/unifi/config"
[ -f "$NAS_DOCKER/unifi/data/system.properties" ] && \
  cp -f "$NAS_DOCKER/unifi/data/system.properties" "$ROOT/apps/unifi/config/"

# --- App configs ---
mkdir -p "$ROOT/apps/homarr/config"
cp -f "$NAS_DOCKER/homarr"/*.json "$ROOT/apps/homarr/config/" 2>/dev/null && echo "  homarr" || true

mkdir -p "$ROOT/apps/adguard/config"
if docker ps --format '{{.Names}}' | grep -qx Adguard; then
  docker cp Adguard:/opt/adguardhome/conf/AdGuardHome.yaml "$ROOT/apps/adguard/config/AdGuardHome.yaml"
  echo "  adguard (uit container)"
fi

mkdir -p "$ROOT/apps/home-assistant/config"
for f in configuration.yaml secrets.yaml automations.yaml scripts.yaml scenes.yaml; do
  [ -f "$NAS_CONFIGS/HA/config/$f" ] && cp -f "$NAS_CONFIGS/HA/config/$f" "$ROOT/apps/home-assistant/config/$f"
done
echo "  home-assistant"

# Media *arr
for pair in "Sonarr:sonarr:config.xml" "Radarr:radarr:config.xml" "Prowlarr:prowlarr:config.xml" "Sabnzb:sabnzbd:sabnzbd.ini"; do
  IFS=: read -r nasname app file <<EOF
$pair
EOF
  src="$NAS_CONFIGS/$nasname/$file"
  dst="$ROOT/apps/$app/config/$file"
  if [ -f "$src" ]; then
    mkdir -p "$(dirname "$dst")"
    cp -f "$src" "$dst"
    echo "  $app"
  fi
done

if [ -d "$NAS_CONFIGS/QBitTorrent/qBittorrent" ]; then
  mkdir -p "$ROOT/apps/qbittorrent/config"
  cp -rf "$NAS_CONFIGS/QBitTorrent/qBittorrent" "$ROOT/apps/qbittorrent/config/"
  echo "  qbittorrent"
fi

if [ -d "$NAS_CONFIGS/Prowlarr/Definitions" ]; then
  mkdir -p "$ROOT/apps/prowlarr/config"
  cp -rf "$NAS_CONFIGS/Prowlarr/Definitions" "$ROOT/apps/prowlarr/config/"
  echo "  prowlarr Definitions"
fi

# Glance / uptime-kuma / whoogle (als er ooit configs komen)
for app in glance uptime-kuma whoogle; do
  nasdir="$NAS_CONFIGS/$app"
  [ "$app" = "uptime-kuma" ] && nasdir="$NAS_CONFIGS/uptime-kuma"
  [ "$app" = "whoogle" ] && nasdir="$NAS_CONFIGS/whoogle"
  if [ -d "$nasdir" ] && [ -n "$(ls -A "$nasdir" 2>/dev/null)" ]; then
    mkdir -p "$ROOT/apps/$app/config"
    cp -rf "$nasdir"/* "$ROOT/apps/$app/config/" 2>/dev/null || true
    echo "  $app"
  fi
done

# --- Monitoring (uit homelab-command) ---
if [ -d "$NAS_HOMELAB/monitoring" ]; then
  cp -f "$NAS_HOMELAB/monitoring/prometheus.yml" "$ROOT/apps/monitoring/"
  cp -rf "$NAS_HOMELAB/monitoring/prometheus/targets/"* "$ROOT/apps/monitoring/prometheus/targets/" 2>/dev/null || true
  cp -f "$NAS_HOMELAB/docker-compose.grafana.yml" "$ROOT/apps/monitoring/"
  cp -f "$NAS_HOMELAB/docker-compose.mesh.yml" "$ROOT/apps/monitoring/"
  echo "  monitoring"
fi

echo ""
echo "Klaar. Zie: git status && git diff"
echo "Proxmox: handmatig scp van /etc/pve → apps/proxmox/hosts/<host>/"
