#!/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.
#

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

JobName=ai-backup-bareos-fd

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

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

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/extra.out
messages
wait
.api 2
@$out $tmp/virtualfull_jobs.out
run job=VirtualLongtermFull yes
wait
@exec "sh -c 'touch ${tmp}/data/weird-files/file-13'"
run job=$JobName level=Incremental yes
wait
@exec "sh -c 'touch ${tmp}/data/weird-files/file-14'"
run job=$JobName level=Incremental yes
wait
@exec "sh -c 'touch ${tmp}/data/weird-files/file-15'"
run job=$JobName level=Incremental yes
wait
@$out $tmp/virtualfull_messages.out
.api 1
messages
END_OF_DATA
run_bconsole

jobid=$(grep "jobid" "$tmp/virtualfull_jobs.out" | tail -n 4 | head -n 1 | sed 's/["jobid:]//g' | xargs)

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/virtualfull_bvfs.out
.bvfs_lsdirs jobid=${jobid} path=
.bvfs_update
.bvfs_lsdirs jobid=${jobid} path=
.bvfs_versions jobid=${jobid} path=${tmp}/data/weird-files/ client=bareos-fd fname=normalfile
messages
END_OF_DATA
run_bconsole

expect_grep "0	0	A A A A A A A A A A A A A A	.*/" \
  "$tmp/virtualfull_bvfs.out" \
  "bvfs_update did not take into consideration VirtualFull Archive."

expect_grep "x9mlMotNz6zXMPiL2rDLrg	AI-Longterm" \
  "$tmp/virtualfull_bvfs.out" \
  ".bvfs_versions did not find a file backup up by a VirtualFull Archive."

end_test
