#!/bin/bash

set -euo pipefail

# ---------- Configuration ----------
COMPOSE_FILE="/opt/appdata/.docker-compose/docker-compose.yml"
BCK_DST="/media/backup/"
HC_ID="wB6tZxgIly"
KUMA_URL="https://uptime.noxs.me/api/push/$HC_ID"

# ---------- Functions ----------
notify_kuma() {
  local status=$1
  curl -fsS -X GET "$KUMA_URL?status=$status" > /dev/null || echo "⚠️ Failed to notify Uptime Kuma."
}

# ---------- Main ----------
echo "📦 Dumping Paperless data with document_exporter..."

if docker compose -f "$COMPOSE_FILE" exec -T paperless document_exporter --delete --compare-checksums --use-folder-prefix --use-filename-format "$BCK_DST"; then
  echo "✅ Paperless backup successful."
  notify_kuma "up"
  exit 0
else
  echo "❌ document_exporter command failed!"
  notify_kuma "down"
  exit 1
fi