#!/bin/bash
#   BAREOS® - Backup Archiving REcovery Open Sourced
#
#   Copyright (C) 2019-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
# run a backup
# copy it
# purge/delete original volume
# bscan
# restore

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

JobName=backup-bareos-fd
#shellcheck source=../../environment.in
. ./environment

JobName=backup-bareos-fd
#shellcheck source=../../scripts/functions
. "${BAREOS_SCRIPTS_DIR}"/functions
"${BAREOS_SCRIPTS_DIR}"/cleanup
"${BAREOS_SCRIPTS_DIR}"/setup
mkdir -p "${archivedir}-remote"
find "${archivedir}-remote" -mindepth 1 -delete

# Fill ${BackupDirectory} with data.
setup_data

start_test

bin/bareos start

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
label volume=TestVolume001 storage=File pool=Full
run job=$JobName yes
status director
status client
status storage=File
wait
update volume=TestVolume001 volstatus=Used
messages
@#
@# do a copy
@#
@$out $tmp/log1.out
label volume=TestCopyVolume001 storage=RemoteFile pool=FullCopy
run copy yes
status director
status client
status storage=File
wait
messages
purge volume=TestVolume001 yes
delete volume=TestVolume001 yes
purge volume=TestCopyVolume001 yes
delete volume=TestCopyVolume001 yes
quit
END_OF_DATA

run_bconsole "$tmp/bconcmds"

oldconf="${BAREOS_CONFIG_DIR}"
BAREOS_CONFIG_DIR="${BAREOS_CONFIG_DIR}-remote"
run_bscan_db -vv -d100 -s -V TestCopyVolume001 RemoteFileStorage
ret=$?
BAREOS_CONFIG_DIR="${oldconf}"

if [ $ret -ne 0 ]; then
  echo "bscan exit code: $ret"
  bin/bareos stop
  exit $ret
fi

# we set this to the *wrong* number that is currently written to the SOS
original_job_id=4
new_job_id=5

if ! grep -q "Created new JobId=${new_job_id} record for original JobId=${original_job_id}" "$tmp/bscan.out"; then
  echo 'Job numbers of scanned job are not correct'
  bin/bareos stop
  exit 1
fi

num_sos=$(grep -c '^Begin Job Session Record:' "$tmp/bscan.out")
if [ "$num_sos" -ne 1 ]; then
  echo "Found $num_sos start of session records instead of 1"
  bin/bareos stop
  exit 1
fi
num_eos=$(grep -c '^End Job Session Record:' "$tmp/bscan.out")
if [ "$num_eos" -ne 1 ]; then
  echo "Found $num_eos end of session records instead of 1"
  bin/bareos stop
  exit 1
fi

total_jobid_records=$(grep -cE '^JobId +: ' "$tmp/bscan.out")
my_jobid_records=$(grep -cE "^JobId +: ${original_job_id}" "$tmp/bscan.out")

if [ "$my_jobid_records" -ne 2 ]; then
  echo "Got $my_jobid_records mentions of my jobid, expected 2"
  bin/bareos stop
  exit 1
fi
if [ "$my_jobid_records" -ne "$total_jobid_records" ]; then
  echo "Volume contains excess session records:"
  echo "Got $total_jobid_records in total, only $my_jobid_records mention my original jobid"
  bin/bareos stop
  exit 1
fi

cat <<END_OF_DATA >"$tmp/bconcmds2"
@#
@# now do a restore
@#
@$out $tmp/log2.out
wait
restore client=bareos-fd fileset=SelfTest where=$tmp/bareos-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole "$tmp/bconcmds2"

check_for_zombie_jobs storage=File

bin/bareos stop

check_two_logs
check_restore_diff "${BackupDirectory}"
end_test
