#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory but 
#   split the archive into two volumes then bscan it
#   into the catalog after the backup. It also to a limited
#   extent tests the purge volume and delete volume commands.
#

TestName="bscan-test"
JobName=bscan
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
echo "${cwd}/build" >tmp/file-list

change_jobname NightlySave $JobName
start_test

cat <<END_OF_DATA >tmp/bconcmds
@$out /dev/null
messages
@$out tmp/log1.out
label storage=File1
TestVolume001
label storage=File1
TestVolume002
update Volume=TestVolume001 MaxVolBytes=3000000
run job=$JobName storage=File1
yes
wait
list volumes
list files jobid=1
sql
select * from JobMedia;


messages
@$out /dev/null
@#
@# now purge the Volume
@#
purge volume=TestVolume001
purge volume=TestVolume002
delete volume=TestVolume001
yes
delete volume=TestVolume002
yes
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File1
stop_bacula

echo "volume=TestVolume001" >tmp/bscan.bsr
echo "volume=TestVolume002" >>tmp/bscan.bsr

bscan_libdbi

# If the database has a password pass it to bscan
if test "x${db_password}" = "x"; then
  PASSWD=
else
  PASSWD="-P ${db_password}"
fi

if test "$debug" -eq 1 ; then
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp                   
else
  $bin/bscan -w working $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr -c bin/bacula-sd.conf ${cwd}/tmp >tmp/log3.out 2>&1
fi

cat <<END_OF_DATA >tmp/bconcmds
@$out /dev/null
messages
@$out tmp/log2.out
@# 
@# now do a restore
@#
@#setdebug level=400 storage=File1
restore bootstrap=${cwd}/tmp/kern.bsr where=${cwd}/tmp/bacula-restores select all storage=File1 done
yes
wait
messages
quit
END_OF_DATA

# now run restore
run_bacula
check_for_zombie_jobs storage=File1
stop_bacula

check_two_logs
check_restore_diff
end_test
