#!/bin/bash
# Restic restore script for Hördle deployment
# Restores files from the Restic repository created by backup-restic.sh
#
# Usage:
#   scripts/restore-restic.sh [SNAPSHOT] [TARGET_DIR]
#
#   SNAPSHOT   : Optional. Restic snapshot reference (ID, tag, or "latest").
#                Defaults to "latest".
#   TARGET_DIR : Optional. Directory to restore into.
#                Defaults to "./restic-restore-<DATE>-<TIME>".
#
# Examples:
#   scripts/restore-restic.sh
#     → Restore latest snapshot into a new timestamped directory
#
#   scripts/restore-restic.sh latest ./restore-latest
#     → Restore latest snapshot into ./restore-latest
#
#   scripts/restore-restic.sh d3adb33f ./restore-commit
#     → Restore specific snapshot ID into ./restore-commit

set -e

# Optional: Restic-Umgebungsvariablen aus ~/.restic-env laden
if [ -f "$HOME/.restic-env" ]; then
    # shellcheck source=/dev/null
    . "$HOME/.restic-env"
fi

echo "💾 Restoring from Restic backup..."

if ! command -v restic >/dev/null 2>&1; then
    echo "❌ restic nicht im PATH gefunden. Bitte installiere restic oder füge es zum PATH hinzu."
    exit 1
fi

# Erforderliche Umgebungsvariablen prüfen
if [ -z "$RESTIC_PASSWORD" ]; then
    echo "❌ RESTIC_PASSWORD ist nicht gesetzt. Abbruch."
    exit 1
fi

if [ -z "$RESTIC_AUTH_USER" ] || [ -z "$RESTIC_AUTH_PASSWORD" ]; then
    echo "❌ RESTIC_AUTH_USER oder RESTIC_AUTH_PASSWORD ist nicht gesetzt. Abbruch."
    exit 1
fi

# Repository-URL auf Basis des Backup-Skripts
RESTIC_REPO="rest:https://${RESTIC_AUTH_USER}:${RESTIC_AUTH_PASSWORD}@restic.elpatron.me/"

# Passwort für restic exportieren
export RESTIC_PASSWORD

# Snapshot-Referenz und Zielverzeichnis bestimmen
SNAPSHOT_REF="${1:-latest}"
TIMESTAMP="$(date +%Y-%m-%d_%H-%M-%S)"
DEFAULT_TARGET_DIR="./restic-restore-${TIMESTAMP}"
TARGET_DIR="${2:-$DEFAULT_TARGET_DIR}"

echo "   Repository : $RESTIC_REPO"
echo "   Snapshot   : $SNAPSHOT_REF"
echo "   Zielordner : $TARGET_DIR"

# Prüfen, ob Repository existiert
if ! restic -r "$RESTIC_REPO" snapshots >/dev/null 2>&1; then
    echo "❌ Kein gültiges Restic-Repository gefunden (oder keine Snapshots vorhanden)."
    exit 1
fi

# Zielverzeichnis vorbereiten
if [ -e "$TARGET_DIR" ] && [ ! -d "$TARGET_DIR" ]; then
    echo "❌ $TARGET_DIR existiert und ist kein Verzeichnis. Abbruch."
    exit 1
fi

if [ ! -d "$TARGET_DIR" ]; then
    echo "   Erstelle Zielverzeichnis $TARGET_DIR ..."
    mkdir -p "$TARGET_DIR"
fi

echo "   Verfügbare Snapshots (gekürzt):"
restic -r "$RESTIC_REPO" snapshots --compact || true
echo

echo "   Starte Restic-Restore..."

RESTIC_EXIT_CODE=0

# Standard-Restore: gesamtes Repo in Zielverzeichnis
# (Das spiegelt die beim Backup gesicherten Pfade unterhalb von TARGET_DIR.)
restic -r "$RESTIC_REPO" restore "$SNAPSHOT_REF" \
    --target "$TARGET_DIR" || RESTIC_EXIT_CODE=$?

if [ $RESTIC_EXIT_CODE -eq 0 ]; then
    echo "✅ Restic-Restore erfolgreich abgeschlossen."
    echo "   Wiederhergestellte Daten befinden sich in: $TARGET_DIR"
    exit 0
else
    echo "⚠️  Restic-Restore fehlgeschlagen (Exit-Code: $RESTIC_EXIT_CODE)."
    exit $RESTIC_EXIT_CODE
fi


