#!/bin/sh
#
# Copyright (C) 2000-2021 Kern Sibbald
# Copyright (C) 2022-2026 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Simple test to check if storage list is correctly handled for the LastBackupedTo policy
#

TestName="store-mngr-lastbackedupto"
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

# Limit per-device max concurrent jobs to 1
$bperl -e 'add_virtual_changer("vDisk1", 5)'
$bperl -e 'add_virtual_changer("vDisk2", 5)'
$bperl -e 'add_virtual_changer("vDisk3", 5)'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "LabelFormat", "Vol", "Pool")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "CommCompression", "no", "Director")'
$bperl -e 'add_attribute("$conf/bacula-fd.conf", "CommCompression", "no", "FileDaemon")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "CommCompression", "no", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "AllowCompression", "no", "Storage")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Autoprune", "no", "Director")'
sed 's/sparse=yes;//' $conf/bacula-dir.conf > $tmp/1
mv $tmp/1 $conf/bacula-dir.conf


ERROR="'E'||'RROR'" 
if echo -- $WHICHDB | grep mysql
then
    ERROR="CONCAT('E', 'RROR')"
fi

cat >> $conf/bacula-dir.conf <<EOF
Pool {
  Name = Test1
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365d             # one year
  LabelFormat = Vol
  Storage = vDisk2, vDisk3
  Storage Group Policy = LastBackedUpTo
}
Pool {
  Name = FullPool
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365d             # one year
  LabelFormat = Vol
  Storage = vDisk2, vDisk3
  Storage Group Policy = LastBackedUpTo
}
Pool {
  Name = IncrPool
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365d             # one year
  LabelFormat = Vol
  Storage = vDisk2, vDisk3
  Storage Group Policy = LastBackedUpTo
}
Pool {
  Name = DiffPool
  Pool Type = Backup
  Recycle = yes                       # Bacula can automatically recycle Volumes
  AutoPrune = yes                     # Prune expired volumes
  Volume Retention = 365d             # one year
  LabelFormat = Vol
  Storage = vDisk2, vDisk3
  Storage Group Policy = LastBackedUpTo
}
EOF

echo $tmp/f > $tmp/file-list
dd if=/dev/zero of=$tmp/f count=10000

# Add two simple jobs (one of them with store group)
cat <<END_OF_DATA >> $bin/bacula-dir.conf
Job {
  Name = "PoolOverwrite"
  Client = $HOST-fd
  Messages = Standard
  Type = Backup
  Storage = File
  Pool = Default
  FileSet="Full Set"
  FullBackupPool = FullPool
  IncrementalBackupPool = IncrPool
  DifferentialBackupPool = DiffPool
}

Job {
  Name = "SingleStoreJob"
  Client = $HOST-fd
  Messages = Standard
  Type = Backup
  Storage = File
  Pool = Default
  FileSet="Full Set"
}
Job {
  Name = "SingleStorePool"
  Client = $HOST-fd
  Messages = Standard
  Type = Backup
  Storage = File
  Pool = Test1
  FileSet="Full Set"
}
Job {
  Name = "StoreGroupJob"
  Type = Backup
  Client = $HOST-fd
  Messages = Standard
  Storage = vDisk1, vDisk2
  Storage Group Policy = LastBackedUpTo
  Pool = Default
  FileSet = "Full Set"
}
END_OF_DATA

$bperl -e 'set_global_maximum_concurrent_jobs(10)'

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out  ${cwd}/tmp/log1.out
setdebug level=5 trace=1 trace=1 dir
label volume=TestVolume001 storage=File1 pool=Default slot=1 drive=0
label volume=TestVolume002 storage=File2 pool=Default slot=1 drive=0
@$out  ${cwd}/tmp/log1.out
run job=StoreGroupJob comment="vDisk1" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk2" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk2" yes
wait
messages

run job=StoreGroupJob comment="vDisk2" yes
wait
messages

run job=StoreGroupJob comment="vDisk2" yes
wait
messages

run job=StoreGroupJob comment="vDisk1" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk1" yes
wait
messages

run job=StoreGroupJob comment="vDisk1" yes
wait
messages

run job=StoreGroupJob comment="vDisk1" yes
wait
messages

run job=StoreGroupJob comment="vDisk2" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk2" yes
wait
messages

run job=StoreGroupJob comment="vDisk1" level=Full yes
wait
messages

list jobs

quit
END_OF_DATA

echo "*********** Run some backups with vDisk1 and vDisk2 *************************"
run_bacula

# add a new vDisk Storage
sed -i 's/vDisk1, vDisk2/vDisk1, vDisk2, vDisk3/g' $bin/bacula-dir.conf

echo "*********** Run some backups after adding vDisk3 *************************"

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
reload
@output /dev/null
messages

@$out  ${cwd}/tmp/log1.out
run job=StoreGroupJob comment="vDisk3" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk2" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk1"  level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk1" yes
wait
messages

run job=StoreGroupJob comment="vDisk1" yes
wait
messages

run job=StoreGroupJob comment="vDisk1" yes
wait
messages

run job=StoreGroupJob comment="vDisk1" yes
wait
messages

run job=StoreGroupJob comment="vDisk3" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk3" yes
wait
messages

run job=StoreGroupJob comment="vDisk3" yes
wait
messages

run job=StoreGroupJob comment="vDisk2" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk1" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk1" yes
wait
messages

run job=StoreGroupJob comment="vDisk3" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk3" yes
wait
messages

run job=StoreGroupJob comment="vDisk3" yes
wait
messages

run job=StoreGroupJob comment="vDisk2" level=Full yes
wait
messages

run job=StoreGroupJob comment="vDisk2" yes
wait
messages

@# Job with the Storage/StorageGroup specified in the Pool and the pool specified in command line
run job=SingleStoreJob comment="vDisk2" level=Full pool=Test1 yes
wait
messages

run job=SingleStoreJob  comment="vDisk2" pool=Test1 yes
wait
messages

run job=SingleStoreJob comment="vDisk2" pool=Test1 yes
wait
messages

run job=SingleStoreJob comment="vDisk3" level=Full pool=Test1 yes
wait
messages

run job=SingleStoreJob comment="vDisk3" pool=Test1 yes
wait
messages

@# We run a job with a pool in parameter that should use it
run job=StoreGroupJob comment="vDisk3" level=Full pool=Test1 yes
wait
messages

run job=StoreGroupJob  comment="vDisk3" pool=Test1 yes
wait
messages

run job=StoreGroupJob comment="vDisk3" pool=Test1 yes
wait
messages

run job=StoreGroupJob comment="vDisk2" level=Full pool=Test1 yes
wait
messages

run job=StoreGroupJob comment="vDisk2" pool=Test1 yes
wait
messages

@# Job with the Storage/StorageGroup specified in the Pool
run job=SingleStorePool comment="vDisk2" level=Full yes
wait
messages

run job=SingleStorePool comment="vDisk2"  yes
wait
messages

run job=SingleStorePool comment="vDisk3"  level=Full yes
wait
messages

run job=SingleStorePool comment="vDisk2"  level=Full yes
wait
messages

@# Should select the storage provided in command line fixed in 18.1
@#run job=SingleStorePool comment="vDisk1" storage=vDisk1 level=Full yes
@#wait
@#messages

run job=PoolOverwrite comment="vDisk2" yes
wait
messages

run job=PoolOverwrite comment="vDisk2" yes
wait
messages

run job=PoolOverwrite comment="vDisk2" level=Differential yes
wait
messages

run job=PoolOverwrite comment="vDisk2" level=Differential yes
wait
messages

@#sleep 3m
wait
messages

@$out $tmp/log2.out
sql
SELECT 'OK', J1.Level, J1.JobId, J1.Name, S1.Name, Comment FROM Job AS J1 JOIN Storage AS S1 ON (S1.StorageId = J1.WriteStorageId) WHERE J1.Comment = S1.Name  UNION ALL SELECT $ERROR, J2.Level, J2.JobId, J2.Name, S2.Name, Comment FROM Job AS J2 JOIN Storage AS S2 ON (S2.StorageId = J2.WriteStorageId) WHERE J2.Comment <> S2.Name ORDER BY JobId;
SELECT $ERROR, Job.Level, Job.JobId, Job.Name, 'BAD ALGO' FROM Job JOIN Log USING (JobId) WHERE LogText LIKE '%StorageGroupPolicy%' AND LogText NOT LIKE '%LastBackedUpTo%';

messages
quit
END_OF_DATA

run_bconsole

stop_bacula

grep ERROR $tmp/log2.out
if [ $? = 0 ]; then
    print_debug "ERROR: Should not find jobs with not the correct storage in $tmp/log2.out"
    estat=1
fi

nb=`grep OK $tmp/log2.out | wc -l`
if [ $nb -lt 10 ]; then
    print_debug "ERROR: Should find many results in $tmp/log2.out"
    estat=1
fi


end_test
