#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# Run backups with dummy tape driver
# This test setups an Autochanger with 800 slots
# and 5 drives (3 LTO3 and 2 LTO1)
#
# TAPE_DRIVE="$cwd/working/ach/drive0"
# TAPE_DRIVE1="$cwd/working/ach/drive0"
# AUTOCHANGER="$cwd/working/ach/conf"
# USE_VTAPE=yes
# AUTOCHANGER_SCRIPT=disk-changer
# 

TestName="vtape-test-changer"
JobName=backup
. scripts/functions

require_vtape

scripts/cleanup
scripts/copy-tape-confs
cp $rscripts/bacula-dir-vtape.conf $conf/bacula-dir.conf
cp $rscripts/bacula-sd-vtape.conf $conf/bacula-sd.conf
scripts/prepare-fake-autochanger 300

perl -Mscripts::functions \
    -e "set_maximum_concurrent_jobs('$conf/bacula-dir.conf', 100)"
perl -Mscripts::functions \
    -e "set_maximum_concurrent_jobs('$conf/bacula-sd.conf', 100)"
perl -Mscripts::functions \
    -e "set_maximum_concurrent_jobs('$conf/bacula-fd.conf', 100)"

echo "${cwd}/build" >${cwd}/tmp/file-list

start_test

when1=`perl -Mscripts::functions -e "get_time(30)"`
when2=`perl -Mscripts::functions -e "get_time(45)"`

clientname=`awk '/Name = .*-fd/ { if (!ok) { print $3 ; ok=1 } }' bin/bacula-dir.conf`

perl -Mscripts::functions \
    -e "extract_resource('$conf/bacula-dir.conf', 'Job', 'NightlySave')" > $tmp/1

for i in `seq 4 40`; do
    sed "s%NightlySave%NightlySave$i%" $tmp/1 >> $conf/bacula-dir.conf
done



# Catalog record for cleaning tape "CLN01" successfully created.
# CLN01      | Cleaning

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log7.out
label barcodes pool=Scratch slots=1-300 storage=LTO3 drive=0
yes
messages
list volumes
@$out ${cwd}/tmp/log1.out
run level=full storage=LTO3 when="$when1" job=NightlySave  pool=Inc     yes
run level=full storage=LTO3 when="$when1" job=NightlySave1 pool=Full    yes
run level=full storage=LTO3 when="$when1" job=NightlySave2 pool=Default yes
run level=full storage=LTO3 when="$when1" job=NightlySave3 pool=Default yes
run level=full storage=LTO3 when="$when1" job=NightlySave4 pool=Inc     yes
run level=full storage=LTO3 when="$when1" job=NightlySave5 pool=Full    yes
run level=full storage=LTO3 when="$when1" job=NightlySave6 pool=Default yes
run level=full storage=LTO3 when="$when1" job=NightlySave7 pool=Default yes
run level=full storage=LTO3 when="$when1" job=NightlySave8 pool=Inc     yes
run level=full storage=LTO3 when="$when1" job=NightlySave9 pool=Full    yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave10  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave11  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave12  pool=Inc     yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave13  pool=Full    yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave14  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave15  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave16  pool=Inc     yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave17  pool=Full    yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave18  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave19  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave20  pool=Inc     yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave21  pool=Full    yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave22  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave23  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave24  pool=Inc     yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave25  pool=Full    yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave26  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave27  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave28  pool=Inc     yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave29  pool=Full    yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave30  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave31  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave32  pool=Inc     yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave33  pool=Full    yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave34  pool=Default yes
@#run level=full storage=LTO3 when="$when1" job=NightlySave35  pool=Default yes
@sleep 50
@#wait jobid=1
@#restore client=$clientname fileset="Full Set" pool=Inc where=${cwd}/tmp/bacula-restores1 select all done
@#yes
@#sleep 30
cancel
1
yes
@sleep 1
cancel
1
yes
@sleep 1
cancel
1
yes
@sleep 1
cancel
1
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=LTO3 client=$clientname

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log2.out  
@# 
@# now do a restore
@#
restore client=$clientname fileset="Full Set" pool=Full where=${cwd}/tmp/bacula-restores select all done
yes
wait
messages
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=LTO3 client=$clientname

# test update slots
# remove volume vol20 from autochanger
grep -v vol20 ${cwd}/working/ach/barcodes > ${cwd}/tmp/1
cp ${cwd}/tmp/1 ${cwd}/working/ach/barcodes

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log3.out
list volume=vol20
update slots slots=5-40 storage=LTO3 drive=3
list volume=vol20
END_OF_DATA

run_bconsole

stop_bacula

check_two_logs
check_restore_diff

# get InChanger flag
RES=`awk -F'|' '/vol20 .+(Full|Append)/ { print $11 }' ${cwd}/tmp/log3.out`
set $RES
if [ $1 -eq $2 ]; then
    print_debug "ERROR: Error in update slots"
    bstat=1
fi

grep LTO1_5 tmp/log1.out > /dev/null
if test $? = 0; then
    print_debug "ERROR: AutoSelect option broken"
    bstat=1
fi

grep 'Error: Re-read' tmp/log1.out > /dev/null
if test $? = 0; then
    print_debug "ERROR: Found Re-read errors"
    #bstat=1
fi

end_test
