#!/bin/bash
set -euo pipefail

ROOT=$(mktemp -d)
trap 'rm -rf "$ROOT"' EXIT

OLD_NAME="back""upper"
OLD="$ROOT/boot/config/plugins/$OLD_NAME"
NEW="$ROOT/boot/config/plugins/urbm"
mkdir -p "$OLD/secrets" "$ROOT/var/log/plugins" "$ROOT/tmp/plugins"
printf '%s\n' '{"settings":{"resticPath":"/usr/local/libexec/'"$OLD_NAME"'/restic","restoreRoot":"/mnt/user/'"$OLD_NAME"'-restores","persistentLogDir":"/mnt/user/system/'"$OLD_NAME"'-logs"}}' > "$OLD/config.json"
printf '%s\n' '[]' > "$OLD/runs.json"
printf '%s\n' 'encrypted' > "$OLD/secrets/secret.json"
touch "$ROOT/boot/config/plugins/$OLD_NAME.plg" "$ROOT/var/log/plugins/$OLD_NAME.plg" "$ROOT/tmp/plugins/$OLD_NAME.plg"

URBM_ROOT_PREFIX="$ROOT" "$(dirname "$0")/../plugin/doinst.sh"

test -f "$NEW/config.json"
test -f "$NEW/runs.json"
test -f "$NEW/secrets/secret.json"
test ! -e "$OLD"
test ! -e "$ROOT/boot/config/plugins/$OLD_NAME.plg"
grep -q '/usr/local/libexec/urbm/restic' "$NEW/config.json"
grep -q '/mnt/user/urbm-restores' "$NEW/config.json"
grep -q '/mnt/user/system/urbm-logs' "$NEW/config.json"
