#!/bin/bash

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

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

JobName=backup-bareos-fd

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

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

setup_data

# FreeBSD will not work nicely with FIFO files Xattr
if [ $(uname) = "FreeBSD" ]; then
  find "${BackupDirectory}" -type p -delete
fi

start_test

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out /dev/null
messages
@$out $tmp/backup.log
setdebug level=100 storage=dplcompat
label volume=TestVolume001 storage=dplcompat pool=Full
run job=$JobName yes
status director
status client
status storage=dplcompat
wait
messages
quit
END_OF_DATA

run_bconsole "$@"
check_for_zombie_jobs storage=dplcompat

check_log "$tmp/backup.log"

# In case we want to tackle the close_wait issue
#bareos_sd_pid=$(pidof bareos_sd-${TestName})
#if [ $(lsof -p ${bareos_sd_pid} | grep -c "CLOSE_WAIT") -ne 0 ]
#then
#   echo "Error: Connection CLOSE_WAIT found"
#   estat=1
#fi

end_test
