#!/bin/sh
#
# Run a simple backup 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
sed 's/migrate/copy/g' ${cwd}/bin/bareos-dir.conf > ${cwd}/tmp/1
sed 's/Migrate/Copy/g' ${cwd}/tmp/1 > ${cwd}/bin/bareos-dir.conf
change_jobname NightlySave $JobName

# 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


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
@$out /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_bareos

echo test > ${BackupDirectory}/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 > ${BackupDirectory}/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
restore where=${cwd}/tmp/bareos-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

rm -rf ${cwd}/tmp/bareos-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

rm -rf ${cwd}/tmp/bareos-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

rm -rf ${cwd}/tmp/bareos-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

rm -f ${BackupDirectory}/test1
rm -rf ${cwd}/tmp/bareos-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_bareos
end_test
