#!/bin/bash
set -euo pipefail
if [ $# -ne 2 ]; then
echo "Usage: $0 DUMP TARGET_DATABASE_URL" >&2
echo "Set GNEXUS_BOOK_RESTORE_YES=1 to confirm; target data will be replaced." >&2
exit 2
fi
DUMP="$1"
TARGET_URL="$2"
if [ "${GNEXUS_BOOK_RESTORE_YES:-}" != "1" ]; then
echo "Refusing to restore without GNEXUS_BOOK_RESTORE_YES=1" >&2
exit 2
fi
if [ ! -f "$DUMP" ]; then
echo "Dump not found: $DUMP" >&2
exit 2
fi
TARGET_PG="${TARGET_URL/postgresql+psycopg/postgresql}"
pg_restore --clean --if-exists --no-owner --no-acl --dbname "$TARGET_PG" "$DUMP"
echo "Restored $DUMP"