#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test the Address = "|my script" Client field
#
TestName="dynamic-client-address-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

change_jobname BackupClient1 $JobName
start_test

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Address', '|echo 127.0.0.11', 'Client')"

cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
label volume=TestVolume001 storage=File1 pool=File slot=1 drive=0
show client
status client
estimate job=$JobName
run job=$JobName yes
wait
messages
@# 
@# now do a restore
@#
@$out $tmp/log2.out  
setdebug level=4 storage=File1
restore where=$tmp/bacula-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File1

nb=`grep "127.0.0.11" $tmp/log1.out | wc -l`
if [ $nb != 3 ]; then
    print_debug "ERROR: Should find 5 times the ip address in $tmp/log1.out"
    estat=1
fi

grep "echo 127.0.0.11" $tmp/log1.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should find the command $tmp/log1.out"
    estat=1
fi

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Address', '|aaaaa', 'Client')"
cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log3.out
reload
status client
estimate job=$JobName
messages
quit
END_OF_DATA

run_bconsole

nb=`grep "invalid address" $tmp/log3.out| wc -l`
if [ $nb != 4 ]; then
    print_debug "ERROR: Should find 4 times 'invalid address' in $tmp/log3.out"
    estat=1
fi

stop_bacula
check_two_logs
check_restore_diff
end_test
