#!/bin/bash

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

#
# This systemtest validates that devices, multiplied using the 'Count' directive,
# work and can be used as any other device.
# To validate the devices behaviour, we perform the following actions:
#  - backups
#  - restores
#  - purges
#  - truncates
#

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

JobName=backup-to-pool4
#shellcheck source=../../environment.in
. ./environment

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

RestoreDirectory="${tmp}/implicit"
mkdir -p "${RestoreDirectory}"

start_test

Vol=("Pool4-0001" "Pool4-0002" "Pool4-0003")

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out $tmp/backup-log.out
label volume=${Vol[0]} storage=ImplicitAutochangerFile drive=0 pool=Pool4
label volume=${Vol[1]} storage=ImplicitAutochangerFile drive=1 pool=Pool4
label volume=${Vol[2]} storage=ImplicitAutochangerFile drive=2 pool=Pool4
run job=$JobName level=Full storage=ImplicitAutochangerFile yes
run job=$JobName level=Full storage=ImplicitAutochangerFile yes
run job=$JobName level=Full storage=ImplicitAutochangerFile yes
run job=$JobName level=Full storage=ImplicitAutochangerFile yes
run job=$JobName level=Full storage=ImplicitAutochangerFile yes
@$out $tmp/status-storage.out
status storage=ImplicitAutochangerFile
@$out $tmp/backup-log.out
wait
messages
@#
@# now do a restore
@#
@$out $tmp/restore-log.out
restore client=bareos-fd fileset=SelfTest where=${RestoreDirectory} select all done
yes
wait
messages
@#
@# now purge and truncate
@#
@$out $tmp/purge-truncate.out
purge volume=${Vol[0]} yes
truncate volstatus=Purged drive=0 yes
purge volume=${Vol[1]} yes
truncate volstatus=Purged drive=1 yes
purge volume=${Vol[2]} yes
truncate volstatus=Purged drive=2 yes
messages
quit
END_OF_DATA

###

run_bconsole "$@"
check_for_zombie_jobs storage=File

for i in {0..3}; do
  expect_grep "Device \"ImplicitAutochangerDevice000$i\"" \
    "$tmp/status-storage.out" "Device \"ImplicitAutochangerDevice000$i\" was not created."
done

for i in {1..3}; do
  expect_grep "Using Device \"ImplicitAutochangerDevice000$i\" (storage) to write." \
    "$tmp/backup-log.out" "Device \"ImplicitAutochangerDevice000$i\" was not used."
done

expect_not_grep "Using Device \"ImplicitAutochangerDevice\" (storage) to write." \
  "$tmp/backup-log.out" "Used device \"ImplicitAutochangerDevice\". This device has count > 1 so only copies with serial numbers of this device should be used."

check_log "${tmp}/backup-log.out"
check_log "${tmp}/restore-log.out"
check_restore_diff "${BackupDirectory}" "${RestoreDirectory}"

#make sure all volumes are purged
expect_grep "There are no more Jobs associated with Volume \"${Vol[0]}\". Marking it purged." \
  "${tmp}"/purge-truncate.out \
  "The Purge command failed for Volume \"${Vol[0]}\"."
expect_grep "There are no more Jobs associated with Volume \"${Vol[1]}\". Marking it purged." \
  "${tmp}"/purge-truncate.out \
  "The Purge command failed for Volume \"${Vol[1]}\"."
expect_grep "There are no more Jobs associated with Volume \"${Vol[2]}\". Marking it purged." \
  "${tmp}"/purge-truncate.out \
  "The Purge command failed for Volume \"${Vol[2]}\"."

#make sure all volumes are correctly truncated with the correct drive
expect_grep "3000 OK label. VolFiles=0 VolBytes=... Volume=\"${Vol[0]}\" Device=\"ImplicitAutochangerDevice0000\" (storage)" \
  "${tmp}"/purge-truncate.out \
  "The Truncate command failed for Volume \"${Vol[0]}\"."
expect_grep "The volume '${Vol[0]}' has been truncated." \
  "${tmp}"/purge-truncate.out \
  "The Truncate command failed for Volume \"${Vol[0]}\"."
expect_grep "3000 OK label. VolFiles=0 VolBytes=... Volume=\"${Vol[1]}\" Device=\"ImplicitAutochangerDevice0001\" (storage)" \
  "${tmp}"/purge-truncate.out \
  "The Truncate command failed for Volume \"${Vol[1]}\"."
expect_grep "The volume '${Vol[1]}' has been truncated." \
  "${tmp}"/purge-truncate.out \
  "The Truncate command failed for Volume \"${Vol[1]}\"."
expect_grep "3000 OK label. VolFiles=0 VolBytes=... Volume=\"${Vol[2]}\" Device=\"ImplicitAutochangerDevice0002\" (storage)" \
  "${tmp}"/purge-truncate.out \
  "The Truncate command failed for Volume \"${Vol[2]}\"."
expect_grep "The volume '${Vol[2]}' has been truncated." \
  "${tmp}"/purge-truncate.out \
  "The Truncate command failed for Volume \"${Vol[2]}\"."

# make sure the devices really have been truncated
for vol in "${Vol[@]}"; do
  size=$(wc -c <"storage/${vol}")
  if [ "$size" -ge 300 ]; then
    echo "file storage/${vol}" was not truncated
    estat=3
  fi
done

end_test
