#!/bin/sh
#
# Run a backup but force it to have
#   a comm error, and check that it restarts correctly.
#
TestName="restart-accurate-job-test"
JobName=RestartJob
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
change_jobname NightlySave $JobName
$bperl -e "add_attribute('$conf/bareos-dir.conf', 'Accurate', 'yes', 'Job', 'RestartJob')"

# 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/flat-c.tgz
touch $BackupDirectory/testfile

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



start_test

# hangup after backing up 5 files
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
setdebug level=0 trace=0 hangup=5 client
@#setdebug level=20 dir
run job=$JobName yes
wait
messages
sql
select * from JobMedia;

quit
END_OF_DATA

run_bareos

scripts/check_for_zombie_jobs storage=File

# backup all files, but have testfile removed before
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
@$out ${cwd}/tmp/log1.out
setdebug level=0 trace=0 hangup=0 client
@exec "sh -c 'touch $cwd/*.c'"
@exec "rm $BackupDirectory/testfile"
run job=$JobName yes
wait
messages
sql
select * from JobMedia;

@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=$tmp/bareos-restores storage=File select all done
yes
wait
messages
@$out ${cwd}/tmp/log3.out
list jobs
quit
END_OF_DATA

run_bconsole
scripts/check_for_zombie_jobs storage=File
stop_bareos

check_two_logs
check_restore_diff

#nb_F=`awk -F '|' '/RestartJob.+F/ { gsub(/,/, ""); print $7}' $tmp/log3.out`
#nb_I=`awk -F '|' '/RestartJob.+I/ { gsub(/,/, ""); print $7}' $tmp/log3.out`
#nb1=`ls $cwd/*.c | wc -l`
#nb1=`expr $nb1 + 50`

#if [ "$nb_I" -eq "$nb_F" ]; then
#    print_debug "ERROR: Incremental saves too much files (nb Incr == nb Full)"
#    estat=1
#fi

#if [ "$nb_I" -ge "$nb1" ]; then
#    print_debug "ERROR: Incremental saves too much files (more than $nb1)"
#    estat=1
#fi

end_test
