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

# Not yet working
#
# Run a simple backup of the Bacula build directory then copy it
#   to another device.
##
TestName="prune-copy-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

dircfg=$conf/bacula-dir.conf
$bperl -e "add_attribute('$dircfg', 'AutoPrune', 'No', 'Client')"
$bperl -e "add_attribute('$dircfg', 'Job Retention', '1s', 'Client')"

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
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 level=Full yes
wait
run job=$JobName level=Full yes
wait
list jobs
list volumes
@#setdebug level=100 dir
@# should copy two jobs
@#setdebug level=51 storage=DiskChanger
@sleep 2
run job=copy-job yes
wait
messages
@#purge volume=FileVolume001
list jobs 
list volumes
wait
messages
@#
@# Now do another backup, but level Incremental
@#
@sleep 2
run job=$JobName level=Incremental yes
wait
messages
@#
@# This final job that runs should be Incremental and
@# not upgraded to full.
@sleep 2
@exec "touch $cwd/build/po/fr.po"
run job=$JobName level=Incremental yes
wait
messages
@$out ${cwd}/tmp/log10.out
@sleep 1
setdebug level=1 director
list jobs
sql
SELECT JobId, Name, JobTDate, StartTime, Type, Level, JobFiles, JobStatus
FROM Job ORDER BY StartTime;

prune jobs yes
list jobs
@################################################################ 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
list volumes 
restore where=${cwd}/tmp/bacula-restores select
unmark *
mark *
done
yes
list volumes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
check_restore_diff

# Now we will verify that the pruning code is working as expected.  Each time,
# we run 'list jobs', 'prune', 'list jobs'. check_prune_list ensures that jobids
# in argument are present in the first 'list jobs', the 'prune' command deletes
# them (same number), and checks that the last 'list jobs' doesn't contain them
# anymore. See scripts/functions.pm for details.

# Copy jobs are pruned like normal jobs after the Job Retention period
# the admin 'c'opy job should also be pruned
# F F c c C C I0 I -> F I
$bperl -e "check_prune_list('$tmp/log10.out',1,3,4,5,6,7)"
estat=$(($estat + $?))

end_test
