#!/bin/bash
set -e
set -o pipefail
set -u
#
# Run a simple backup
#   then restore it.
#
TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-to-pool1
#shellcheck source=../../environment.in
. ./environment

JobName=backup-bareos-fd
#shellcheck source=../../scripts/functions
. "${rscripts}"/functions
"${rscripts}"/cleanup
"${rscripts}"/setup

# Fill ${BackupDirectory} with data.
setup_data

start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
setdevice storage=File device=MultiFileStorage0001 autoselect=yes
setdevice storage=File device=MultiFileStorage0002 autoselect=yes
setdevice storage=File device=MultiFileStorage0003 autoselect=yes
run job=backup-to-pool1 level=Full yes
run job=backup-to-pool2 level=Full yes
run job=backup-to-pool3 level=Full yes
run job=backup-to-pool1 level=Full yes
run job=backup-to-pool2 level=Full yes
run job=backup-to-pool3 level=Full yes
status storage=File
wait
messages
@#
@# now do a restore
@#
@$out $tmp/log2.out
restore client=bareos-fd fileset=SelfTest where=$tmp/bareos-restores select all done
yes
wait
messages
@#
@# now purge and truncate
@#
@$out $tmp/log3.out
purge volume=Full-0001 yes
truncate volstatus=Purged drive=0 yes
purge volume=Full-0002 yes
truncate volstatus=Purged drive=1 yes
purge volume=Full-0003 yes
truncate volstatus=Purged drive=2 yes
messages
quit
END_OF_DATA

run_bareos "$@"
check_for_zombie_jobs storage=File

# rename the storages instead of deleting them
mv ./etc/bareos/bareos-dir.d/storage/fakestorage1.conf ./etc/bareos/bareos-dir.d/storage/fakestorage1.conf_backup
mv ./etc/bareos/bareos-dir.d/storage/fakestorage2.conf ./etc/bareos/bareos-dir.d/storage/fakestorage2.conf_backup

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $tmp/log4.out
label volume=fakevolume storage=fakestorage1 pool=Full
reload
delete storage=File
delete storage=fakestorage1
messages
delete storage=fakestorage2
messages
quit
END_OF_DATA

run_bconsole

# rename files to be able to run the test again later
mv ./etc/bareos/bareos-dir.d/storage/fakestorage1.conf_backup ./etc/bareos/bareos-dir.d/storage/fakestorage1.conf
mv ./etc/bareos/bareos-dir.d/storage/fakestorage2.conf_backup ./etc/bareos/bareos-dir.d/storage/fakestorage2.conf

check_two_logs
check_restore_diff ${BackupDirectory}

#make sure all the setdevice commands were successful
grep "3000 OK setdevice=MultiFileStorage0001 autoselect=1" "${tmp}"/log1.out >/dev/null 2>&1 \
  && grep "3000 OK setdevice=MultiFileStorage0002 autoselect=1" "${tmp}"/log1.out >/dev/null 2>&1 \
  && grep "3000 OK setdevice=MultiFileStorage0003 autoselect=1" "${tmp}"/log1.out >/dev/null 2>&1
if test $? -ne 0; then
  echo "The setdevice command failed for a MultiFileStorage."
  estat=1
fi

if test $estat -ne 1; then
  #make sure all MultiFileStorage devices are used
  grep "Using Device \"MultiFileStorage0001\"" "${tmp}"/log1.out >/dev/null 2>&1 \
    && grep "Using Device \"MultiFileStorage0002\"" "${tmp}"/log1.out >/dev/null 2>&1 \
    && grep "Using Device \"MultiFileStorage0003\"" "${tmp}"/log1.out >/dev/null 2>&1
  if test $? -ne 0; then
    echo "Not all MultiFileStorage devices are used."
    estat=1
  fi
fi

#make sure all volumes are purged
grep "There are no more Jobs associated with Volume \"Full-0001\". Marking it purged." "${tmp}"/log3.out >/dev/null 2>&1 \
  && grep "There are no more Jobs associated with Volume \"Full-0002\". Marking it purged." "${tmp}"/log3.out >/dev/null 2>&1 \
  && grep "There are no more Jobs associated with Volume \"Full-0003\". Marking it purged." "${tmp}"/log3.out >/dev/null 2>&1
if test $? -ne 0; then
  echo "The Purge command failed for a MultiFileStorage."
  estat=1
fi

#make sure all volumes are correctly truncated with the correct drive
grep "3000 OK label. VolFiles=0 VolBytes=... Volume=\"Full-0001\" Device=\"MultiFileStorage0001\" (storage)" "${tmp}"/log3.out >/dev/null 2>&1 \
  && grep "3000 OK label. VolFiles=0 VolBytes=... Volume=\"Full-0002\" Device=\"MultiFileStorage0002\" (storage)" "${tmp}"/log3.out >/dev/null 2>&1 \
  && grep "3000 OK label. VolFiles=0 VolBytes=... Volume=\"Full-0003\" Device=\"MultiFileStorage0003\" (storage)" "${tmp}"/log3.out >/dev/null 2>&1 \
  && grep "The volume 'Full-0001' has been truncated." "${tmp}"/log3.out >/dev/null 2>&1 \
  && grep "The volume 'Full-0002' has been truncated." "${tmp}"/log3.out >/dev/null 2>&1 \
  && grep "The volume 'Full-0003' has been truncated." "${tmp}"/log3.out >/dev/null 2>&1
if test $? -ne 0; then
  echo "The Truncate command failed for a MultiFileStorage."
  estat=1
fi

# make sure the devices really have been truncated
for i in 1 2 3; do
  size=$(wc -c <"storage/Full-000${i}")
  if [ "$size" -ge 300 ]; then
    echo "file storage/Full-000$i" was not truncated
    estat=3
  fi
done

# check if the relabel went correctly
grep "3000 OK label. VolFiles=0 VolBytes=... Volume=\"fakevolume\" Device=\"FileStorage\" (storage)" "${tmp}"/log4.out >/dev/null 2>&1
if test $? -ne 0; then
  echo "The fakevolume relabel failed."
  estat=1
fi

# make sure exact number of orphaned storages is found
grep "Found 2 orphaned Storage records" "${tmp}"/log4.out >/dev/null 2>&1
if test $? -ne 0; then
  echo "The delete command failed to find the exact number of fakestorages."
  estat=1
fi

# make sure storage 'File' cannot be deleted
grep "The given storage 'File' either does not exist at all, or still exists in the configuration." "${tmp}"/log4.out >/dev/null 2>&1
if test $? -ne 0; then
  echo "The delete command failed for an existing storage."
  estat=1
fi

# make sure extra storages fakestorage1 cannot be deleted
grep "Orphaned storage 'fakestorage1' is being used by volume 'fakevolume'" "${tmp}"/log4.out >/dev/null 2>&1
if test $? -ne 0; then
  echo "The delete command failed to detect fakestorages related to other medias."
  estat=1
fi

end_test
