#!/bin/sh
#
# Run a backup on two Volumes
#   then copy it to another device.
#
# This script uses the virtual disk autochanger and migration scripts
#
TestName="copy-uncopied-test"
JobName=MigVolBackup
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
scripts/prepare-disk-changer
#cp ${cwd}/bin/bareos-sd.conf ${cwd}/tmp/1
#sed "s%# Maximum File Size%  Maximum File Size%" ${cwd}/tmp/1 >${cwd}/bin/bareos-sd.conf
change_jobname NightlySave $JobName
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

# 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

touch ${cwd}/tmp/log2.out

$bperl -e 'add_attribute("$conf/bareos-dir.conf", "Allow Duplicate Jobs", "no", "Job", "MigVolBackup")'

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
label storage=File volume=FileVolume001 Pool=Default
label storage=File volume=FileVolume002 Pool=Scratch
label storage=DiskChanger volume=ChangerVolume001 slot=1 Pool=Scratch drive=0
label storage=DiskChanger volume=ChangerVolume002 slot=2 Pool=Scratch drive=0
@#
run job=$JobName level=Full yes
wait
run job=$JobName level=Full yes
wait
update volume=FileVolume001 VolStatus=Used
run job=$JobName level=Full yes
wait
messages
@$out ${cwd}/tmp/log11.out
@############################################################
@# should copy job 1,2,3
@# Expect: 3 copies (1,2,3)
@############################################################
setdebug level=200 dir
run job=copy-uncopied yes
@sleep 5
wait
messages
list jobs
list volumes
list copies
@$out ${cwd}/tmp/log12.out
@############################################################
@# should not copy jobs
@# Expect: 3 copies (1,2,3)
@############################################################
run job=copy-uncopied yes
wait
messages
list copies
@$out ${cwd}/tmp/log13.out
@############################################################
@# should not have a copy of the copy
@# Expect: 2 copies (2,3)
@############################################################
delete jobid=1
run job=copy-uncopied yes
wait
messages
list copies
@$out ${cwd}/tmp/log14.out
@############################################################
@# should copy jobid=2
@# Expect: 2 copies (2,3)
@############################################################
delete jobid=8
run job=copy-uncopied yes
wait
messages
list copies
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=File
stop_bareos

bstat=0
rstat=0
dstat=0
zstat=0

perl -Mscripts::functions -e 'check_multiple_copies(3)' tmp/log11.out
bstat=`expr $bstat + $?`

perl -Mscripts::functions -e 'check_multiple_copies(3)' tmp/log12.out
bstat=`expr $bstat + $?`

perl -Mscripts::functions -e 'check_multiple_copies(2)' tmp/log13.out
bstat=`expr $bstat + $?`

perl -Mscripts::functions -e 'check_multiple_copies(2)' tmp/log14.out
bstat=`expr $bstat + $?`

check_two_logs
end_test
