#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# This script will test object deletion
#
. scripts/functions

TestName="delete-test"
JobName=NightlySave

scripts/cleanup
scripts/copy-test-confs

start_test

echo $PWD/build/po > tmp/file-list

cat <<EOF > ${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName level=full yes
wait
messages
@exec "touch $cwd/build/po/fr.po"
run job=$JobName level=incremental yes
wait
messages
@exec "touch $cwd/build/po/de.po"
run job=$JobName level=incremental yes
wait
messages
@out $tmp/log2.out
delete jobid=3
sql
SELECT 'FOUND 1' FROM File WHERE JobId=3;
SELECT 'FOUND 2' FROM JobMedia WHERE JobId=3;
SELECT 'FOUND 3' FROM Log WHERE JobId=3;
SELECT 'FOUND 4' FROM BaseFiles WHERE JobId=3;

@out $tmp/log3.out
delete volume=TestVolume001 yes
sql
SELECT 'FOUND 1' FROM File WHERE JobId IN (1,2);
SELECT 'FOUND 2' FROM JobMedia WHERE JobId IN (1,2);
SELECT 'FOUND 3' FROM Log WHERE JobId IN (1,2);
SELECT 'FOUND 4' FROM BaseFiles WHERE JobId IN (1,2);

quit
EOF

run_bacula
stop_bacula

check_two_logs

estat=0

grep -v SELECT $tmp/log2.out | grep FOUND > /dev/null
if [ $? = 0 ]; then
    print_debug "Found orphan records in $tmp/log2.out"
    estat=1
fi

grep -v SELECT $tmp/log3.out | grep FOUND > /dev/null
if [ $? = 0 ]; then
    print_debug "Found orphan records in $tmp/log3.out"
    estat=1
fi

end_test
