#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory
#   then verify the data on the volume.           
#
TestName="verify-data-test"
JobName=VerifyVol
. scripts/functions

cwd=`pwd`
scripts/cleanup
scripts/copy-test-confs
echo "${cwd}/build/src/dird"      >$tmp/file-list
echo "${cwd}/build/src/filed"     >>$tmp/file-list
#echo "${cwd}/tmp/files" >>$tmp/file-list

#
# Add this back when verify can handle sparse files
#  the same way that backup does
#
#mkdir -p $tmp/files
#dd if=${cwd}/build/configure of=$tmp/files/sparsefile bs=1 count=1 seek=10M > /dev/null

# Create list of files being backed up and verified to compare afterwards
files_listing=${cwd}/tmp/files
find ${cwd}/build/src/dird -print > ${files_listing}
find ${cwd}/build/src/filed -print >> ${files_listing}

change_jobname NightlySave $JobName
start_test

cat <<END_OF_DATA >tmp/bconcmds
@output /dev/null
messages
@$out tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName level=full fileset=FS1 yes
run job=$JobName level=full fileset=FS5 yes
run job=$JobName level=full fileset=FSno5 yes
run job=$JobName level=full fileset=FS5nocheck yes
run job=$JobName level=full fileset=FS2 yes
run job=$JobName level=full fileset=FS3 yes
wait
messages
@# 
@# now do a verify volume
@#
@$out ${cwd}/tmp/log2.out
setdebug level=10 client trace=1
run job=VerifyData jobid=1 yes
run job=VerifyData jobid=2 yes
run job=VerifyData jobid=3 yes
run job=VerifyData jobid=4 yes
run job=VerifyData jobid=5 yes
run job=VerifyData jobid=6 yes
run job=VerifyData jobid=1 accurate=yes yes
run job=VerifyData jobid=2 accurate=yes yes
run job=VerifyData jobid=3 accurate=yes yes
run job=VerifyData jobid=4 accurate=yes yes
run job=VerifyData jobid=5 accurate=yes yes
run job=VerifyData jobid=6 accurate=yes yes
wait
messages
quit
END_OF_DATA

run_bacula

cat <<END_OF_DATA >tmp/bconcmds
@$out $tmp/log4.out
sql
UPDATE File SET Md5='xxx' WHERE JobId=2 and Filename='heartbeat.o';

run job=VerifyData jobid=2 accurate=yes yes
wait
messages
END_OF_DATA

run_bconsole

stop_bacula

while IFS= read -r line <&3; do
   grep "Veryfying: ${line}" tmp/log2.out > /dev/null
   if [ $? -ne 0 ]; then
      estat=1
      print_debug "Did not found ${line} file in verified files log!"
      print_debug "see: ${cwd}/tmp/log2.out"
   fi
done 3< ${files_listing}

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "BlockChecksum", "no", "Device")'

nbok=2
if [ x$FORCE_DEDUP = xyes -o x$FORCE_CLOUD = xyes ]; then
    nbok=6
elif [ x$FORCE_ALIGNED = xyes ]; then
    sed 's/DUALCASE/PUALCASE/g' $tmp/TestVolume001.add > $tmp/1
    mv $tmp/1 $tmp/TestVolume001.add
else
    sed  's/DUALCASE/PUALCASE/g' $tmp/TestVolume001 > $tmp/1
    mv $tmp/1 $tmp/TestVolume001
fi

cat <<END_OF_DATA >tmp/bconcmds
@$out tmp/log3.out
@# 
@# now do a verify volume with errors
@#
@$out ${cwd}/tmp/log3.out
setdebug level=10 client
run job=VerifyData jobid=1 yes
run job=VerifyData jobid=2 yes
run job=VerifyData jobid=3 yes
run job=VerifyData jobid=4 yes
run job=VerifyData jobid=5 yes
run job=VerifyData jobid=6 yes
wait
messages
quit
END_OF_DATA

run_bacula

sleep 1
check_for_zombie_jobs storage=File
stop_bacula

# Turn off verbose file listing, rerun verify jobs and check if files are not listed as expected
sed '/console =*/ s/$/, !verified/g' $conf/bacula-dir.conf > $tmp/1
mv $tmp/1 $conf/bacula-dir.conf

sed '/director =*/ s/$/, !verified/g' $conf/bacula-fd.conf > $tmp/1
mv $tmp/1 $conf/bacula-fd.conf

cat <<END_OF_DATA >tmp/bconcmds
@output /dev/null
messages
@$out tmp/log5.out
run job=VerifyData jobid=1 yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
stop_bacula


while IFS= read -r line <&3; do
   grep "Veryfying: ${line}" tmp/log5.out > /dev/null
   if [ $? -eq 0 ]; then
      estat=1
      print_debug "Found ${line} file in verified files log, no files should be listed now!"
      print_debug "see: ${cwd}/tmp/log5.out"
   fi
done 3< ${files_listing}

nb=`grep "^  Termination: *Backup OK" tmp/log1.out | wc -l`
if [ $nb -ne 6 ]; then
    bstat=1
fi
nb=`grep "^  Termination: *Verify OK" tmp/log2.out | wc -l`
if [ $nb -ne 12 ]; then
    vstat=1
fi
nb=`grep "^  Termination: *Verify OK" tmp/log3.out | wc -l`
if [ $nb -ne $nbok ]; then
    vstat=2
fi
nb=`grep "^  Termination: *Verify Dif" tmp/log4.out | wc -l`
if [ $nb -ne 1 ]; then
    vstat=3
fi
dstat=0
rstat=0
end_test
