#!/bin/sh
#
# Copyright (C) 2000-2023 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory
#   then restore it. Used to generate BSR files.
#
TestName="bsr-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
echo "s%  Write Bootstrap%# Write Bootstrap%g" >> ${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

$bperl -e "add_attribute('$conf/bacula-sd.conf', 'MaximumFileSize', '1MB', 'Device')"

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
label volume=TestVolume001 storage=File1 pool=File slot=1 drive=0
run job=$JobName yes
wait
messages
@# 
@# now do a restore
@#
@$out $tmp/log2.out
restore where=$tmp/bacula-restores select all done
@exec "sh -c 'cp $working/*.bsr $tmp/1.bsr'"
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File1
stop_bacula

check_two_logs
check_restore_diff

# here we can add checks on the BSR file
awk '/Volume=/ { nbvol++ } /FileIndex=/ { nbfdx++ } END { print "Found Volumes=" nbvol " FileIndexes=" nbfdx }' $tmp/1.bsr

end_test
