#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# Copyright (C) 2021-2026 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Cloud test. We run backups and restore with the "truncate cache" command in-between
#
TestName="cloud-test-2clouds"
JobName=NightlySave
. scripts/functions

require_cloud

#config is required for cloud cleanup
scripts/copy-test-confs
scripts/cleanup

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

start_test

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "MaximumPartSize", "10000000", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Device Type", "Cloud", "Device")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Cloud", "'$CLOUD_NAME'", "Device")'

#$bperl -e 'comment_out("$conf/bacula-sd.conf", "", "Device", "Dummy")'

echo "Add an extra Cloud $CLOUD_NAME from ${conf}/bacula-sd.conf"
cfile="${cwd}/cloud.conf.sample"
if [ -f "${cwd}/cloud.conf" ]; then
    cfile="${cwd}/cloud.conf"
fi
if grep $CLOUD_NAME $cfile > /dev/null; then
    echo "Adding cloud $CLOUD_NAME in $cfile. Contact R@D manager for more info."
    $bperl -e "extract_resource('$cfile', 'Cloud', '$CLOUD_NAME')" | sed "s/Name = ${CLOUD_NAME}/Name = ${CLOUD_NAME}1/" >> ${conf}/bacula-sd.conf
else
    echo "Missing cloud $CLOUD_NAME in $cfile. Contact R@D manager for more info."
fi

BucketName=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'BucketName')")
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "BucketName", "'${BucketName}1'", "Cloud", "'${CLOUD_NAME}1'")'
$bperl -e 'add_attribute("$conf/bacula-sd.conf", "Cloud", "'${CLOUD_NAME}1'", "Device", "FileStorage1")'


cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log0.out
label storage=File volume=Vol0
END_OF_DATA

# do label
run_bacula

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File1 volume=Vol1
END_OF_DATA

# do label
run_bconsole

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
@#setdebug level=100 storage
run job=NightlySave level=Full storage=File yes
run job=BackupCatalog level=Full storage=File1 yes
wait
list volumes
llist volume=Vol0
llist volume=Vol1
messages
truncate cache volume=Vol0 storage=File
truncate cache volume=Vol1 storage=File1
END_OF_DATA

for i in 1 2 ; do
  echo "Running job $i"
  run_bconsole
  ls -l tmp/Vol*
done

cat <<END_OF_DATA >${cwd}/tmp/bconcmds-form
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
@#setdebug level=200 client
@#setdebug level=350 storage
truncate cache volume=Vol0 storage=File
@exec "ls -l ${cwd}/tmp/Vol0"
truncate cache volume=Vol1 storage=File1
@exec "ls -l ${cwd}/tmp/Vol1"
restore where=${cwd}/tmp/bacula-restores storage=File jobid=@jobid@
restore where=${cwd}/tmp/bacula-restores storage=File1 jobid=@jobid@
mark *
done
yes
wait
messages
sql
select * from JobMedia;

@$out $tmp/log30.out
cloud list volume=Vol0 storage=File
@$out $tmp/log31.out
cloud list volume=Vol1 storage=File1
quit
END_OF_DATA

# Restore and check each job
for i in 3 4; do
   rm -rf ${cwd}/tmp/bacula-restores $tmp/log3*out
   echo "s%@jobid@%$i%" >${cwd}/tmp/in
   sed -f ${cwd}/tmp/in ${cwd}/tmp/bconcmds-form >${cwd}/tmp/bconcmds
   run_bconsole
   nb=`cat $tmp/log30.out | wc -l`
   if [ "$nb" -lt 10 ]; then
       print_debug "ERROR: Not enough lines in $tmp/log3.out for the parts list"
       estat=1
   fi
   grep Vol0 $tmp/log30.out > /dev/null
   if [ $? -ne 0 ]; then
       print_debug "ERROR: Unable to find the volume list in $tmp/log31.out"
       estat=1
   fi
   grep Vol1 $tmp/log31.out > /dev/null
   if [ $? -ne 0 ]; then
       print_debug "ERROR: Unable to find the volume list in $tmp/log31.out"
       estat=1
   fi
done

ls -l tmp/Vol* >>${cwd}/tmp/log5.out
ls -l ${cwd}/tmp/Vol1

sleep 2
check_for_zombie_jobs storage=File 
stop_bacula

check_two_logs
#check_restore_diff

$rscripts/diff.pl -s "$FORCE_FILE_SET" -d "$cwd/tmp/bacula-restores/$FORCE_FILE_SET"
if test $? -ne 0; then
    dstat=$?
fi

end_test
