#!/bin/bash

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

#
# Check whether the file daemon correctly double checks
# the number of files transfered during accurate
#

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

JobName=backup-bareos-fd

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

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

start_test

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
messages
@$out ${runner_tmp}/backup-jobid.out
.api 2
run job=$JobName level=Full yes
.api 1
@$out ${runner_tmp}/create-full.out
wait
status director
status client
status storage=File
messages
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

# check that accurate is enabled
expect_grep "Accurate:[[:space:]]*yes" \
  "${runner_tmp}/create-full.out" \
  "Accurate was not enabled."

# check that more than 0 files are in backup
expect_not_grep "SD Files Written:[[:space:]]*0" \
  "${runner_tmp}/create-full.out" \
  "No files were backed up"

# check that backup was success
expect_grep "Backup OK" \
  "${runner_tmp}/create-full.out" \
  "Backup was not created correctly."

jobid=$(cat "${runner_tmp}/backup-jobid.out" | grep "jobid" | tail -n1 | cut -d':' -f2 | cut -d'"' -f2)

# make the director miscount the number of files
run_query "UPDATE job SET jobfiles=0 WHERE jobid=${jobid}"

# try restoring the backup
cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${runner_tmp}/create-incremental.out
setdebug level=100 trace=1 client
run job=$JobName level=Incremental yes
wait
messages
status director
status client
status storage=File
END_OF_DATA
run_bconsole

# check that job is incremental
expect_grep "Backup Level:[[:space:]]*Incremental" \
  "${runner_tmp}/create-incremental.out" \
  "Accurate was not enabled."

# check that accurate is enabled
expect_grep "Accurate:[[:space:]]*yes" \
  "${runner_tmp}/create-incremental.out" \
  "Accurate was not enabled."

# check that we used the htable backend
expect_grep "accurate_lmdb.cc" \
  "${runner_tmp}/create-incremental.out" \
  "Wrong backend chosen."

# check that miscount was found
expect_grep "only 0 were anticipated." \
  "${runner_tmp}/create-incremental.out" \
  "Warning was not send out."

# check that backup was success
expect_grep "Backup Error" \
  "${runner_tmp}/create-incremental.out" \
  "Backup was not created correctly."

end_test
