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

#
# Run a simple backup of the Bacula build directory then migrate it
#   to another device, then see how prune code handle that
#
# This script uses the virtual disk autochanger
#
TestName="prune-migration-test"
JobName=MigrationJobSave
. scripts/functions


scripts/cleanup
scripts/copy-migration-confs
scripts/prepare-disk-changer
echo "${cwd}/build" >${cwd}/tmp/file-list

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

start_test

#
# Note, we first backup into Pool Default, 
#          then Migrate 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 migrated)
run job=$JobName yes
@sleep 4
wait
@sleep 3
run job=$JobName yes
wait
@sleep 5
list jobs
@# should migrate two jobs
setdebug level=1 storage=File
run job=migrate-job yes
wait
run job=migrate-job yes
wait
messages
list jobs 
wait
@sleep 3
@#
@# Now do another backup, but level Incremental
@#
run job=$JobName level=Incremental yes
wait
messages
@sleep 3
@################################################################
@$out $cwd/tmp/log10.out
setdebug level=1 director
sql
SELECT JobId, Name, Level, JobTDate, StartTime, EndTime, Type FROM Job ORDER BY JobId;

list jobs
prune jobs yes
list jobs
sql
SELECT JobId, Name, Level, JobTDate, StartTime, EndTime, Type FROM Job ORDER BY JobId;

@$out $cwd/tmp/log11.out
sql
SELECT JobId FROM Job LIMIT 1;

@################################################################ 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
list volumes 
restore where=${cwd}/tmp/bacula-restores select storage=DiskChanger
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.

# M M g g F F I0 -> F
# Note, for some strange reason, we end either with JobId=5 or
#  with JobId=8.  Why it changes, I do not know...
# So, we get it from the job list to exclude it and build the check_prune_list
id=`awk '/ [0-9] / { print $2 }' $tmp/log11.out`

if [ "$id" = "" ]; then
    print_debug "ERROR: Unable to determine the id in $tmp/log11.out"
    estat=1
fi

jobid=1
for i in `seq 2 7`
do
    if [ "$i" != "$id" ]; then
        jobid="$jobid,$i"
    fi
done
$bperl -e "check_prune_list('$tmp/log10.out',$jobid)"
estat=$(($estat + $?))

end_test
