#!/bin/sh
#
# Run a job with a limited tape size so that the tape fills up.
#  Then manually unload the tape and load a new one, label
#  it.
#
TestName="manual-two-vol-tape"
JobName=manualtwovolltape
. scripts/functions

require_tape_drive
require_autochanger

scripts/cleanup
scripts/copy-tape-confs

sleep=90

slot=0
export slot

# TODO -- check this
init_slot $TAPE_DRIVE $SLOT1
init_slot $TAPE_DRIVE $SLOT2

load_slot1

init_drive ${TAPE_DRIVE}

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

# Label second volume
cat <<END_OF_DATA >tmp/bconcmds
@out /dev/null
messages
@$out tmp/log1.out
label storage=tape volume=TestVolume002 slot=0 pool=Default drive=0
unmount storage=tape
quit
END_OF_DATA

run_bareos

# Load slot2
load_other_slot $slot
init_drive ${TAPE_DRIVE}

# label first volume, and run first half of job
cat <<END_OF_DATA >tmp/bconcmds
@$out tmp/log1.out
label storage=tape volume=TestVolume001 slot=0 pool=Default drive=0
update Volume=TestVolume001 MaxVolBytes=500000 pool=Default drive=0
list volumes
@#setdebug level=150 storage=tape
run job=$JobName yes
@# This sleep must be long enough for the first tape to fill (4MB)
@#  otherwise the following mtx-changer commands will fail
@#sleep $sleep
wait mount timeout=180
list volumes
unmount storage=tape
list volumes
status storage=tape
messages
quit
END_OF_DATA

run_bconsole

load_other_slot $slot

cat <<END_OF_DATA >tmp/bconcmds
@$out tmp/log1.out
wait mount timeout=180
setdebug level=200 storage=tape
mount storage=tape
@# backup should continue
wait
list volumes
@#
@# now do a restore
@#
restore where=${cwd}/tmp/bareos-restores select all storage=tape done
yes
@sleep $sleep
wait mount timeout=180
exec "sh -c 'load_other_slot $slot'"
unmount storage=tape
list volumes
messages
wait
quit
END_OF_DATA

# run second half of backup + restore
run_bconsole

touch tmp/log2.out

check_for_zombie_jobs storage=tape
stop_bareos

check_two_logs
#check_restore_diff
dstat=0   # no diff
rstat=0   # no restore
end_test
