#!/usr/bin/env bash
set -euo pipefail
if [[ $# -ne 1 ]]; then
  echo "Uso: $0 CARTELLA_DESTINAZIONE" >&2
  exit 2
fi
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=ransombackup-common
source "${SCRIPT_DIR}/ransombackup-common"
target="$1"

if [[ -z "${RB_RESTORE_TOKEN:-}" ]]; then
  read -r -s -p 'Token RESTORE temporaneo: ' RB_RESTORE_TOKEN
  printf '\n'
fi
if [[ -z "${RB_RESTORE_TOKEN}" ]]; then
  echo "Token RESTORE vuoto" >&2
  exit 2
fi
export RESTIC_REST_PASSWORD="${RB_RESTORE_TOKEN}"
unset RB_RESTORE_TOKEN
install -d -m 0700 "${target}"
restic restore latest --target "${target}"
