#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a backup of the build directory and check that it restarts correctly.
#
TestName="restart-sd-test"
JobName=RestartSD
. 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

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log3.out
setbandwidth limit="1000 kb/s" client
@#setdebug level=20 dir
label storage=File volume=TestVolume001 slot=1 drive=0
run job=$JobName yes
@sleep 10
messages
quit
END_OF_DATA

run_bacula  

pid=`cat $working/bacula-sd.*.pid`
if ps $pid | grep bacula-sd > /dev/null; then
    kill -9 $pid
else
    print_debug "ERROR: Unable to find bacula-sd pid, please review the script"
    stop_bacula
    end_test
    exit 1
fi

sleep 5

$scripts/bacula-ctl-sd start

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log3.out
messages
cancel all yes
wait
messages
@$out ${cwd}/tmp/log1.out
setbandwidth limit="10000000 kb/s" client
run job=$JobName yes
wait
messages
@$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
end_test
