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

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

#shellcheck source=./environment
. ./environment
. ./test-config

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

start_test

on_error() {
  local lc="${BASH_COMMAND}"
  echo "Error occurred in testrunner script [${lc}]"
  export estat=1
  exit 1
}
trap 'on_error' ERR

echo "Set debug on the Tape-Storage daemon"
tracefile="$(echo "setdebug level=200 trace=1 storage=Tape-0" | bin/bconsole | grep "^3000" | awk -F"tracefile=" '{print $2}' )"
: > "${tracefile}" # clear tracefile

echo "Label barcodes"
for i in $(seq ${NUMBER_OF_POOLS}); do
  pool=$(( i -1 )) #counts from 0
  for j in $(seq ${NUMBER_OF_TAPES_PER_POOL}); do
    slot=$(( j + pool * NUMBER_OF_TAPES_PER_POOL )) #counts from 1
    echo "label barcodes slot=${slot} drive=0 pool=Full-${pool} storage=Tape-0 yes" | bin/bconsole | grep -E "(OK label|already exists)"
  done
done

# create the test backup jobs
rm -f bconsole_backup_jobs

spooling="spooldata=yes"
for i in $(seq ${NUMBER_OF_TEST_ROUNDS}); do
  for j in $(seq ${NUMBER_OF_POOLS}); do
    cat << EOF >> bconsole_backup_jobs
run job=backup-bareos-fd level=Full storage=Tape-0 pool=Full-$(( j -1 )) ${spooling} yes
status dir
EOF
    if [ $(( NUMBER_OF_SPOOLING_JOBS_PER_ROUND -j )) -le 0 ]; then
      spooling=""
    fi
  done
done

# start all the jobs
bin/bconsole < bconsole_backup_jobs

DevicesFromConfig=$(grep Device= ${current_test_directory}/etc/bareos/bareos-sd.d/autochanger/autochanger.conf | sed -n -e 's/^.*Device=//p')
tapedevices=($DevicesFromConfig)

rm -f $tmp/log-label-release-autoselect-no.out
# set devices to `autoselect = no` and wait for jobs to finish
cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $tmp/log-label-release-autoselect-no.out
setdevice storage=Tape-0  device=${tapedevices[0]} autoselect=no
wait
messages

label barcodes slot=8 drive=0 pool=Full-0 storage=Tape-0 yes
setdevice storage=Tape-0  device=${tapedevices[0]} autoselect=yes
wait
quit
END_OF_DATA

run_bconsole

# Redo the backup to check if drive 0 is still being used
rm -f bconsole_backup_jobs
echo "@$out $tmp/log-second-backups.out" >> bconsole_backup_jobs

spooling="spooldata=yes"
for i in $(seq ${NUMBER_OF_TEST_ROUNDS}); do
  for j in $(seq ${NUMBER_OF_POOLS}); do
    cat << EOF >> bconsole_backup_jobs
run job=backup-bareos-fd level=Full storage=Tape-0 pool=Full-$(( j -1 )) ${spooling} yes
status dir
EOF
    if [ $(( NUMBER_OF_SPOOLING_JOBS_PER_ROUND -j )) -le 0 ]; then
      spooling=""
    fi
  done
done

echo "wait" >> bconsole_backup_jobs
echo "messages" >> bconsole_backup_jobs

# start all the jobs again
bin/bconsole < bconsole_backup_jobs

#testing for releasing when device is on autoselect=no
cat <<END_OF_DATA >"$tmp/bconcmds"
@$out $tmp/log-label-release-autoselect-no.out
setdevice storage=Tape-0  device=${tapedevices[0]} autoselect=no
release storage=Tape-0 drive=0
messages
quit
END_OF_DATA

run_bconsole

#check the that the labeling went through
if ! grep "Slot 8 successfully created." "$tmp"/log-label-release-autoselect-no.out &&
   grep "3999 Device \"autochanger-0\" not found or could not be opened." "$tmp"/log-label-release-autoselect-no.out
   then
  echo "Labeling of a device with autoselect=no was not successful. Check $tmp/log-label-release-autoselect-no.out" >&2
  estat=1
fi

#check that release went through
if ! grep "3921 Device \"\"${tapedevices[0]}\" (.*)\" already released."  "$tmp"/log-label-release-autoselect-no.out &&
     grep "3999 Device \"autochanger-0\" not found or could not be opened." "$tmp"/log-label-release-autoselect-no.out
   then
  echo "Releasing a device with autoselct=no was not successful. Check $tmp/log-label-release-autoselect-no.out" >&2
  estat=1
fi

#check that the tapedrive works normally again after the manipulations
if  ! grep "Using .* \"${tapedevices[0]}" "$tmp"/log-second-backups.out; then
  echo "A device that was on autoselect=no and then back to autoselect=yes was not used. Check $tmp/log-second-backups.out" >&2
  estat=1
fi


check_for_zombie_jobs storage=Tape-0

end_test
exit 0
