#!/bin/sh
#
# Run a simple backup using the compressed option
#   then backup four times, each with incremental then finally restore.
#   It should require at least 4 different bsrs.
#
# bscrypto requires to run as root or with addtional capabilities.
# On Linux these can be set via
#   setcap cap_sys_rawio=ep $bin/bscrypto
#   setcap cap_sys_rawio=ep $bin/bareos-sd
#
TestName="scsi-crypto-test"
JobName=scsicryptotest
. scripts/functions

require_tape_drive

scripts/cleanup-tape
scripts/copy-tape-confs

CRYPTO_CACHE="working/bareos-sd.${BAREOS_SD_PORT}.cryptoc"

rm -f  ${CRYPTO_CACHE}

# prepare director
KEY_ENCRYPTION_KEY=`${bin}/bscrypto -g -`
echo "Key Encryption Key = \"$KEY_ENCRYPTION_KEY\"" > ${conf}/scsicrypto-dir.conf

/bin/cp -f ${rconfigs}/${TestName}/bareos-dir.conf.${TestName} ${conf}/bareos-dir.conf
/bin/cp -f ${rconfigs}/${TestName}/bareos-sd.conf.${TestName}  ${conf}/bareos-sd.conf

change_jobname $JobName

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

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

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log1.out
label storage=tape volume=TestVolume001 slot=0 pool=Default encrypt
run job=$JobName yes
wait
messages
status storage=tape
quit
END_OF_DATA
run_bareos

scripts/check_for_zombie_jobs storage=tape
echo "Backup 1 done"

# make some files for the incremental to pick up
change_files

#
# run a second job
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
run job=$JobName level=Incremental yes
wait
messages
status storage=tape
quit
END_OF_DATA

run_bconsole

scripts/check_for_zombie_jobs storage=tape
echo "Backup 2 done"
change_files

#
# run a third job
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
run job=$JobName level=Incremental yes
wait
messages
status storage=tape
quit
END_OF_DATA

run_bconsole

scripts/check_for_zombie_jobs storage=tape
echo "Backup 3 done"
# make some files for the incremental to pick up
change_files

#
# run a fourth job
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
run job=$JobName level=Incremental yes
wait
messages
status storage=tape
sql
select volumename, encryptionkey from media;

quit
END_OF_DATA
run_bconsole

scripts/check_for_zombie_jobs storage=tape
echo "Backup 4 done"
#
# now do several restores to ensure we cleanup between jobs
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
restore where=${cwd}/tmp/bareos-restores select all done
yes
wait
restore where=${cwd}/tmp/bareos-restores select all done
yes
wait
@$out ${cwd}/tmp/log2.out
@#
@# now unmount the tape and start two restores
@#  at the same time
@#
unmount storage=tape
restore where=${cwd}/tmp/bareos-restores select all done
yes
restore where=${cwd}/tmp/bareos-restores select
unmark *
mark *
done
yes
mount storage=tape
wait
messages
status storage=tape
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=tape
stop_bareos

check_two_logs
check_restore_diff

echo "Content of cryptocache file:"
echo
${bin}/bscrypto -D ${CRYPTO_CACHE}
echo
echo "---------------------"

echo "Key Encryption Key:"
echo
cat ${conf}/scsicrypto-dir.conf
echo
echo "---------------------"

end_test
