#!/bin/sh
#
#
TestName="rerun-test"
JobName=BackupClient1
. scripts/functions

scripts/cleanup
scripts/copy-test-confs



/bin/cp -f  ${rconfigs}/${TestName}/bareos-dir.conf.${TestName}  bin/bareos-dir.conf

change_jobname $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

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


start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
messages
label storage=File volume=TestVolume001
run job=$JobName yes
wait
messages

@$out ${cwd}/tmp/log2.out
rerun jobid=1 yes
wait
messages

@$out ${cwd}/tmp/log3.out
rerun hours=1 yes
wait
messages

@$out ${cwd}/tmp/log4.out
rerun days=1 yes
wait
messages

@$out ${cwd}/tmp/log5.out
rerun since_jobid=1 yes
wait
messages

quit
END_OF_DATA
run_bareos

#scripts/check_for_zombie_jobs storage=File client=client

echo "rerun check done"

stop_bareos

## check if first job failed
grep "JobId 1: Fatal error: Client .* RunScript failed." ${tmp}/log1.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "First job failed as intended."
else
   estat=1
fi

## check if rerun of first job worked and failed itself
grep "JobId 2: Fatal error: Client .* RunScript failed." ${tmp}/log2.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "First job failed as intended."
else
   estat=2
fi


## check if rerun of failed jobs in last hour worked, we expect to have selected ids 1 and 2
grep "^1,2" ${tmp}/log3.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "rerun last hour selected jobs 1 and 2."
else
   estat=3
fi

## check if rerun of failed jobs in last day worked, we expect to have selected ids 1 2 3
grep "^1,2,3" ${tmp}/log4.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "rerun last hour selected jobs 1,2,3 OK"
else
   estat=4
fi


## check if rerun of failed jobs in last day worked, we expect to have selected ids 1 2 3
grep "^1,2,3,4,5,6,7,8" ${tmp}/log5.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "rerun last hour selected jobs 1-8 OK"
else
   estat=5
fi

stop_bareos

end_test
