#!/bin/sh

. scripts/functions

require_tape_drive

# Define the Name of the test as "TestName".
# Should be the same as the filename, therefore we use the filename as default.
TestName="`basename $0`"

# set other test specific variables
Client=bareos-fd
JobName=backup-bareos-fd

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

# Remove old configuration, working and tmp files. Setup the database.
cleanup

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data".
setup_data data/small.tgz

${rscripts}/set_tape_options

# Config files will be copied to required location.
# Base configuration files come from the
# configs/BASE/ directory, see
# configs/BASE/README.txt for an overview of the provides resources.
# Store your additonal resources to
# configs/$TestName.
# It is also possible to overwrite resources from configs/BASE.
# If you define a full config file (instead of config files for individual resources),
# only these will be taken.
copy_configs

# The default fileset FS_TESTJOB backups all files and directories defined in "${tmp}/file-list".
# setup_data() sets this automatically to "$tmp/data/",
# so it only be needs to be set if another directory is used.
#echo "${BackupDirectory}" >${tmp}/file-list

get_block_and_data_size()
{
    # block 1 and 2 are label blocks.
    # blocks >= 3 are data blocks.
    # Parameter 1 is the block of interest. Default is block 1.
    LINE=${1:-1}
    ${bin}/bls $TAPE_DRIVE -V"*" -k -d 250 | sed -r -n -e "s/^bls .* Exit read_block (read_len=[0-9]+) (block_len=[0-9]*)/\1 \2/p" | sed "${LINE}q;d"
}

get_block_size()
{
    # block 1 and 2 are label blocks.
    # blocks >= 3 are data blocks.
    # Parameter 1 is the block of interest. Default is block 1.
    LINE=${1:-1}
    ${bin}/bls $TAPE_DRIVE -V"*" -k -d 250 | sed -r -n -e "s/^bls .* Exit read_block read_len=([0-9]+) block_len=[0-9]*/\1/p" | sed "${LINE}q;d"
}

get_data_size()
{
    # block 1 and 2 are label blocks.
    # blocks >= 3 are data blocks.
    # Parameter 1 is the block of interest. Default is block 1.
    LINE=${1:-1}
    ${bin}/bls $TAPE_DRIVE -V"*" -k -d 250 | sed -r -n -e "s/^bls .* Exit read_block read_len=[0-9]+ block_len=([0-9]*)/\1/p" | sed "${LINE}q;d"
}



# start the test
start_test

cat <<END_OF_DATA >tmp/bconcmds
messages
@$out ${tmp}/log-config.out
show client=${Client}
show storage=tape
@$out tmp/log1.out
@#setdebug level=2 storage=tape
label storage=tape slot=0 volume=TestVolume001 pool=Default
run job=$JobName level=Full storage=tape yes
wait
messages
@#
@# now do a restore
@#
@$out ${tmp}/log2.out w
restore client=${Client} where=${tmp}/bareos-restores select storage=tape
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

# Start the bareos daemons
# and run the bconsole commands from ${tmp}/bconcmds
# Further bconsole commands can be executed by "run_bconsole".
run_bareos

# verify that all are terminated
if ! check_for_zombie_jobs storage=tape client=${Client}; then
    set_error "zombie jobs"
fi

# stop bareos
stop_bareos

# check log files for common error messages
check_log ${tmp}/log1.out
check_log ${tmp}/log2.out

# check tmp/log1.out and tmp/log2.out for errors
check_two_logs

# check for differences between original files and restored files
check_restore_diff ${BackupDirectory}

# do some manual testing
if [ ! -d ${BackupDirectory} ]; then
    set_error "Directory ${BackupDirectory} does not exists any more."
fi

check_log ${tmp}/log-config.out
if ! grep -qi "TlsRequire = yes" ${tmp}/log-config.out; then
   set_error "client is not configured to use TLS."
fi

DEFAULT_BLOCK_SIZE=64512
blocksize_label=$(get_block_size 1)
print_debug "Label block size: $blocksize_label"
if [ $blocksize_label != $DEFAULT_BLOCK_SIZE ]; then
    set_error "Wrong label block size: expected: $DEFAULT_BLOCK_SIZE, got: $blocksize_label"
fi

blocksize_data=$(get_block_size 3)
print_debug "Data block size (block 3): $blocksize_data"
if [ $blocksize_data != $DEFAULT_BLOCK_SIZE ]; then
    set_error "Wrong data block size (block 3): expected: $DEFAULT_BLOCK_SIZE, got: $blocksize_data"
fi

# end tests and check for error codes
end_test
