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

#
# Run a simple backup of the Bacula build directory then copy it
#   to another device. We test that when the original job is purged
#   the next copy is "upgraded" to Type=B
#
# This script uses the virtual disk autochanger
#
TestName="copy-upgrade-test"
JobName=CopyJobSave
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
scripts/prepare-disk-changer
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


change_jobname NightlySave $JobName
start_test

#
# Note, we first backup into Pool Default, 
#          then Copy into Pool Full. 
#              Pool Default uses Storage=File
#              Pool Full    uses Storage=DiskChanger

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
@# setdebug level=100 storage=File
label storage=File volume=FileVolume001 Pool=Default
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Full drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Full drive=0
run job=$JobName yes
wait
messages
END_OF_DATA

run_bacula

echo test > ${cwd}/build/po/test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=$JobName level=incremental yes
wait
messages
END_OF_DATA

run_bconsole

echo test1 > ${cwd}/build/po/test1

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=$JobName level=incremental yes
wait
messages
@# we should be able to purge jobid 1 3 times
update volume=FileVolume001 VolStatus=Used
@# 4,5
run job=copy-job jobid=1 yes
wait
@# 6,7
run job=copy-job jobid=2 yes
wait
@# 8,9
run job=copy-job jobid=3 yes
wait
@# 10,11
run job=copy-job jobid=1 yes
wait
@# 12,13
run job=copy-job jobid=2 yes
wait
@# 14,15
run job=copy-job jobid=3 yes
wait
@# 16,17
run job=copy-job jobid=1 yes
wait
@# 18,19
run job=copy-job jobid=1 yes
wait
messages
list jobs
list volumes
END_OF_DATA

run_bconsole

cat<<END_OF_DATA > ${cwd}/tmp/bconcmds.resto
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
list volumes 
@# Note, here we are restoring from the original backup,
@#  so if you specify storage=DiskChanger the restore will fail
restore copies where=${cwd}/tmp/bacula-restores select
unmark *
mark *
done
yes
list volumes
wait
messages
quit
END_OF_DATA

run_bconsole ${cwd}/tmp/bconcmds.resto
check_two_logs
check_restore_diff

cat <<EOF >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out
@# the last restore
@# should restore with 1,2,3
delete jobid=1
@# should restore with 5,2,3
EOF
run_bconsole

cat ${cwd}/tmp/log2.out | grep "Following Copy jobs have been upgraded to Backup jobs: 5"
if [ $? -ne 0 ]; then
   estat=1
   print_debug "Log about upgrading copy job to backup hasn't been found!"
fi

rm -rf ${cwd}/tmp/bacula-restores
run_bconsole ${cwd}/tmp/bconcmds.resto
check_two_logs
check_restore_diff

# remove first copy of job=1
cat <<EOF >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out
delete jobid=5
@# should restore with 11,2,3
EOF
run_bconsole

cat ${cwd}/tmp/log2.out | grep "Following Copy jobs have been upgraded to Backup jobs: 11"
if [ $? -ne 0 ]; then
   estat=1
   print_debug "Log about upgrading copy job to backup hasn't been found!"
fi

rm -rf ${cwd}/tmp/bacula-restores
run_bconsole ${cwd}/tmp/bconcmds.resto
check_two_logs
check_restore_diff

# remove 2 copies at the same time
cat <<EOF >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out
delete jobid=11,17
@# should restore with 19,2,3
EOF
run_bconsole

cat ${cwd}/tmp/log2.out | grep "Following Copy jobs have been upgraded to Backup jobs: 17"
if [ $? -ne 0 ]; then
   estat=1
   print_debug "Log about upgrading copy job to backup hasn't been found!"
fi

cat ${cwd}/tmp/log2.out | grep "Following Copy jobs have been upgraded to Backup jobs: 19"
if [ $? -ne 0 ]; then
   estat=1
   print_debug "Log about upgrading copy job to backup hasn't been found!"
fi

rm -rf ${cwd}/tmp/bacula-restores
run_bconsole ${cwd}/tmp/bconcmds.resto
check_two_logs
check_restore_diff

# remove job and copies of jobid=3
cat <<EOF >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out
delete jobid=3,9,15
@# should restore with 19,2
EOF
run_bconsole

cat ${cwd}/tmp/log2.out | grep "Following Copy jobs have been upgraded to Backup jobs: 9"
if [ $? -ne 0 ]; then
   estat=1
   print_debug "Log about upgrading copy job to backup hasn't been found!"
fi

cat ${cwd}/tmp/log2.out | grep "Following Copy jobs have been upgraded to Backup jobs: 15"
if [ $? -ne 0 ]; then
   estat=1
   print_debug "Log about upgrading copy job to backup hasn't been found!"
fi

rm -f ${cwd}/build/po/test1
rm -rf ${cwd}/tmp/bacula-restores
run_bconsole ${cwd}/tmp/bconcmds.resto
check_two_logs
check_restore_diff

awk '/You have selected the following JobIds:/ { print $7 }' \
      ${cwd}/tmp/log2.out > ${cwd}/tmp/real
awk '/should restore with/ { print $5 }' \
      ${cwd}/tmp/log2.out > ${cwd}/tmp/should

diff -u ${cwd}/tmp/real ${cwd}/tmp/should
if [ $? -ne 0 ]; then
    print_debug "Bad job selection!!!"
    rstat=2
fi

check_for_zombie_jobs storage=File
stop_bacula
end_test
