#!/bin/bash

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

#
# Check whether multiple file=/dir= are handled
# correctly during restore.
#

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
#shellcheck source=functions
. functions

start_test

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
messages
@$out ${runner_tmp}/setup.out
setdebug level=100 storage=File
status director
status client
status storage=File
wait
messages
@$out ${runner_tmp}/jobs.out
list jobs
@$out ${runner_tmp}/multi-file.out
restore client=bareos-fd fileset=SelfTest where=$tmp/multi-file/1 file=$tmp/data/weird-files/2 file=$tmp/data/weird-files/1 yes
restore client=bareos-fd fileset=SelfTest where=$tmp/multi-file/2 file=$tmp/data/weird-files/1 yes
restore client=bareos-fd fileset=SelfTest where=$tmp/multi-file/2 file=$tmp/data/weird-files/2 yes
wait
messages
@$out ${runner_tmp}/multi-dir.out
restore jobid=1 client=bareos-fd fileset=SelfTest where=$tmp/multi-dir/1 directory=$tmp/data/weird-files/simple-dir-1/ directory=$tmp/data/weird-files/simple-dir-2/ yes
restore jobid=1 client=bareos-fd fileset=SelfTest where=$tmp/multi-dir/2 directory=$tmp/data/weird-files/simple-dir-2/ yes
restore jobid=1 client=bareos-fd fileset=SelfTest where=$tmp/multi-dir/2 directory=$tmp/data/weird-files/simple-dir-1/ yes
wait
messages
@$out ${runner_tmp}/file-and-dir.out
restore jobid=1 client=bareos-fd fileset=SelfTest where=$tmp/files-and-dir/1 directory=$tmp/data/build/src/tests/ file=$tmp/data/weird-files/2 file=$tmp/data/weird-files/1 yes
restore jobid=1 client=bareos-fd fileset=SelfTest where=$tmp/files-and-dir/2 directory=$tmp/data/build/src/tests/ yes
restore jobid=1 client=bareos-fd fileset=SelfTest where=$tmp/files-and-dir/2 file=$tmp/data/weird-files/2 yes
restore jobid=1 client=bareos-fd fileset=SelfTest where=$tmp/files-and-dir/2 file=$tmp/data/weird-files/1 yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

check_preconditions

check_log "${runner_tmp}/multi-file.out"
check_log "${runner_tmp}/multi-dir.out"
check_log "${runner_tmp}/file-and-dir.out"

expect_not_grep "No database record found for:" \
  "${runner_tmp}/multi-file.out" \
  "Something was not restored in test 1"
expect_not_grep "No database record found for:" \
  "${runner_tmp}/multi-dir.out" \
  "Something was not restored in test 2"
expect_not_grep "No database record found for:" \
  "${runner_tmp}/file-and-dir.out" \
  "Something was not restored in test 3"

if ! bareos_diff -d "$tmp/multi-file/1" -s "$tmp/multi-file/2"; then
  echo "Error: multi-file"
  estat=1
fi

if ! bareos_diff -d "$tmp/multi-dir/1" -s "$tmp/multi-dir/2"; then
  echo "Error: multi-dir"
  estat=1
fi

if ! bareos_diff -d "$tmp/files-and-dir/1" -s "$tmp/files-and-dir/2"; then
  echo "Error: files-and-dir"
  estat=1
fi

end_test
