#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# Copyright (C) 2021-2026 Bacula Systems SA
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a couple of tests with bconsole and dotcommands
#
TestName="console-exec"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-test-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

echo "$cwd/build/po" > $tmp/file-list

change_jobname NightlySave $JobName
start_test

cat <<EOF > $tmp/bconcmds
@out /dev/null
messages
@$out $tmp/log1.out
@exec "echo OK"
EOF

run_bacula

$bperl -e "add_attribute('$conf/bconsole.conf', 'AllowedScriptDirectories', '*none*', 'Director')" 

cat <<EOF > $tmp/bconcmds
@out /dev/null
messages
@$out $tmp/log2.out
@exec "echo OK"
@$out $tmp/log3.out
@exec "*none*/echo OK"
EOF
run_bconsole

$bperl -e "add_attribute('$conf/bconsole.conf', 'AllowedScriptDirectories', '/bin', 'Director')" 

cat <<EOF > $tmp/bconcmds
@out /dev/null
messages
@$out $tmp/log4.out
@exec "/usr/sbin/echo OK"
EOF
run_bconsole

nb=`grep OK $tmp/log1.out | wc -l`
if [ $nb != 2 ]; then
    print_debug "ERROR: Should find echo OK in $tmp/log1.out"
    estat=1
fi

grep "Not in allowed paths" $tmp/log2.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should not find successfull execution in $tmp/log2.out"
    estat=1
fi

grep "Not in allowed paths" $tmp/log3.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should not find successfull execution in $tmp/log3.out"
    estat=1
fi

grep "Not in allowed paths" $tmp/log4.out > /dev/null
if [ $? != 0 ]; then
    print_debug "ERROR: Should not find successfull execution in $tmp/log4.out"
    estat=1
fi


CHECK_API2_JSON=0
CHECK_JSON=0
stop_bacula
end_test
