#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# What to test
# ------------
# - Normal backup
# - Weird backup
# - Restore
# - Accurate backup
# - Compression
# - Checksum
# - TLS/Encryption
# - compare files
# - runscript
#
# How to use this test
# --------------------
# See config or prototype.conf for all variables
# Client name. Note, it must be configured to accept your regress director
# REMOTE_CLIENT="test"
# Client FQDN or IP address
# REMOTE_ADDR="192.168.0.10"
# File or Directory to backup.  This is put in the "File" directive 
#   in the FileSet
# REMOTE_FILE="/tmp"
# Port of the remote client
# REMOTE_PORT=8102
# Remote Client password
# REMOTE_PASSWORD="zzzzz"
# Where the remote client will have to contact the SD, usually the local ip address
# REMOTE_STORE_ADDR="192.168.0.15"
#
#
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/bacula/bin/bacula-fd
if [ $? != 0 ]; then
    print_debug "ERROR: Need to install bacula on $REMOTE_ADDR"
    echo "ERROR: Need to install bacula on $REMOTE_ADDR"
    exit 1
fi

$bperl -e remote_stop
$bperl -e remote_init
$bperl -e remote_config

if [ $? -ne 0 ]; then
    print_debug "ERROR: found errors with remote binaries"
    estat=1
fi

cfg=$conf/bacula-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/log4.out
@#
run job=CompressedTest fileset=LZOSet 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_bacula
check_for_zombie_jobs storage=File
stop_bacula

check_two_logs

version=`$bin/bacula-dir -? 2>&1 | 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
