#!/bin/sh

. scripts/functions

p() {
    echo "##############################################" >> ${cwd}/tmp/log1.out
    echo "$*" >> ${cwd}/tmp/log1.out
    echo "##############################################" >> ${cwd}/tmp/log2.out
    echo "$*" >> ${cwd}/tmp/log2.out
}

#
# Run a basejob backup
#   then restore it.
#

TestName="prune-base-job-test"
JobName=backup

# 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

copy_test_confs
echo 's/backup_advance/base_backup/' > $tmp/s
echo 's/Name = backup/Name = backup; Base = base_backup, backup/' >> $tmp/s
sed -f $tmp/s $rscripts/bareos-dir.conf.accurate > $conf/bareos-dir.conf
rm -f $tmp/s

sed s/all,/all,saved,/ $conf/bareos-fd.conf > tmp/1
cp tmp/1 $conf/bareos-fd.conf

change_jobname BackupClient1 $JobName
dircfg=$conf/bareos-dir.conf

# modify some attributes in configuration file for the test
$bperl -e "add_attribute('$dircfg', 'AutoPrune', 'No', 'Client')"
$bperl -e "add_attribute('$dircfg', 'Job Retention', '1s', 'Client')"
$bperl -e "add_attribute('$dircfg', 'File', '<$tmp/lst', 'FileSet', 'FS_TESTJOB_ADVANCE')"

echo ${cwd}/bin > ${cwd}/tmp/lst
echo ${cwd}/bin > ${cwd}/tmp/file-list
echo ${BackupDirectory}/build >> ${cwd}/tmp/file-list
mkdir -p ${BackupDirectory}/build/po

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
label volume=TestVolume001 storage=File pool=Default
messages
END_OF_DATA

run_bareos

################################################################
p "Now do a backup using base backup"
#ckupDirectory}/build###############################################################

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
run job=base_backup level=base yes
wait
messages
update volume=TestVolume001 volstatus=Used
END_OF_DATA

run_bconsole

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out $tmp/log4.out
label volume=TestVolume002 storage=File pool=Default
run job=backup level=full yes
wait
messages
@exec "touch $BackupDirectory/build/po/fr.po"
run job=backup level=incremental yes
wait
messages
run job=backup level=differential yes
wait
messages
@# should be empty
run job=backup level=incremental yes
wait
messages
@$out $tmp/log2.out
@#
@# now do a restore
@#
restore fileset=FS_TESTJOB where=${cwd}/tmp/bareos-restores select all done
yes
wait
messages
quit
END_OF_DATA


run_bconsole
check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff $BackupDirectory/build || exit 1

export bstat dstat estat

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log4.out
@exec "touch $BackupDirectory/build/po/fr.po"
run job=backup level=incremental yes
wait
messages
@$out $tmp/log10.out
sql
SELECT count(*) FROM BaseFiles;

list jobs
prune jobs jobtype=backup yes
list jobs
@$out $tmp/log1.out
@################################################################
@# This full job will force the purge of the previous one
run job=backup level=full yes
wait
messages
@$out $tmp/log11.out
list jobs
prune jobs jobtype=backup yes
list jobs
@$out $tmp/log1.out
@################################################################
@# This base job will replace the first one after the next full replacement
run job=base_backup level=base yes
wait
messages
@$out $tmp/log1.out
@################################################################
@# This full job will force the purge of the previous one
run job=backup level=full yes
wait
messages
@$out $tmp/log13.out
list jobs
prune jobs jobtype=backup yes
list jobs
@$out $tmp/log1.out
@################################################################
@# see with two base jobs (base_backup and backup)
run job=backup level=base yes
wait
messages
@exec "touch $cwd/bin/bareos-dir.conf"
run job=base_backup level=base yes
wait
messages
list jobs
run job=backup level=full yes
wait
messages
list jobs
@$out $tmp/log14.out
list jobs
prune jobs jobtype=backup yes
list jobs
END_OF_DATA

run_bconsole

# Now we will verify that the pruning code is working as expected.  Each time,
# we run 'list jobs', 'prune', 'list jobs'. check_prune_list ensures that jobids
# in argument are present in the first 'list jobs', the 'prune' command deletes
# them (same number), and checks that the last 'list jobs' doesn't contain them
# anymore. See scripts/functions.pm for details.

# B F I D I0 R I -> B F D I
$bperl -e "check_prune_list('$tmp/log10.out', 3,5,6)"
estat=$(($estat + $?))

# B F D I F -> B F
$bperl -e "check_prune_list('$tmp/log11.out', 2,4,7)"
estat=$(($estat + $?))

# B F B F -> B F
$bperl -e "check_prune_list('$tmp/log13.out',1,8)"
estat=$(($estat + $?))

# B F B B F -> B B F
$bperl -e "check_prune_list('$tmp/log14.out',9,10)"
estat=$(($estat + $?))

stop_bareos
end_test
