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

#shellcheck source=../environment.in
. ./environment
#shellcheck source=../scripts/functions
. "${rscripts}"/functions

skip_if_root

JobName=backup-bareos-fd
MARIABACKUP="${MARIABACKUP_BINARY} --defaults-file=mysqldefaults"
mariabackup_test_db="${db_name}_mariabackup"

# override MYSQL_*_BINARY variables to be able to use mysql.sh
MYSQL_DAEMON_BINARY="${MARIADB_DAEMON_BINARY}"
MYSQL_CLIENT_BINARY="${MARIADB_CLIENT_BINARY}"

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


start_test

mysql_init
mysql_server_start

# Patch so mysqlclient find its default connection file
cp my.cnf ~/.my.cnf || :

echo "create database ${mariabackup_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 "${mariabackup_test_db}"
echo "insert into test (data) VALUES ('test entry 1') " | $MYSQL_CLIENT "${mariabackup_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 "${mariabackup_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=MariabackupTest yes restorejob=RestoreFile select all done
@$out $tmp/log2.out
wait
END_OF_DATA3

run_bareos "$@"

mysql_server_stop


# Check if mariabackup has extracted some files at least
# TODO: verify that mariabackup --prepare works and eventually do complete datbase restore
ls -lR  "$tmp/bareos-restores/_mariabackup/"
if [ -z "$(ls -A "$tmp"/bareos-restores/_mariabackup/)" ]; 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/_mariabackup/4/ -type d  -name '*001')
INCDIR1=$(find ./tmp/bareos-restores/_mariabackup/4/ -type d  -name '*002')
INCDIR2=$(find ./tmp/bareos-restores/_mariabackup/4/ -type d  -name '*002')

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


mysql_server_start

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

if ! echo "SELECT * from test " | $MYSQL_CLIENT "${mariabackup_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
