#!/bin/bash

#   BAREOS® - Backup Archiving REcovery Open Sourced
#
#   Copyright (C) 2026-2026 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 an incremental backup job
#

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

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

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

# make sure the modification is newer than the previous backup
sleep 1
echo "incremental-1" >"${NDMP_FILESYSTEM}/incremental"

cat <<END_OF_DATA >"${tmp}/bconcmds"
@$out ${log_home}/incr.out
run job=${NDMPCLIENT}-native level=Incremental yes
wait
messages
@$out ${log_home}/list.out
list files jobid=2
END_OF_DATA

run_bconsole "${tmp}/bconcmds"

check_log "${log_home}/incr.out"
expect_grep "Termination:.*Backup OK" "${log_home}/incr.out" \
  "incremental backup did not succeed"
expect_grep "${NDMP_FILESYSTEM}/incremental" "${log_home}/list.out" \
  "incremental file missing in the incremental backup"
expect_not_grep "${NDMP_FILESYSTEM}/testfile2" "${log_home}/list.out" \
  "unchanged file found in the incremental backup"

end_test
