#!/usr/bin/env bash

set -euo pipefail

repo_root="$(cd "$(dirname "$0")/.." && pwd)"
output_dir="${1:-$repo_root/backups}"

if ! git -C "$repo_root" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  echo "Kein Git-Repository gefunden: $repo_root" >&2
  exit 1
fi

mkdir -p "$output_dir"

timestamp="$(date +%Y%m%d-%H%M%S)"
short_sha="$(git -C "$repo_root" rev-parse --short HEAD)"
archive_name="ruhepunkt-${timestamp}-${short_sha}.tar.gz"
archive_path="$output_dir/$archive_name"

git -C "$repo_root" archive \
  --format=tar.gz \
  --output="$archive_path" \
  HEAD

echo "Backup erstellt: $archive_path"