#!/bin/bash

#   BAREOS® - Backup Archiving REcovery Open Sourced
#
#   Copyright (C) 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.
#
# This creates a synthetic, systemtest-only restore-selection fixture: one
# backup job per curated plugin restore hint (see
# core/src/dird/restore_plugin_hints.cc), each completing successfully
# through the always-available bpipe plugin and then having its catalog
# FileSetText rewritten (see below) to look, for hint-resolution purposes,
# exactly like the plugin it stands in for. This is only meant to exercise
# restore selection (tree browsing, plugin hints, the Plugin Options
# editor); it does not attempt to actually restore plugin-specific data or
# use any real external plugin infrastructure (vCenter, MSSQL, ...).

set -e
set -o pipefail
set -u

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

JobNames=(
  backup-bareos-fd
  backup-bareos-fd-pluginoptions
  backup-bareos-fd-plugin-vmware
  backup-bareos-fd-plugin-postgresql
  backup-bareos-fd-plugin-proxmox
  backup-bareos-fd-plugin-mariabackup
  backup-bareos-fd-plugin-percona-xtrabackup
  backup-bareos-fd-plugin-ldap
  backup-bareos-fd-plugin-libcloud
  backup-bareos-fd-plugin-hyperv
  backup-bareos-fd-plugin-incus
  backup-bareos-fd-plugin-grpc
  backup-bareos-fd-plugin-barri
  backup-bareos-fd-plugin-qumulo
  backup-bareos-fd-plugin-mariadb-dump
  backup-bareos-fd-plugin-mysql-dump
  backup-bareos-fd-plugin-python
  backup-bareos-fd-plugin-tasks-mariadb
  backup-bareos-fd-plugin-tasks-mysql
  backup-bareos-fd-plugin-tasks-oracle
  backup-bareos-fd-plugin-tasks-pgsql
)

. ./environment
. "${BAREOS_SCRIPTS_DIR}/functions"

start_test

cat <<END_OF_DATA >"${tmp}/bconcmds"
@$out ${NULL_DEV}
messages
@$out ${tmp}/webui-vue-create-backup.out
$(for job in "${JobNames[@]}"; do
  echo "run job=${job} yes"
  echo "wait"
done)
messages
quit
END_OF_DATA

run_bconsole

expect_grep 'Termination:.*Backup OK' \
  "${tmp}/webui-vue-create-backup.out" \
  "Backup did not succeed."
for job in "${JobNames[@]}"; do
  expect_grep "${job}" \
    "${tmp}/webui-vue-create-backup.out" \
    "Plugin backup job ${job} did not run."
done

# The demo FileSets above all actually run through the stock bpipe plugin
# (see webui-vue-common/etc/bareos/bareos-dir.d/fileset/PluginOptionsTest-*),
# since the real integrations (VMware, PostgreSQL, ...) are not available in
# this test environment. To let the webui-vue restore page (and the
# subscription-info catalog queries, which use the same detection) correctly
# recognize which plugin each demo backup is simulating, rewrite the
# catalog's stored FileSet.FileSetText for each demo FileSet so it carries
# the same "module_name=" marker a real python-fd plugin invocation would
# use (see core/src/cats/dml/0080_subscription_with_clause_0), or -- for
# grpc/barri, which are not python-fd modules -- the real plugin name itself
# as the Plugin prefix. This only rewrites descriptive catalog metadata; it
# does not change the live FileSet resource or which plugin actually runs.
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/vmware-demo/vmware-backup.vmdk:reader=', 'Plugin = \"python:module_name=bareos-fd-vmware:file=/vmware-demo/vmware-backup.vmdk:reader=') WHERE FileSet = 'PluginOptionsTest-vmware'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/postgresql-demo/dump.sql:reader=', 'Plugin = \"python:module_name=bareos-fd-postgresql:file=/postgresql-demo/dump.sql:reader=') WHERE FileSet = 'PluginOptionsTest-postgresql'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/proxmox-demo/100.vma:reader=', 'Plugin = \"python:module_name=bareos-fd-proxmox:file=/proxmox-demo/100.vma:reader=') WHERE FileSet = 'PluginOptionsTest-proxmox'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/mariabackup-demo/xbstream:reader=', 'Plugin = \"python:module_name=bareos-fd-mariabackup:file=/mariabackup-demo/xbstream:reader=') WHERE FileSet = 'PluginOptionsTest-mariabackup'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/percona-xtrabackup-demo/stream:reader=', 'Plugin = \"python:module_name=bareos-fd-percona-xtrabackup:file=/percona-xtrabackup-demo/stream:reader=') WHERE FileSet = 'PluginOptionsTest-percona-xtrabackup'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/ldap-demo/directory.ldif:reader=', 'Plugin = \"python:module_name=bareos-fd-ldap:file=/ldap-demo/directory.ldif:reader=') WHERE FileSet = 'PluginOptionsTest-ldap'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/libcloud-demo/objects.tar:reader=', 'Plugin = \"python:module_name=bareos-fd-libcloud:file=/libcloud-demo/objects.tar:reader=') WHERE FileSet = 'PluginOptionsTest-libcloud'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/hyperv-demo/vm.vhdx:reader=', 'Plugin = \"python:module_name=hyper-v:file=/hyperv-demo/vm.vhdx:reader=') WHERE FileSet = 'PluginOptionsTest-hyperv'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/incus-demo/instance.tar:reader=', 'Plugin = \"python:module_name=bareos-fd-incus:file=/incus-demo/instance.tar:reader=') WHERE FileSet = 'PluginOptionsTest-incus'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/grpc-demo/stream.bin:reader=', 'Plugin = \"grpc:file=/grpc-demo/stream.bin:reader=') WHERE FileSet = 'PluginOptionsTest-grpc'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/barri-demo/barri-image.dd:reader=', 'Plugin = \"barri:file=/barri-demo/barri-image.dd:reader=') WHERE FileSet = 'PluginOptionsTest-barri'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/qumulo-demo/snapshot.qfsd:reader=', 'Plugin = \"python:module_name=yuzuy-qumulo:file=/qumulo-demo/snapshot.qfsd:reader=') WHERE FileSet = 'PluginOptionsTest-qumulo'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/mariadb-dump-demo/dump.sql:reader=', 'Plugin = \"python:module_name=bareos-fd-mariadb-dump:file=/mariadb-dump-demo/dump.sql:reader=') WHERE FileSet = 'PluginOptionsTest-mariadb-dump'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/mysql-dump-demo/dump.sql:reader=', 'Plugin = \"python:module_name=bareos_mysql_dump:file=/mysql-dump-demo/dump.sql:reader=') WHERE FileSet = 'PluginOptionsTest-mysql-dump'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/python-demo/generic.dat:reader=', 'Plugin = \"python:file=/python-demo/generic.dat:reader=') WHERE FileSet = 'PluginOptionsTest-python'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/tasks-mariadb-demo/dump.sql:reader=', 'Plugin = \"python:module_name=bareos_tasks.mariadb:file=/tasks-mariadb-demo/dump.sql:reader=') WHERE FileSet = 'PluginOptionsTest-tasks-mariadb'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/tasks-mysql-demo/dump.sql:reader=', 'Plugin = \"python:module_name=bareos_tasks.mysql:file=/tasks-mysql-demo/dump.sql:reader=') WHERE FileSet = 'PluginOptionsTest-tasks-mysql'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/tasks-oracle-demo/dump.dmp:reader=', 'Plugin = \"python:module_name=bareos_tasks.oracle:file=/tasks-oracle-demo/dump.dmp:reader=') WHERE FileSet = 'PluginOptionsTest-tasks-oracle'"
run_query "UPDATE FileSet SET FileSetText = REPLACE(FileSetText, 'Plugin = \"bpipe:file=/tasks-pgsql-demo/dump.sql:reader=', 'Plugin = \"python:module_name=bareos_tasks.pgsql:file=/tasks-pgsql-demo/dump.sql:reader=') WHERE FileSet = 'PluginOptionsTest-tasks-pgsql'"

end_test
