#!/bin/bash
set -e
set -o pipefail
set -u
#
# Run multiple backups in parallel and
#   then restore them.
#
TestName="$(basename "$(pwd)")"
export TestName

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

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

start_test

backupjob="slow-backup-big-volume"
parallelbackuplog="$tmp/backup-parallel-jobs.out"
parallelrestorelog="$tmp/restore-parallel-jobs.out"
bandwidthlog="$tmp/bandwidth-parallel-jobs.out"
# Hold each connected File daemon job until the bandwidth assertion finishes.
gate_dir=$(mktemp -d "$tmp/bandwidth-gate.XXXXXX")
trap 'touch "$gate_dir/release"' EXIT
trap 'touch "$gate_dir/release"; estat=999; end_test' TERM INT
cat >"$tmp/bandwidth-gate.sh" <<END_OF_GATE
touch "$gate_dir/ready.\$1"
while [ ! -f "$gate_dir/release" ]; do sleep 1; done
END_OF_GATE

rm -f $parallelbackuplog
rm -f $parallelrestorelog

cat <<END_OF_DATA >"$tmp/bconcmds"
setbandwidth limit=200 client=bareos-fd yes
status client=bareos-fd
setbandwidth limit=0 client=bareos-fd yes
setbandwidth limit=200 storage=File yes
status storage=File
setbandwidth limit=0 storage=File yes
quit
END_OF_DATA
timeout "${BCONSOLE_TIMEOUT:-180s}" "${BAREOS_BCONSOLE_BINARY}" -c "${BAREOS_CONFIG_DIR}" \
  <"$tmp/bconcmds" >"$bandwidthlog"
if [[ $(grep -c 'bwlimit=200kB/s' "$bandwidthlog" || :) -ne 2 ]]; then
  echo "File or Storage daemon did not receive the bandwidth limit"
  cat "$bandwidthlog"
  estat=1
fi

"${BAREOS_GENTESTDATA_BINARY}" --size=524288 >"${BackupDirectory}/bandwidth-test.bin"

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out ${NULL_DEV}
messages
@$out $parallelbackuplog
run job=${backupjob} level=Full yes
run job=${backupjob} level=Full yes
run job=${backupjob} level=Full yes
run job=${backupjob} level=Full yes
run job=${backupjob} level=Full yes
run job=${backupjob} level=Full yes
quit
END_OF_DATA

run_bconsole

backup_jobids=($(grep 'Job queued.' $parallelbackuplog | sed -n -e 's/^.*JobId=//p'))

num_jobs=${#backup_jobids[@]}

if [[ "$num_jobs" -ne 6 ]]; then
  echo "Expected six queued backup jobs, got $num_jobs"
  exit 1
fi

for attempt in $(seq 1 60); do
  timeout "${BCONSOLE_TIMEOUT:-180s}" "${BAREOS_BCONSOLE_BINARY}" -c "${BAREOS_CONFIG_DIR}" \
    <<<"status client=bareos-fd" >"$bandwidthlog"
  running=0
  for jobid in "${backup_jobids[@]}"; do
    if [[ -f "$gate_dir/ready.$jobid" ]] \
      && grep -q "JobId $jobid Job .* is running" "$bandwidthlog"; then
      running=$((running + 1))
    fi
  done
  if [[ "$running" -eq "$num_jobs" ]]; then break; fi
  sleep 1
done
if [[ "$running" -ne "$num_jobs" ]]; then
  echo "Not all backup jobs reached the File daemon"
  cat "$bandwidthlog"
  exit 1
fi

cat <<END_OF_DATA >"$tmp/bconcmds"
setbandwidth limit=200 all yes
status client=bareos-fd
quit
END_OF_DATA
timeout "${BCONSOLE_TIMEOUT:-180s}" "${BAREOS_BCONSOLE_BINARY}" -c "${BAREOS_CONFIG_DIR}" \
  <"$tmp/bconcmds" >"$bandwidthlog"

if [[ $(grep -c 'Bwlimit=204,800' "$bandwidthlog" || :) -ne "$num_jobs" ]]; then
  echo "Not all running backup jobs received the bandwidth limit"
  cat "$bandwidthlog"
  estat=1
fi
touch "$gate_dir/release"

{
  echo "@$out $parallelbackuplog"
  for jobid in "${backup_jobids[@]}"; do
    echo "setbandwidth limit=0 jobid=$jobid yes"
  done
  echo "wait"
  echo "messages"
  echo "quit"
} >"$tmp/bconcmds"
run_bconsole

find "$tmp" -type d -name "restore*" -exec rm -rf {} +

cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $parallelrestorelog
restore jobid=${backup_jobids[0]} where=$tmp/restore1 all done yes
restore jobid=${backup_jobids[1]} where=$tmp/restore2 all done yes
restore jobid=${backup_jobids[2]} where=$tmp/restore3 all done yes
restore jobid=${backup_jobids[3]} where=$tmp/restore4 all done yes
restore jobid=${backup_jobids[4]} where=$tmp/restore5 all done yes
restore jobid=${backup_jobids[5]} where=$tmp/restore6 all done yes
wait
messages
quit
END_OF_DATA

run_bconsole

if [[ $(grep -c "Termination:.*Backup OK" "$parallelbackuplog") -ne "$num_jobs" ]]; then
  echo "Not all backups jobs finished successfully."
  estat=1
fi

if [[ $(grep -c "Termination:.*Restore OK" "$parallelrestorelog") -ne "$num_jobs" ]]; then
  echo "Not all restore jobs finished successfully."
  estat=2
fi

for i in $(seq 1 "$num_jobs"); do
  restoredir="$tmp/restore$i"
  check_restore_diff "${BackupDirectory}" "$restoredir"
done

end_test
