#!/bin/bash
set -e
set -o pipefail
set -u

TestName=02-copy

#shellcheck source=../environment.in
. ./environment

#shellcheck source=../scripts/functions
. "${rscripts}"/functions

log="$tmp/$TestName.log"
query_results="$tmp/query_results.out"

check_log() {
  if ! grep -q "$@" "$log"; then
    echo "'$*' not found in $log" >&2
    estat=1
  fi
}

start_test

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out /dev/null
messages
@$out $log
label volume=TestCopyVolume001 storage=File2 pool=FullCopy
run copy yes
wait
messages
@$out $query_results
sqlquery
SELECT count(jobid) FROM job WHERE type='c';
SELECT count(jobid) FROM job WHERE type='C';

quit
END_OF_DATA

run_bconsole "$tmp/bconcmds"

check_for_zombie_jobs storage=File

check_log -F 'Catalog record for Volume "TestCopyVolume001", Slot 0 successfully created.'
check_log 'Termination:.*Copying OK'

if [[ $(grep -c "|     1 |" "$query_results") -ne "2" ]]; then
    echo "The expected amount of copy jobs in the catalog is not met."
    estat=1
fi

end_test
