#!/bin/sh
. scripts/functions

# Define the Name of the test as "TestName".
# Should be the same as the filename, therefore we use the filename as default.
TestName="`basename $0`"

# set other test specific variables
Client=bareos-fd
JobName=backup-bareos-fd

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

# Remove old configuration, working and tmp files. Setup the database.
cleanup

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

# Config files will be copied to required location.
# Base configuration files come from the
# configs/BASE/ directory, see
# configs/BASE/README.txt for an overview of the provides resources.
# Store your additonal resources to
# configs/$TestName.
# It is also possible to overwrite resources from configs/BASE.
# If you define a full config file (instead of config files for individual resources),
# only these will be taken.
copy_configs

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list

# start the test
start_test

run_bareos
# create a new file and run multiple backups
for file in 1 2 3 4 5 6 7 ; do

echo file$file > ${BackupDirectory}/file$file

cat <<END_OF_DATA >${tmp}/bconcmds
messages
@$out ${tmp}/log1.out w
run job=$JobName yes
wait
messages
END_OF_DATA
run_bconsole

done

#stop_bareos
#check_for_zombie_jobs storage=File1 client=${Client}

cat <<END_OF_DATA >${tmp}/bconcmds
@#
@# try to consolidate
@#
@$out ${tmp}/log1.out w
list jobs
run job=Consolidate yes
wait
list jobs
messages
END_OF_DATA

run_bconsole

cat <<END_OF_DATA >${tmp}/bconcmds
@#
@# now do a restore
@#
@$out ${tmp}/log2.out w
restore client=${Client} where=${tmp}/bareos-restores select
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bconsole

# stop bareos
stop_bareos

# check tmp/log1.out and tmp/log2.out for errors
check_two_logs

# check for differences between original files and restored files
check_restore_diff ${BackupDirectory}

# do some manual testing
if [ ! -d ${BackupDirectory} ]; then
    set_error "Directory ${BackupDirectory} does not exists any more."
fi


# check if consolidation worked as expected
grep "purged JobIds 1,2,3,4,5 as they were consolidated into Job 9" tmp/log1.out || set_error "consolidation did not consolidate jobs that were expected"

# check if the expected jobids were chosen for restore
grep "You have selected the following JobIds: 9,6,7" tmp/log2.out || set_error "other jobs than expected were chosen for restore"

# check if all files were restored
for file in 1 2 3 4 5 6 7 ; do
   grep $file ${tmp}/bareos-restores/${BackupDirectory}/file$file || set_error "file $file was not recovered"
done



# Now purge volumes from the database and check if we can bscan and recover everything

# remove restored data as we want to restore it again after bscan run
rm -Rvf ${cwd}/tmp/bareos-restores

# purge and remove volumes
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log3.out

purge volume=AI-Full-0001
purge volume=AI-Incremental-0002
purge volume=AI-Full-0003

delete volume=AI-Full-0001 yes
delete volume=AI-Incremental-0002 yes
delete volume=AI-Full-0003 yes

messages
list jobs
quit
END_OF_DATA

run_bareos
stop_bareos

# prepare BSR file

echo "volume=AI-Full-0001" >tmp/bscan.bsr
echo "volume=AI-Incremental-0002" >>tmp/bscan.bsr
echo "volume=AI-Full-0003" >>tmp/bscan.bsr
# 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 -c ${conf} $BSCANLIBDBI -B ${DBTYPE} -n ${db_name} -u ${db_user} $PASSWD -m -s -v -b $tmp/bscan.bsr File1 2>&1 | $BSCAN_OUT


# do a restore of the bscan-ed content
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log4.out
@#setdebug level=100 storage=File
@#
@# now do a restore of the consolidated Full
@#
restore where=${cwd}/tmp/bareos-restores select storage=File1
unmark *
mark *
done
yes
wait
list volumes
list jobs
messages
quit
END_OF_DATA



run_bareos
check_for_zombie_jobs storage=File1
stop_bareos


# check if all files were restored
for file in 1 2 3 4 5 6 7 ; do
   grep $file ${tmp}/bareos-restores/${BackupDirectory}/file$file || set_error "file $file was not recovered"
done


# end tests and check for error codes
end_test
