#!/bin/sh
#
# What to test
# ------------
# - Normal backup
# - Weird backup
# - Restore
# - Accurate backup
# - Compression
# - Checksum
# - TLS/Encryption
# - compare files
# - runscript
#
# How to use this test
# --------------------
#
#
TestName="remote-fd-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

change_jobname NightlySave $JobName
change_jobname MonsterSave AccurateTest

start_test

echo $REMOTE_FILE/save > $tmp/file-list

ssh $REMOTE_ADDR test -x /opt/bareos/bin/bareos-fd
if [ $? != 0 ]; then
    print_debug "ERROR: Need to install bareos on $REMOTE_ADDR"
    echo "ERROR: Need to install bareos on $REMOTE_ADDR"
    exit 1
fi

$bperl -e remote_stop
$bperl -e remote_init
$bperl -e remote_config
cfg=$conf/bareos-dir.conf
$bperl -e "add_attribute('$cfg', 'clientrunbeforejob', '$REMOTE_FILE/save/test.sh', 'Job', 'CompressedTest')"
$bperl -e "add_attribute('$cfg', 'accurate', 'yes', 'Job', 'AccurateTest')"
$bperl -e "add_attribute('$cfg', 'address', '$REMOTE_ADDR', 'Client')"
$bperl -e "add_attribute('$cfg', 'password', '$REMOTE_PASSWORD', 'Client')"
$bperl -e "add_attribute('$cfg', 'address', '$REMOTE_STORE_ADDR', 'Storage')"

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output
messages
@$out ${cwd}/tmp/log4.out
status client
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName storage=File yes
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=$REMOTE_FILE/restore select all done storage=File
yes
wait
messages
@$out ${cwd}/tmp/log3.out
@#
@# make some runscript tests
@#
run job=CompressedTest storage=File yes
wait
messages
@$out ${cwd}/tmp/log1.out
@#
@# make some accurate tests
@#
run job=AccurateTest storage=File yes
wait
messages
run job=AccurateTest storage=File yes
wait
messages
@output
quit
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=File
stop_bareos

check_two_logs

version=`$bin/bareos-dir -? | awk '/Version:/ { print $2 }'`
version_fd=`awk '/Version:/ { print $3 }' $tmp/log4.out`

if [ "$version" != "$version_fd" ]; then
    print_debug "ERROR: client version doesn't match $version ($version_fd)"
    estat=1
fi

grep 'this is a script' $tmp/log3.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Can't find runscript output"
    bstat=1
fi

$bperl -e remote_diff
if [ $? != 0 ]; then
    dstat=1
fi

$bperl -e remote_stop

end_test
