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

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

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

# Fill ${BackupDirectory} with data.
setup_data

start_test

start_bareos

cat <<END_OF_DATA >$tmp/bconcmds
@$out ${NULL_DEV}
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
label volume=TestVolume001 storage=File pool=Full
label volume=TestVolume002 storage=File pool=Full
run job=$JobName yes
status director
status client
status storage=File
wait
messages
purge volume=TestVolume001 yes
delete volume=TestVolume001 yes
quit
END_OF_DATA

run_bconsole "$tmp/bconcmds"

expect_device_listing()
{
  log_no_device="${tmp}/no-device-$1.out"
  "$2" -c "${BAREOS_CONFIG_DIR}" &>"$log_no_device" || :
  if ! grep -q "\"FileStorage\"" "$log_no_device" || ! grep -q "\"FileStorage2\"" "$log_no_device"; then
    echo "Expected listing of available devices if no device is specified but not found in output, instead got:"
    cat $log_no_device
    stop_bareos
    exit 1
  fi

  log_invalid_device="${tmp}/invalid-device-$1.out"
  "$2" -c "${BAREOS_CONFIG_DIR}" InvalidDevice "${@:3}" &>"$log_invalid_device" || :
  if ! grep -q "\"FileStorage\"" "$log_invalid_device" || ! grep -q "\"FileStorage2\"" "$log_invalid_device"; then
    echo "Expected listing of available devices if invalid device is specified but not found in output, instead got:"
    cat $log_invalid_device
    stop_bareos
    exit 1
  fi
}

expect_device_listing bscan $BAREOS_BSCAN_BINARY
expect_device_listing bextract $BAREOS_BEXTRACT_BINARY .
expect_device_listing bls $BAREOS_BLS_BINARY
expect_device_listing bcopy $BAREOS_BCOPY_BINARY OtherInvalidDevice
expect_device_listing btape $BAREOS_BTAPE_BINARY

run_bls -vv -V TestVolume001 FileStorage
ret=$?
if [ $ret -ne 0 ]; then
  echo "bls exit code: $ret"
  stop_bareos
  exit $ret
fi

if ! grep -q "tests.bregtest.c" "$tmp/bls.out"; then
  echo 'Expected string \"tests.bregtest.c\" not found in  bls output'
  stop_bareos
  exit 1
fi

rm -rf "$tmp/bareos-restores"
mkdir -p "$tmp/bareos-restores"
run_bextract -vv -V TestVolume001 FileStorage "$tmp/bareos-restores"
ret=$?
if [ $ret -ne 0 ]; then
  echo "bextract exit code: $ret"
  stop_bareos
  exit $ret
else
  find "$tmp/bareos-restores"
  check_restore_diff "${BackupDirectory}"            # check extracted files
  mv "$tmp/bareos-restores" "$tmp/bextract-restores" # move extracted files away
  mkdir -p "$tmp/bareos-restores"
fi

run_bscan_db -vv -s -V TestVolume001 FileStorage
ret=$?
if [ $ret -ne 0 ]; then
  echo "bscan exit code: $ret"
  stop_bareos
  exit $ret
fi

original_job_id=1
new_job_id=2

if ! grep -q "Created new JobId=${new_job_id} record for original JobId=${original_job_id}" "$tmp/bscan.out"; then
  echo 'Job numbers of scanned job are not correct'
  stop_bareos
  exit 1
fi

num_sos=$(grep -c '^Begin Job Session Record:' "$tmp/bscan.out")
if [ "$num_sos" -ne 1 ]; then
  echo "Found $num_sos start of session records instead of 1"
  stop_bareos
  exit 1
fi
num_eos=$(grep -c '^End Job Session Record:' "$tmp/bscan.out")
if [ "$num_eos" -ne 1 ]; then
  echo "Found $num_eos end of session records instead of 1"
  stop_bareos
  exit 1
fi

total_jobid_records=$(grep -cE '^JobId +: ' "$tmp/bscan.out")
my_jobid_records=$(grep -cE "^JobId +: ${original_job_id}" "$tmp/bscan.out")

if [ "$my_jobid_records" -ne 2 ]; then
  echo "Got $my_jobid_records mentions of my jobid, expected 2"
  stop_bareos
  exit 1
fi
if [ "$my_jobid_records" -ne "$total_jobid_records" ]; then
  echo "Volume contains excess session records:"
  echo "Got $total_jobid_records in total, only $my_jobid_records mention my original jobid"
  stop_bareos
  exit 1
fi

# use bcopy to copy the job and extract it afterwards to verify copy is verbatim
# TestVolume 002 is the target volume for the copy operation
run_bcopy -v -i TestVolume001 -o TestVolume002 FileStorage FileStorage2

rm -rf "$tmp/bareos-restores"
mkdir -p "$tmp/bareos-restores"
run_bextract -vv -V TestVolume002 FileStorage2 "$tmp/bareos-restores"
ret=$?

if [ $ret -ne 0 ]; then
  echo "bextract exit code: $ret"
  stop_bareos
  exit $ret
else
  find "$tmp/bareos-restores"
  check_restore_diff "${BackupDirectory}"            # check extracted files
  mv "$tmp/bareos-restores" "$tmp/bextract-restores" # move extracted files away
  mkdir -p "$tmp/bareos-restores"
fi

cat <<END_OF_DATA >"$tmp/bconcmds2"
@#
@# now do a restore
@#
@$out $tmp/log2.out
wait
restore client=bareos-fd fileset=SelfTest where=$tmp/bareos-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bconsole "$tmp/bconcmds2"

check_for_zombie_jobs storage=File

check_two_logs
check_restore_diff "${BackupDirectory}"
end_test
