#!/bin/sh
#
# Run a simple backup
#   then restore it.
#
TestName="estimate-test"
. scripts/functions
JobName=BackupClient1FileList

# Directory to backup.
# This directory will be created by setup_data().
# If set, $BackupDirectory will be used
# as base directory by check_restore_diff().
# Don't use a "/" at the end of the directory path,
# as it might cause problems
# (filenames given as parameter to restore must match 100%. "//" are not identical with "/").
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

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}
cp ${conf}/bareos-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >${conf}/bareos-dir.conf

start_test

rm -rf ${cwd}/build/src/test ${cwd}/build/po/test
mkdir -p ${BackupDirectory}/build/src/test
mkdir -p ${BackupDirectory}/build/po/
touch ${BackupDirectory}/build/src/test/test1 ${BackupDirectory}/build/src/test/test2

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
label volume=TestVolume001 storage=File pool=Full
run job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out $tmp/log2.out
restore where=$tmp/bareos-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bareos

check_two_logs
check_restore_diff

mv ${BackupDirectory}/build/src/test ${BackupDirectory}/build/po/test

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log3.out
estimate listing job=$JobName level=incremental
messages
quit
END_OF_DATA

run_bconsole

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log4.out
estimate listing job=$JobName level=incremental accurate=yes
messages
quit
END_OF_DATA

run_bconsole

cp ${conf}/bareos-dir.conf $tmp/1
sed 's/JobDefs {/JobDefs { Accurate=yes/' $tmp/1>${conf}/bareos-dir.conf

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log5.out
reload
estimate listing job=$JobName level=incremental
messages
quit
END_OF_DATA

run_bconsole

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log6.out
estimate listing job=$JobName level=incremental accurate=no
messages
quit
END_OF_DATA

run_bconsole

rm -rf ${BackupDirectory}/build/po/test

grep test1 $tmp/log3.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Normal estimate shouldn't detect test1 file in $tmp/log3.out"
    dstat=2
fi

grep test1 $tmp/log4.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Accurate=yes estimate should detect test1 file in $tmp/log4.out"
    dstat=2
fi

grep test1 $tmp/log5.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Accurate estimate should detect test1 file in $tmp/log5.out"
    dstat=2
fi

grep test1 $tmp/log6.out > /dev/null
if [ $? = 0 ]; then
    print_debug "ERROR: Accurate=no estimate shouldn't detect test1 file in $tmp/log6.out"
    dstat=2
fi

check_for_zombie_jobs storage=File

stop_bareos
end_test
