#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test if Plugin Objects are created and correctly deleted during volume purging.
#
TestName="plugin-object-test"
JobName=pluginTest
. scripts/functions

scripts/cleanup
scripts/copy-plugin-confs
make -C $src/src/plugins/fd install install-test-plugin
if [ $? -ne 0 ]; then
    print_debug "Failed to build and install test-plugin!"
    exit 1
fi

file=encrypt-bug.jpg
rm -rf ${cwd}/tmp/*
echo "${cwd}/README" >${cwd}/tmp/file-list

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "MaximumFileIndex", "48KB", "Device")'

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File1 volume=TestVolume001
run job=TestPluginTest storage=File1 yes
wait
status client=$CLIENT
messages
@$out ${cwd}/tmp/log2.out
list objects
@$out ${cwd}/tmp/log3.out
llist objects
quit
END_OF_DATA

run_bacula

#TODO add check for fileds in llist output
objects_lines=`cat ${cwd}/tmp/log2.out | grep 'VMWare' | wc -l`
if [ ${objects_lines} -ne 2 ]; then
   estat=1
   print_debug "ERROR: Wrong plugin objects count found in output $tmp/log2.out: ${objects_lines}, expected 2"
fi

objects_lines=`cat ${cwd}/tmp/log2.out | grep 'PostgreSQL' | wc -l`
if [ ${objects_lines} -ne 4 ]; then
   estat=1
   print_debug "ERROR: Wrong plugin objects count found in output $tmp/log2.out: ${objects_lines}, expected 4"
fi

id_vmware_obj=`cat $tmp/log2.out | grep 'VMWare' | head -n 1 | awk  '/VMWare/ {print $2 }'`
if [ "$id_vmware_obj" = "" ]; then
    print_debug "ERROR: Should find any ID for VMWare Object in restore menu in $tmp/log2.out"
    estat=1
fi

id_postgres_obj=`cat $tmp/log2.out | grep 'PostgreSQL' | head -n 1 | awk  '/PostgreSQL/ {print $2 }'`
if [ "$id_postgres_obj" = "" ]; then
   print_debug "ERROR: Should find any ID for PostgreSQL Object in restore menu $tmp/log2.out"
    estat=1
fi

n_OK=`cat ${cwd}/tmp/log3.out | grep -i "status: T" | wc -l`
if [ ${n_OK} -ne 4 ]; then
   estat=1
   print_debug "ERROR: Wrong count of \'T\' status for objects in $tmp/log3.out, expected 4, got: ${n_OK}"
fi

n_UNSET=`cat ${cwd}/tmp/log3.out | grep -i "status: U" | wc -l`
if [ ${n_UNSET} -ne 0 ]; then
   estat=1
   print_debug "ERROR: Wrong count of \'U\' status for objects in $tmp/log3.out, expected 0, got: ${n_UNSET}"
fi


# Get id of each type in restore menu
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
messages
@$out ${cwd}/tmp/log4.out
restore
13
.
quit
END_OF_DATA

run_bconsole

id_vmware_menu=`cat $tmp/log4.out | grep 'VMWare' | head -n 1 | awk  '/VMWare/ {print $1 }'`
if [ "$id_vmware_menu" = "" ]; then
    print_debug "ERROR: Did not find any ID for VMWare Object in restore menu in $tmp/log4.out"
    estat=1
fi

id_postgres_menu=`cat $tmp/log4.out | grep 'PostgreSQL' | head -n 1 | awk  '/PostgreSQL/ {print $1 }'`
if [ "$id_postgres_menu" = "" ]; then
    print_debug "ERROR: Did not find any ID for PostgreSQL Object in restore menu in $tmp/log4.out"
    estat=1
fi

# Strip trailing ':'
id_vmware_menu=${id_vmware_menu%?}
id_postgres_menu=${id_postgres_menu%?}

# Restore 'VMWare' object
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
messages
@$out ${cwd}/tmp/log5.out
restore
13
${id_vmware_menu}
${id_vmware_obj}
yes
wait
messages
.
quit
END_OF_DATA

run_bconsole

cat $tmp/log5.out | grep "Restore OK"
if [ $? -ne 0 ]; then
    print_debug "ERROR: Restore failed in $tmp/log5.out"
    estat=1
fi

# Restore 'PostgreSQL' object
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
messages
@$out ${cwd}/tmp/log6.out
restore
13
${id_postgres_menu}
${id_postgres_obj}
.
yes
wait
messages
.
quit
END_OF_DATA

run_bconsole

postgres_id=`cat $tmp/log6.out | grep "test db"  | tail -n 1 | awk '/test db/ {print $2}'`
if [ "$postgres_id" = "" ]; then
    print_debug "ERROR: Did not find any ID for PostgreSQL Object in restore menu ($tmp/log6.out)"
    estat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
messages
@$out ${cwd}/tmp/log7.out
restore
13
${id_postgres_menu}
${id_postgres_obj}
${postgres_id}
yes
wait
messages
.
quit
END_OF_DATA

run_bconsole

cat $tmp/log7.out | grep "Restore OK"
if [ $? -ne 0 ]; then
    print_debug "ERROR: Restore failed in $tmp/log7.out"
    estat=1
fi

# Restore object with bvfs
cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log8.out
setdebug level=15 tags=bvfs,sql trace=1 dir
.bvfs_cleanup path=b21$$
.bvfs_restore path=b21$$ objectid=$id_vmware_obj,$id_postgres_obj
wait
messages
@$out $tmp/log9.out
restore file=?b21$$ where=$tmp/bacula-restore-bvfs yes
wait
messages
quit
END_OF_DATA

run_bconsole

# Check if restored file exists
if [ ! -f $tmp/bacula-restore-bvfs/@testplugin/test.zero ]; then
   print_debug "Failed to restore object using bvfs, file not found!"
   rstat=1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
messages
@# now do a restore
@$out ${cwd}/tmp/log10.out
restore where=${cwd}/tmp select all storage=File1 done
yes
wait
messages
quit
END_OF_DATA

run_bconsole

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log11.out
purge volume=TestVolume001
@$out ${cwd}/tmp/log12.out
list objects
END_OF_DATA

run_bconsole

objects_lines=`cat ${cwd}/tmp/log10.out | grep 'VMWare' | wc -l`
if [ ${objects_lines} -ne 0 ]; then
   estat=1
   print_debug "ERROR: Wrong plugin objects count found in output $tmp/log10.out: ${objects_lines}, expected 0"
fi

objects_lines=`cat ${cwd}/tmp/log11.out | grep 'PostgreSQL' | wc -l`
if [ ${objects_lines} -ne 0 ]; then
   estat=1
   print_debug "ERROR: Wrong plugin objects count found in output $tmp/log11.out: ${objects_lines}, expected 0"
fi

stop_bacula
end_test
