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

#
# Create a full and and an incremental, and consolidate them
#  via a virtualfull.  Afterwards check that the virtualfull
#  worked via a restore.
#

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

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

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

start_test

cat <<END_OF_DATA >"${tmp}/bconcmds"
@$out ${NULL_DEV}
messages
@$out ${test_home}/label.out
setdebug level=100 storage=File
label volume=TestVolume001 storage=File pool=Full
label volume=TestVolume002 storage=File pool=Incremental
label volume=TestVolume003 storage=File pool=VirtualFull
@$out ${test_home}/full.out
run job=$JobName level=Full yes
wait
messages
END_OF_DATA
run_bconsole "${tmp}/bconcmds"

check_log "${test_home}/full.out"

touch "${tmp}"/data/*.c
cat <<END_OF_DATA >"${tmp}/bconcmds"
@$out ${test_home}/incr.out
list jobs
run job=$JobName level=Incremental yes
wait
messages
END_OF_DATA
run_bconsole "${tmp}/bconcmds"

check_log "${test_home}/incr.out"

cat <<END_OF_DATA >"${tmp}/bconcmds"
@$out ${test_home}/virtual.out
list jobs
run job=$JobName level=VirtualFull yes
wait
messages
@$out ${test_home}/status.out
list jobs
status director
status client
status storage=File
END_OF_DATA
run_bconsole "${tmp}/bconcmds"

check_log "${test_home}/virtual.out"

virtual_jobid=$(grep -oE 'JobId=[0-9]+' "${test_home}/virtual.out" | tail -n 1 | cut -d= -f2)

cat <<END_OF_DATA >"${tmp}/bconcmds"
@$out ${test_home}/primary-data.out
.sql query="SELECT JobId,PrimaryDataSource,PrimaryDataBytes FROM Job WHERE JobId=${virtual_jobid}"
END_OF_DATA
run_bconsole "${tmp}/bconcmds"

cat <<END_OF_DATA >"${tmp}/bconcmds"
@# make sure we really restore the virtualfull
@$out ${test_home}/restore.out
purge volume=TestVolume001 yes
purge volume=TestVolume002 yes

restore client=bareos-fd fileset=SelfTest where=${test_home}/bareos-restores select all done
yes
wait
messages
END_OF_DATA
run_bconsole "${tmp}/bconcmds"

check_log "${test_home}/restore.out"

check_for_zombie_jobs storage=File

check_restore_diff "${BackupDirectory}" "${test_home}/bareos-restores"

if on_windows_host; then
  expected_file_count=".."
else
  expected_file_count=87
fi

expect_grep "Consolidating JobIds 1,2 containing ${expected_file_count} files" \
  "${test_home}/virtual.out" \
  "Consolidation message was not emitted"

expect_grep "Virtual job will contain .* bytes primary data" \
  "${test_home}/virtual.out" \
  "VirtualFull primary-data summary message was not emitted."

expect_grep "computed_virtual" \
  "${test_home}/primary-data.out" \
  "VirtualFull Job.PrimaryDataSource was not set to computed_virtual."

expect_grep "computed_virtual.*[1-9][0-9]*" \
  "${test_home}/primary-data.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}}}' "${test_home}/primary-data.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
