#!/bin/bash
#   BAREOS® - Backup Archiving REcovery Open Sourced
#
#   Copyright (C) 2023-2023 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

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

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

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

debug_level=500
File="bbatch.c"

start_test

restore_dir="${tmp}/LV1-restores"

# 1) Create a full backup with LZ4 compression (as per SelfTest fileset config)
# 2) Copy full backup to other pool and decompress (as per FileStorage2 device config)
# 3) Restore copied job
cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${tmp}/backup.out
messages
setdebug level=$debug_level storage=L1
setdebug level=$debug_level storage=L2
label volume=LV1 storage=L1 pool=Full
run job=backup-bareos-fd level=Full yes
wait
update volume=LV1 volstatus=Used
messages
messages
@$out ${tmp}/restore-LV1.out
restore jobid=1 client=bareos-fd where=$restore_dir all done yes
wait
setdebug level=0 storage=L1
setdebug level=0 storage=L2
messages
quit
END_OF_DATA

run_bconsole "$@"

# There should be no difference, but with autoxflate decompression
# the restore reports success ("Restore OK") but seems to fail silently
# and doesn't actually restore anything
check_restore_diff "${BackupDirectory}" "$restore_dir"

if ! is_file_compressed "LD1" "LV1" "${File}"; then
    echo "TestVolume001 is not compressed"
    estat=3
fi

expect_grep "Backup OK" \
	    "$tmp/backup.out" \
	    "Backup did not succeed."

expect_grep "Restore OK" \
	    "$tmp/restore-LV1.out" \
	    "Restore from LV1 did not succeed."

end_test
