#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Tests for #7552
#
# Run backup job, change its type to copy, reload configuration and run same job once more.
# Second job should not be started at all.
#
TestName="copy-twin-test"
JobName=copy-twin-test
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
echo "${cwd}/build" >${cwd}/tmp/file-list

sed 's/migrate/copy/g' ${cwd}/bin/bacula-dir.conf > ${cwd}/tmp/1
sed 's/Migrate/Copy/g' ${cwd}/tmp/1 > ${cwd}/bin/bacula-dir.conf
rm -f ${cwd}/build/po/test.po >/dev/null

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Type", "Backup", "Job", "copy-uncopied")'

start_test

# Run job with type=backup
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=FileVolume001 Pool=Default
wait
messages
run job=copy-uncopied yes
wait
messages
quit
END_OF_DATA

run_bacula

# Change job's type to copy, reload configuration and run job once more
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Type", "Copy", "Job", "copy-uncopied")'

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
reload
@$out ${cwd}/tmp/log2.out
run job=copy-uncopied yes
wait
messages
quit
END_OF_DATA

run_bconsole

n_msg=`cat ${cwd}/tmp/log2.out | grep "Unexpected type ('c') of job to copy:" | wc -l`
n_err=`cat ${cwd}/tmp/log2.out | grep 'Copying Error' | wc -l`

if [ ${n_msg} -ne 1 ]; then
   estat=1
   print_debug "Wrong count of error message regarding already existing job: ${n_msg}, expected 1"
fi

if [ ${n_msg} -ne 1 ]; then
   estat=1
   print_debug "Wrong count of 'Copying Error' message: ${n_err}, expected 1"
fi

stop_bacula
end_test
