#!/bin/sh
#
# Copyright (C) 2000-2022 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="resume-job-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', 'AllowIncompleteJobs', 'yes', 'Job')"
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "RescheduleOnError", "no", "Job")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "RescheduleIncompleteJobs", "no", "Job")'


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 = "Vol"
  Maximum Volume Bytes = 30MB
}
EOF

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug level=0 trace=0 hangup=596 client
@#setdebug level=20 dir
run job=$JobName yes pool=PoolA
wait
resume incomplete jobid=1
wait
messages
@exec "sh -c 'touch $cwd/build/po/*.po'"
setdebug level=0 trace=0 hangup=5 client
run job=$JobName level=Incremental yes pool=PoolA
wait
@$out ${cwd}/tmp/log3.out
resume jobid=2
wait
messages
@$out ${cwd}/tmp/log1.out
setdebug level=0 trace=0 hangup=5 client
run job=$JobName level=Differential yes pool=PoolA
wait
@$out ${cwd}/tmp/log4.out
resume incomplete jobid=3
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 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

grep Incremental $tmp/log3.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: The job level of the incremental job should be Incremental in $tmp/log3.out"
    estat=1
fi

grep Differential $tmp/log4.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: The job level of the differential job should be Differential in $tmp/log4.out"
    estat=1
fi

end_test
