#!/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
@sleep 2
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"

full_row="$(
  psql -d "${db_name}" -At -F '|' \
    -c "SELECT JobId, Type, Level, JobTDate, COALESCE(BaseId, 0), COALESCE(ContentId, 0), COALESCE(ExpireTime, 0) FROM Job WHERE JobId=1"
)" && full_row="$(printf '%s' "${full_row}" | tr -d '\r')"
incremental_row="$(
  psql -d "${db_name}" -At -F '|' \
    -c "SELECT JobId, Type, Level, JobTDate, COALESCE(BaseId, 0), COALESCE(ContentId, 0), COALESCE(ExpireTime, 0) FROM Job WHERE JobId=2"
)" && incremental_row="$(printf '%s' "${incremental_row}" | tr -d '\r')"
virtual_row="$(
  psql -d "${db_name}" -At -F '|' \
    -c "SELECT JobId, Type, Level, JobTDate, COALESCE(BaseId, 0), COALESCE(ContentId, 0), COALESCE(ExpireTime, 0) FROM Job WHERE JobId=3"
)" && virtual_row="$(printf '%s' "${virtual_row}" | tr -d '\r')"

IFS='|' read -r full_jobid full_type full_level full_jobtdate full_baseid full_contentid full_expiretime <<<"${full_row}"
IFS='|' read -r incremental_jobid incremental_type incremental_level incremental_jobtdate incremental_baseid incremental_contentid incremental_expiretime <<<"${incremental_row}"
IFS='|' read -r virtual_jobid virtual_type virtual_level virtual_jobtdate virtual_baseid virtual_contentid virtual_expiretime <<<"${virtual_row}"

if [ -z "${full_jobid}" ] || [ -z "${incremental_jobid}" ] || [ -z "${virtual_jobid}" ]; then
  set_error "Expected full/incremental/virtual rows. full='${full_row}' incremental='${incremental_row}' virtual='${virtual_row}'"
  exit 1
fi

expected_full_expiretime="$((full_jobtdate + 3 * 60 * 60))"
expected_incremental_expiretime="$((incremental_jobtdate + 60 * 60))"
expected_virtual_expiretime="$((virtual_jobtdate + 3 * 60 * 60))"

if [ "${full_type}" != "B" ] || [ "${full_level}" != "F" ] \
  || [ "${full_baseid}" != "0" ] || [ "${full_contentid}" != "${full_jobid}" ] \
  || [ "${full_expiretime}" != "${expected_full_expiretime}" ]; then
  set_error "Unexpected full lineage row: ${full_row}"
  exit 1
fi

if [ "${incremental_type}" != "B" ] || [ "${incremental_level}" != "I" ] \
  || [ "${incremental_baseid}" != "${full_contentid}" ] \
  || [ "${incremental_contentid}" != "${incremental_jobid}" ] \
  || [ "${incremental_expiretime}" != "${expected_incremental_expiretime}" ]; then
  set_error "Unexpected incremental lineage row: ${incremental_row}"
  exit 1
fi

if [ "${virtual_type}" != "B" ] || [ "${virtual_level}" != "F" ] \
  || [ "${virtual_baseid}" != "0" ] \
  || [ "${virtual_contentid}" != "${incremental_contentid}" ] \
  || [ "${virtual_expiretime}" != "${expected_virtual_expiretime}" ]; then
  set_error "Unexpected virtual full lineage row: ${virtual_row}"
  exit 1
fi

if [ "${virtual_expiretime}" = "${incremental_expiretime}" ]; then
  set_error "Expected virtual full expiry to use FullRetention instead of IncrementalRetention"
  exit 1
fi

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"

end_test
