#!/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.
#
#
TestName="restart-base-job-test"
JobName=RestartJobWithBase
. 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

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Base', '$JobName', 'Job', '$JobName')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Accurate', 'yes', 'Job', '$JobName')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'AllowIncompleteJobs', 'yes', 'Job')"

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001 slot=1 drive=0
setdebug level=200 trace=1 storage=File
setdebug level=0 trace=0 hangup=43 client
run job=$JobName level=Base yes
wait
messages
@exec "touch $cwd/build/po/fr.po"
setdebug level=200 trace=1 dir
setdebug level=0 trace=0 hangup=0 client
run job=$JobName yes
wait
messages
sql
select * from JobMedia;

setdebug level=0 trace=0 dir
@$out ${cwd}/tmp/log3.out
list jobs
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 storage=File select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
scripts/check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
check_restore_diff

#nb_B=`awk -F '|' '/RestartJob.+ B.+ B/ { gsub(/,/, "");print $7}' $tmp/log3.out`
#nb_F=`awk -F '|' '/RestartJob.+F/ { gsub(/,/, ""); print $7}' $tmp/log3.out`

#if [ $nb_F -ne $nb_B ]; then
#    print_debug "ERROR: Base and Full should have the same number of files"
#    estat=1
#fi 

#nb_B=`awk -F '|' '/RestartJob.+B.+B/ { gsub(/,/, ""); print $8}' $tmp/log3.out`
#nb_F=`awk -F '|' '/RestartJob.+F/ { gsub(/,/, ""); print $8}' $tmp/log3.out`

#if [ $nb_F -gt 1000000 ]; then
#    print_debug "ERROR: Base job should backup only one file"
#    estat=2
#fi

end_test
