#!/bin/sh
#
# Run a simple backup 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

change_jobname NightlySave $JobName


# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz


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=10000
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_bareos
check_for_zombie_jobs storage=File1
stop_bareos

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

BSCAN_OUT="write_stdin_to_file ${tmp}/log-bscan.out"
if is_debug; then
   BSCAN_OUT="tee ${tmp}/log-bscan.out"
fi

#$bin/bscan -w ${working} $BSCANLIBDBI -u ${db_user} -n ${db_name} $PASSWD -m -s -v -b tmp/bscan.bsr ${cwd}/tmp >tmp/log3.out 2>&1
$bin/bscan -c ${conf} $BSCANLIBDBI -B ${DBTYPE} -n ${db_name} -u ${db_user} $PASSWD -m -s -v -b $tmp/bscan.bsr ${tmp} 2>&1 | $BSCAN_OUT

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/bareos-restores select all storage=File1 done
yes
wait
messages
quit
END_OF_DATA

# now run restore
run_bareos
check_for_zombie_jobs storage=File1
stop_bareos

check_two_logs
check_restore_diff
end_test
