#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# 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
echo "${cwd}/build" >${cwd}/tmp/file-list
MTX=$AUTOCHANGER_SCRIPT

# Sleep longer if real tape
if [ x${USE_VTAPE} = xyes ] ; then
   sleep=20 
else
   sleep=90
fi

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
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_bacula

# 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/bacula-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_bacula

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