#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run backups with dummy tape driver
# This test setups an Autochanger with 80 slots
# and 5 drives (5 LTO3)
#
# 

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

require_vtape

scripts/cleanup
scripts/copy-tape-confs
cp $rscripts/bacula-dir-vtape.conf $conf/bacula-dir.conf
sed 's/LTO1/LTO3/g' $rscripts/bacula-sd-vtape.conf > $conf/bacula-sd.conf
scripts/prepare-fake-autochanger

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

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Concurrent Jobs", "1", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Maximum Volume Size", "100MB", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "LabelMedia", "Yes", "Device", "FileStorage")'

$bperl -e 'add_attribute("$conf/bacula-fd.conf", "MaximumBandwidthPerJob", "1200Kb/s", "FileDaemon")'

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Label Format",        "TestVolume", "Pool", "Full")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Maximum Volume Jobs", "1",    "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "SpoolData",           "no",   "Job")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "NextPool",            "Full", "Pool", "Inc")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Storage",             "File", "Pool", "Full")'

cat <<EOF >> $conf/bacula-dir.conf
Job {
  Name = "copy-job"
  Type = Copy
  Level = Full
  Client=${HOST}-fd
  FileSet="Full Set"
  Messages = Standard
  Storage = LTO3
  Write Bootstrap = "$working/migratejob.bsr"
  Pool = Default
  Maximum Concurrent Jobs = 4
  Selection Type = Job
  Selection Pattern = ".*Save.*"
# Allow Duplicate Jobs = No
}
EOF

perl -Mscripts::functions -e "extract_resource('$conf/bacula-dir.conf', 'Job', 'NightlySave')" > $tmp/1
cat $tmp/1 | sed s/NightlySave/NightlySave4/ >> $conf/bacula-dir.conf
cat $tmp/1 | sed s/NightlySave/NightlySave5/ >> $conf/bacula-dir.conf
cat $tmp/1 | sed s/NightlySave/NightlySave6/ >> $conf/bacula-dir.conf
cat $tmp/1 | sed s/NightlySave/NightlySave7/ >> $conf/bacula-dir.conf
cat $tmp/1 | sed s/NightlySave/NightlySave8/ >> $conf/bacula-dir.conf
cat $tmp/1 | sed s/NightlySave/NightlySave9/ >> $conf/bacula-dir.conf
cat $tmp/1 | sed s/NightlySave/NightlySave10/ >> $conf/bacula-dir.conf

start_test

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

when1=`perl -MPOSIX -e "print strftime('%F %T', localtime(time+20))"`
when2=`perl -MPOSIX -e "print strftime('%F %T', localtime(time+35))"`
when3=`perl -MPOSIX -e "print strftime('%F %T', localtime(time+50))"`

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

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log6.out
@#setdebug level=200 storage=LTO1
label barcodes pool=Scratch slots=1-60 storage=LTO3 drive=0
yes
messages
list volumes
END_OF_DATA

run_bacula

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
setdebug trace=1 level=200 storage=LTO3
mount storage=LTO3_2 slot=60 drive=2
run storage=LTO3 when="$when1" job=NightlySave   pool=Inc     yes
run storage=LTO3 when="$when2" job=NightlySave2  pool=Inc     yes
run storage=LTO3 when="$when3" job=NightlySave3  pool=Inc     yes
wait jobid=1
status storage=LTO3
messages
run storage=LTO3_2  job=copy-job jobid=1 pool=Inc yes
wait jobid=2
messages
run storage=LTO3_2  job=copy-job jobid=2 pool=Inc yes
wait jobid=3
messages
run storage=LTO3_2  job=copy-job jobid=3 pool=Inc yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=LTO3 client=$clientname

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

run_bconsole
check_for_zombie_jobs storage=LTO3 client=$clientname

stop_bacula

check_two_logs
check_restore_diff
perl -ne '
# foreach Job, we store the drive and we count the total
# job number per drive
m/(\d+): Using Device "(.+?)"/ 
  and $drive_info[$i++]="$1 $2" and $drive{$2}++;

END {
 $err=0;
 foreach $k (sort keys %drive) { 
    if ($drive{$k} > 3) {
       print "ERR $k has more than 3 jobs ($drive{$k})\n";
       $err++;
    }
 }
 if ($err) {
    foreach $k (@drive_info) { print "$k\n"}
    exit 1;
 }
}
' $tmp/log1.out

if [ $? -ne 0 ]; then
    print_debug "ERR: Problem during log analysis"
    estat=1
fi
end_test
