#!/bin/bash
set -e
set -u

TestName="$(basename "$(pwd)")"
export TestName

JobName=backup-bareos-fd

#shellcheck source=../../environment.in
. ./environment

CTEST_SKIP_RETURN_CODE=77

# check if required sudo commands can be executed, otherwise skip test
for SUDO_CMD in mount umount rm; do
  if ! "${SUDO}" -l "${SUDO_CMD}" >/dev/null 2>&1; then
    echo "cannot execute $SUDO_CMD via sudo" && exit "${CTEST_SKIP_RETURN_CODE}"
  fi
done

umount_glusterfs()
{
  #  make sure umount is successful
  tries=60
  echo "umounting glusterfs..."
  while "${SUDO}" mount | grep "${tmp}/data" >/dev/null 2>&1; do
    [ $((tries -= 1)) -eq 0 ] && {
      echo "could not umount glustrfs ${tmp}/data"
      exit 1
    }
    "${SUDO}" umount "${tmp}/data"
    echo -n "."
    sleep 1
  done
}

trap umount_glusterfs EXIT

#shellcheck source=../../scripts/functions
. "${BAREOS_SCRIPTS_DIR}"/functions

umount_glusterfs
"${BAREOS_SCRIPTS_DIR}"/cleanup
"${BAREOS_SCRIPTS_DIR}"/setup

# Directory to backup.
# This directory will be created by setup_data "$@"().
BackupDirectory="${tmp}/data"

[ -d "${tmp}/data" ] || mkdir -p "${tmp}/data"
"${SUDO}" mount -t glusterfs "${gfapi_fd_host}:/${gfapi_fd_testvolume}" "${tmp}/data"

"${SUDO}" rm -Rvf "${tmp}/data"/*

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data "$@"
# remove fifos and links
find "${tmp}/data" -type p -exec rm '{}' \;
find "${tmp}/data" -type l -exec rm '{}' \;

start_test

cat <<END_OF_DATA_BACKUP >"$tmp/bconcmds"
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
setdebug level=150 storage=File client
label volume=TestVolume001 storage=File pool=Full
run job=$JobName yes
status director
status client
status storage=File
wait
messages
status director
status client
status storage=File
messages
wait
status director
status client
status storage=File
messages
wait
status director
status client
status storage=File
messages
wait
status director
status client
status storage=File
messages
wait
status director
status client
status storage=File
messages
wait
quit
END_OF_DATA_BACKUP
run_bareos "$@"
check_for_zombie_jobs storage=File
stop_bareos

#[ -d "${tmp}/bareos-restores" ] && rm -Rvf "${tmp}/bareos-restores"
#mkdir -p "${tmp}"/bareos-restores/"${tmp}/data"
#now move the data away from gluster volume, restore and compare to moved data
#mv "${tmp}/data"/* "${tmp}"/bareos-restores"${tmp}/data"

cat <<END_OF_DATA_RESTORE >"$tmp/bconcmds"
@#
@# now do a restore
@#
@$out $tmp/log2.out
status storage=File
wait
setdebug client=bareos-fd level=1000 trace=1
restore client=bareos-fd fileset=SelfTest where=$tmp/bareos-restores/data select all done
yes
status client
messages
wait
status director
status client
status storage=File
messages
wait
quit
END_OF_DATA_RESTORE
run_bareos "$@"
check_for_zombie_jobs storage=File

check_two_logs
ln -s "${tmp}" "${tmp}/bareos-restores"
check_recursive_diff_of_dirs $tmp/data/weird-files $tmp/data/$tmp/bareos-restores/data/weird-files

umount_glusterfs
end_test
