#!/bin/sh
#
# Test for a tape truncation bug.
#
TestName="truncate-bug-tape"
JobName=truncatebug
. scripts/functions

require_tape_drive

scripts/cleanup-tape
scripts/copy-tape-confs

change_jobname NightlySave $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 >tmp/bconcmds
@$out /dev/null
messages
@$out   tmp/log1.out
label storage=tape volume=TestVolume001 slot=0 pool=Default
@# do a bunch of saves so we have 12 files on the tape
@#setdebug level=100 dir
@#setdebug level=100 storage=tape
run job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
run level=Full job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bareos
scripts/check_for_zombie_jobs storage=tape

cat <<END_OF_DATA >tmp/bconcmds
@$out /dev/null
messages
@$out tmp/log2.out
@#
@# now do a restore
@#
restore where=${cwd}/tmp/bareos-restores storage=tape
3
@# select JobId=4 (i.e. file five on the tape)
4
cd ${BackupDirectory}
@# mark a single file
mark configure
done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
scripts/check_for_zombie_jobs storage=tape

# Now write to tape one more time
cat <<END_OF_DATA >tmp/bconcmds
@$out /dev/null
messages
@$out tmp/log3.out
run level=Full job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=tape
stop_bareos

check_two_logs
grep "^  Termination: *Backup OK" tmp/log3.out 2>&1 >/dev/null
dstat=$?
end_test
