#!/bin/sh
#
# This script will test object deletion
#
. scripts/functions

TestName="delete-test"
JobName=NightlySave

scripts/cleanup
scripts/copy-test-confs

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


start_test

cat <<EOF > ${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName level=full yes
wait
messages
@exec "touch ${BackupDirectory}/fr.po"
run job=$JobName level=incremental yes
wait
messages
@exec "touch ${BackupDirectory}/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_bareos
stop_bareos

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
