#!/bin/bash

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

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

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

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

start_test

# Incremental Part
# Isilon can have now up to 32, but the Solaris/omnios NDMP still only accepts 9.
# So we limit to 9 on omnios
# https://docs.bareos.org/BareosMainReference/TasksAndConcepts/NdmpBackupsWithBareos.html#ndmp-backup-level
limit=10
if [ "${NDMP_TEST}" = "omnios" ]; then
  limit=9
fi
echo "############# running NDMP ${NDMP_TEST} backup incrementals limit set to ${limit} ##################"

make_remote_data_incremental

for ID in $(seq 1 ${limit}); do
  ndmp_ssh "echo \"${NDMP_HOST_ADDRESS}-${ID}\" >> \"${NDMP_TEST_PATH}/incremental\""

  if [ ${ID} -eq ${limit} ]; then
    echo "##### subdata created for last incremental ####"
    # We want to be sure to test backup and restore of subdir/testfile*
    # So we also add this data in last incremental
    make_remote_subdata_incremental
  fi

  cat <<END_OF_DATA >"${tmp}/bconcmds"
@$out ${log_home}/incr-${ID}.out
run job=ndmp-native fileset=${NDMP_CLIENT}-fileset level=Incremental yes
wait
message
END_OF_DATA

  run_bconsole "${tmp}/bconcmds"

  check_log "${log_home}/incr-${ID}.out"
done
#
# Job cancelling doesn't work that well and lead to all sort of issues
# it might be a good idea to test this
#

#uncomment once fixed
#check_for_zombie_jobs storage=ndmpfile client=ndmp-fd

end_test
