#!/usr/bin/env bash

#   BAREOS® - Backup Archiving REcovery Open Sourced
#
#   Copyright (C) 2026-2026 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.

# Once the slow device initialization has finished, the storage daemon must
# work as usual.

set -e
set -o pipefail
set -u

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

# shellcheck source=../../environment.in
. ./environment

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

start_test

cat <<END_OF_DATA >"${tmp}/bconcmds"
@$out ${NULL_DEV}
messages
@$out ${tmp}/log1.out
label barcodes slots=1 drive=0 pool=Full yes
list volumes
run job=backup-bareos-fd level=Full pool=Full yes
status storage=File
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out
wait
messages
restore client=bareos-fd fileset=SelfTest where=${tmp}/bareos-restores select all done yes
wait
messages
quit
END_OF_DATA

run_bconsole

expect_grep "OK label" "${tmp}/log1.out" "volume was not labeled"
expect_grep "Backup OK" "${tmp}/log1.out" "backup was not successful"
expect_grep "Restore OK" "${tmp}/log2.out" "restore was not successful"

check_for_zombie_jobs storage=File
check_two_logs
check_restore_diff "${BackupDirectory}"

end_test
