#!/bin/sh
#
# Run a simple backup then copy it
#   to another device.
#
# This script uses the virtual disk autochanger
#
TestName="copy-job-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 two jobs (both will be copied)
run job=$JobName yes
run job=$JobName yes
run job=$JobName yes
wait
list jobs
list volumes
@#setdebug level=100 dir
@# should copy two jobs
@#setdebug level=51 storage=DiskChanger
run job=copy-job yes
wait
messages
@#purge volume=FileVolume001
list jobs
list volumes
wait
messages
@$out ${cwd}/tmp/log3.out
@#
@# Now do another backup, but level Incremental
@#
run job=$JobName level=Incremental yes
wait
messages
@#
@# This final job that runs should be Incremental and
@# not upgraded to full.
list jobs
@#
@# 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_bareos
check_for_zombie_jobs storage=File
stop_bareos

check_two_logs
check_restore_diff

grep 'Backup Level:' tmp/log3.out  | grep Incremental > /dev/null
if [ $? != 0 ]; then
    bstat=2
    print_debug "The incremental job must use copied jobs"
fi

end_test
