#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a backup of the build directory but force it to have
#   a comm error, and check that it restarts correctly.
# Then do a virtualfull of the job and a restore
#
TestName="restart-job-virtual-full-test"
JobName=RestartJob
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

setup_shstore # simulate shared storage if enabled

echo "${cwd}/build" >${cwd}/tmp/file-list

change_jobname NightlySave $JobName
start_test

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'NextPool', 'PoolA', 'Pool', 'Default')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'LabelFormat', 'Vol', 'Pool', 'Default')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Accurate', 'yes', 'Job', '$JobName')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'SpoolData', 'no', 'Job', '$JobName')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'AllowIncompleteJobs', 'yes', 'Job', '$JobName')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'MaximumVolumeBytes', '1MB', 'Pool')"

cat <<EOF >> $conf/bacula-dir.conf
Pool {
  Name = PoolA
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365d             # one year
  Label Format = "VolA"
  Maximum Volume Bytes = 30MB
  Storage = File1
}
EOF

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug level=0 trace=0 hangup=-15000 client
@#setdebug level=20 dir
run job=$JobName yes
@sleep 5
setdebug level=0 trace=0 hangup=-15000 client
@sleep 5
setdebug level=0 trace=0 hangup=-15000 client
@sleep 5
setdebug level=0 trace=0 hangup=-15000 client
@sleep 5
setdebug level=0 trace=0 hangup=-15000 client
@sleep 5
setdebug level=0 trace=0 hangup=-15000 client
@sleep 5
setdebug level=0 trace=0 hangup=-15000 client
wait
messages
list jobs
@exec "sh -c 'touch $cwd/build/po/*.po'"
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bacula  

scripts/check_for_zombie_jobs storage=File

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=$tmp/bacula-restores select all done
yes
wait
messages
@$out ${cwd}/tmp/log3.out
run job=$JobName yes jobid=1,2 level=virtualfull
wait
messages
@$out ${cwd}/tmp/log2.out
restore where=$tmp/bacula-restores2 select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
scripts/check_for_zombie_jobs storage=File

if [ "$FORCE_DEDUP" = yes ]; then
    cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@# now do a vacuum
@#
@$out ${cwd}/tmp/log3.out
dedup vacuum
quit
END_OF_DATA

    run_bconsole

    grep VacuumBadRef $working/*trace > /dev/null
    if [ $? = 0 ]; then
        print_debug "ERROR: Found VacuumBadRef records in the trace file"
        estat=1
    fi
fi

stop_bacula

check_two_logs

$rscripts/diff.pl -s $cwd/build -d $tmp/bacula-restores/$cwd/build
if [ $? -ne 0 ]; then
    print_debug "ERROR: Error in the restart+incr restore"
    estat=1
fi


$rscripts/diff.pl -s $cwd/build -d $tmp/bacula-restores2/$cwd/build
if [ $? -ne 0 ]; then
    print_debug "ERROR: Error in the Virtual Full restore"
    estat=1
fi

end_test
