#!/bin/sh
#
# Copyright (C) 2000-2023 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup using bpipe, purge the volume and run
# bscan to see if restore objects were restored.

TestName="bscan-restore-object-test"
JobName=pluginTest
. scripts/functions

scripts/cleanup
scripts/copy-plugin-confs

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

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File1 volume=TestVolume001
@#setdebug level=50 traclient=$CLIENT
run job=$JobName storage=File1 yes
wait
@#setdebug level=50 client=$CLIENT
run job=$JobName storage=File1 yes
wait
status client=$CLIENT
messages
quit
END_OF_DATA

run_bacula

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
list jobs
@$out $tmp/list
llist pluginrestoreconf jobid=1
sql
@$out $tmp/robjs_count
select count(*) from RestoreObject;

quit
END_OF_DATA

run_bconsole

# See how many restore objects were created in the first place
robjs_orig=`cat tmp/robjs_count | grep '|' | cut -d'|' -f2 | tail -1`

nb=`grep $file $tmp/list | wc -l`
if [ $nb -ne 1 ]; then
    print_debug "ERROR: Should find the RestoreObject for $file in $tmp/list"
    bstat=1
fi

nb=`grep Makefile $tmp/list | wc -l`
if [ $nb -ne 1 ]; then
    print_debug "ERROR: Should find the RestoreObject for Makefile in $tmp/list"
    bstat=1
fi


# Purge volume to delete plugin objects from db
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log3.out
purge volume=TestVolume001
@$out ${cwd}/tmp/log4.out
list objects
quit
END_OF_DATA

run_bconsole

# Check if restore objects are no longer in db
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
list jobs
@$out $tmp/list_after_purge
llist pluginrestoreconf jobid=1
quit
END_OF_DATA

run_bconsole

nb=`grep $file $tmp/list_after_purge | wc -l`
if [ $nb -ne 0 ]; then
    print_debug "ERROR: Should not find the RestoreObject after purge for $file in $tmp/list"
    bstat=1
fi

nb=`grep Makefile $tmp/list_after_purge | wc -l`
if [ $nb -ne 0 ]; then
    print_debug "ERROR: Should not find the RestoreObject after purge for Makefile in $tmp/list"
    bstat=1
fi

# Run bscan
echo "volume=TestVolume001" >tmp/bscan.bsr

bscan_libdbi

# If the database has a password pass it to bscan
if test "x${db_password}" = "x"; then
  PASSWD=
else
  PASSWD="-P ${db_password}"
fi

if test "$debug" -eq 1 ; then
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp
else
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp >tmp/log5.out 2>&1
fi

# Check if plugin objects restored successfully
# Get new correct JobId
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out tmp/list_job_after_bscan
list jobs
quit
END_OF_DATA

run_bconsole
newJobId=`cat tmp/list_job_after_bscan | grep 'pluginTest' -m 1 | cut -d '|' -f 2 | xargs`
if [ $newJobId -eq 0 ]; then
    print_debug "ERROR: Could not get JobId after bscan!"
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out $tmp/list_after_bscan
llist pluginrestoreconf jobid=$newJobId
quit
END_OF_DATA

run_bconsole

nb=`grep $file $tmp/list_after_bscan | wc -l`
if [ $nb -ne 1 ]; then
    print_debug "ERROR: Should find the RestoreObject for $file in $tmp/list"
    bstat=1
fi

nb=`grep Makefile $tmp/list_after_bscan | wc -l`
if [ $nb -ne 1 ]; then
    print_debug "ERROR: Should find the RestoreObject for Makefile in $tmp/list"
    bstat=1
fi

run_bconsole

# Check for bug #7098
# Check if running bscan once more won't create any duplicates
if test "$debug" -eq 1 ; then
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp
else
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp >tmp/log6.out 2>&1
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
sql
@$out $tmp/robjs_count_double_bscan
select count(*) from RestoreObject;

quit
END_OF_DATA

run_bconsole

# Check if there are no duplicates
robjs_after_bscan=`cat tmp/robjs_count_double_bscan  | grep '|' | cut -d'|' -f2 | tail -1`
if [ ${robjs_after_bscan} -ne ${robjs_orig} ]; then
    print_debug "ERROR: Running bscan twice produced some duplicates of restore objects. \
                 Original count: ${robjs_orig}, after second bscan: ${robjs_after_bscan}"
    bstat=1
fi

stop_bacula

end_test
