#!/bin/bash

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

#CTEST requires=01-full+incr

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

JobName=ai-backup-bareos-fd

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

#shellcheck source=../../scripts/functions
. "${BAREOS_SCRIPTS_DIR}"/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

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/virtualfull_primarydata.out
.sql query="SELECT JobId,PrimaryDataSource,PrimaryDataBytes FROM Job WHERE JobId=${jobid}"
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."

expect_grep "Virtual job will contain .* bytes primary data" \
  "$tmp/virtualfull_messages.out" \
  "VirtualFull primary-data summary message was not emitted."

expect_grep "computed_virtual" \
  "$tmp/virtualfull_primarydata.out" \
  "VirtualFull Job.PrimaryDataSource was not set to computed_virtual."

expect_grep "computed_virtual.*[1-9][0-9]*" \
  "$tmp/virtualfull_primarydata.out" \
  "VirtualFull Job.PrimaryDataBytes was not persisted as a positive value."

expected_primary_bytes=$(find "${BackupDirectory}" -type f -ls 2>/dev/null | awk '{sum += $7} END {print sum + 0}')
actual_primary_bytes=$(awk '/computed_virtual/ {for (i = NF; i >= 1; i--) {token = $i; gsub(/[^0-9]/, "", token); if (token != "") {print token; break}}}' "$tmp/virtualfull_primarydata.out" | tail -n 1)

if [ -z "${actual_primary_bytes}" ] || [ "${actual_primary_bytes}" != "${expected_primary_bytes}" ]; then
  set_error "VirtualFull Job.PrimaryDataBytes mismatch: expected ${expected_primary_bytes}, got ${actual_primary_bytes:-<empty>}"
fi

end_test
