#!/bin/bash
set -e
set -o pipefail
set -u
#
# This systemtest tests the Percona plugin functionality
# of the Bareos FD by using the supplied module
#   BareosFdPluginPerconaXtraBackup.py
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-bareos-fd
#shellcheck source=../../environment.in
. ./environment
MYSQL_CLIENT="${MYSQL_CLIENT_BINARY} --defaults-file=mysqldefaults"

JobName=backup-bareos-fd
#shellcheck source=../../scripts/functions
. "${BAREOS_SCRIPTS_DIR}"/functions
skip_if_root

#shellcheck source=../../scripts/mysql.sh
. "${BAREOS_SCRIPTS_DIR}"/mysql.sh
"${BAREOS_SCRIPTS_DIR}"/cleanup
"${BAREOS_SCRIPTS_DIR}"/setup

XTRABACKUP="${XTRABACKUP_BINARY} --defaults-file=mysqldefaults"
xtrabackup_test_db="${db_name}_xtrabackup"

start_test

mysql_init
mysql_server_start

echo "create database ${xtrabackup_test_db}" | $MYSQL_CLIENT
echo "CREATE TABLE test ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, data VARCHAR(100), created TIMESTAMP DEFAULT NOW())  " | $MYSQL_CLIENT "${xtrabackup_test_db}"
echo "insert into test (data) VALUES ('test entry 1') " | $MYSQL_CLIENT "${xtrabackup_test_db}"

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $tmp/log1.out
run job=$JobName yes
wait JobName=$JobName
status dir

run job=$JobName level=Incremental yes
wait JobName=$JobName
status dir


END_OF_DATA

# insert data and run incremental
echo "insert into test (data) VALUES ('test entry 2') " | $MYSQL_CLIENT "${xtrabackup_test_db}"

run_bareos "$@"

cat <<END_OF_DATA2 >"$tmp/bconcmds"
@# run incremental again without any new data
run job=$JobName yes
wait JobName=$JobName
status dir
wait
messages
quit
END_OF_DATA2
run_bareos "$@"

cat <<END_OF_DATA3 >"$tmp/bconcmds"
restore client=bareos-fd fileset=PerconaXtraBackupTest yes restorejob=RestoreFile select all done
@$out $tmp/log2.out
wait
END_OF_DATA3

run_bareos "$@"

mysql_server_stop

# Check if xtrabackup has extracted some files at least
# TODO: verify that xtrabackup --prepare works and eventually do complete datbase restore
ls -lR "$tmp/bareos-restores/_percona/"
if [ -z "$(ls -A "$tmp"/bareos-restores/_percona/)" ]; then
  echo "No restore data found"
  estat=1
fi

# create new empty data dir
rm -Rf mysql/data/*
mkdir -p mysql/data/

TARGETDIR=$(find ./tmp/bareos-restores/_percona/4/ -type d -name '*001')
INCDIR1=$(find ./tmp/bareos-restores/_percona/4/ -type d -name '*002')
INCDIR2=$(find ./tmp/bareos-restores/_percona/4/ -type d -name '*002')

${XTRABACKUP} --prepare --target-dir=${TARGETDIR}
# after prepare a file is left in data dir, this seems to be a bug
rm -Rf mysql/data/*
${XTRABACKUP} --copy-back --target-dir=${TARGETDIR} --incremental-dir=${INCDIR1} --incremental-dir=${INCDIR2}

mysql_server_start

if ! echo "SELECT * from test " | $MYSQL_CLIENT "${xtrabackup_test_db}" | grep "test entry 1"; then
  echo "test entry 1 not found"
  estat=2
fi

if ! echo "SELECT * from test " | $MYSQL_CLIENT "${xtrabackup_test_db}" | grep "test entry 2"; then
  echo "test entry 2 not found"
  estat=3
fi

check_for_zombie_jobs storage=File

mysql_server_stop

check_two_logs

end_test
