#!/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 that bareos actually selects the archive
#   to restore instead of just picking the newest one.
#

set -e
set -o pipefail
set -u

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

JobName=backup-bareos-fd

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

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

start_test

file="$(pwd)/tmp/data/weird-files/socket.pl"

original_file="${runner_tmp}/socket.pl"

cp "${file}" "${original_file}"

if [[ -f "$tmp/bareos-restores/$file" ]]; then
  rm "$tmp/bareos-restores/$file"
fi

if on_windows_host; then
  bareos_path=$(cygpath -w -m "${file}")
else
  bareos_path="$file"
fi

echo "i overwrite the file" >"$file"
cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
setdebug level=100 storage=File
@$out ${runner_tmp}/log1.out
status director
status client
status storage=File
wait
messages
@$out ${runner_tmp}/jobs.out
list jobs
@$out ${runner_tmp}/create-newer-backup.out
update jobid=1 jobtype=A
run job=$JobName
yes
wait
messages
quit
END_OF_DATA

run_bconsole "${tmp}/bconcmds"
# find the new job id
backupid=$(grep 'Job queued. JobId=' "${runner_tmp}/create-newer-backup.out" | sed -n -e 's/^.*JobId=//p')
cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
setdebug level=100 storage=File
@$out ${runner_tmp}/log1.out
status director
status client
status storage=File
wait
@$out ${runner_tmp}/restore-archive.out
restore archive where="${runner_tmp}/bareos-restores"
5
mark ${bareos_path}
done
yes
wait
messages
delete jobid=$backupid
update jobid=1 jobtype=B
quit
END_OF_DATA
run_bconsole "${tmp}/bconcmds"

check_for_zombie_jobs storage=File

check_preconditions

check_log "${runner_tmp}/restore-archive.out"

if on_windows_host; then
  # remove_colon_... needs the leading slash
  removed=$(remove_colon_from_windows_path "/${bareos_path}")
  restored_path="${runner_tmp}/bareos-restores${removed}"
else
  echo "not windows!"
  restored_path="${runner_tmp}/bareos-restores${bareos_path}"
fi

# since we changed the file they cannot be the same!
if cmp -s "${restored_path}" "${file}"; then
  echo "we recovered the new file!"
  exit 2
fi

# since we changed the file they cannot be the same!
if ! cmp -s "${restored_path}" "${original_file}"; then
  echo "we did not recover the old file!"
  exit 2
fi

# restore the old file

mv "${original_file}" "${file}"

# since the other tests do not care for the original contents
# we just leave the changed file instead of changing it back.

end_test
