#!/bin/bash

#   BAREOS® - Backup Archiving REcovery Open Sourced
#
#   Copyright (C) 2024-2025 Bareos GmbH & Co. KG
#
#   This program is Free Software; you can redistribute it and/or
#   modify it under the terms of version three of the GNU Affero General Public
#   License as published by the Free Software Foundation and included
#   in the file LICENSE.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#   02110-1301, USA.

set -e
set -o pipefail
set -u
#
# Do a full restore
#

TestName="$(basename "$(pwd)")"
export TestName

#shellcheck source=../../environment.in
. ./environment
runner_name="$(basename "$0")"
runner_tmp="${tmp}/${runner_name}"
mkdir -p "${runner_tmp}"

#shellcheck source=../../scripts/functions
. "${BAREOS_SCRIPTS_DIR}"/functions

start_test

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
messages
@$out ${runner_tmp}/setup.out
status director
status client
status storage=File
wait
messages
@$out ${runner_tmp}/jobs.out
list jobs
END_OF_DATA

run_bconsole

# not sure how to figure this out after the fact
migration_job_id=9

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${runner_tmp}/restore-full.out
restore jobid=${migration_job_id} where=$tmp/remote-migration-restores select all done yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

expect_grep "Start Restore Job" \
  "${runner_tmp}/restore-full.out" \
  "Required restore job was not started."

expect_grep "Restore OK" \
  "${runner_tmp}/restore-full.out" \
  "Required restore job did not finish successfully."

check_restore_diff "${BackupDirectory}" "$tmp/remote-migration-restores"
end_test
