#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
#  Test RunScript
#

TestName="runscript-test"
JobName=backup

. scripts/functions
copy_test_confs

rm -f $conf/bacula-dir.conf
rm -f ${cwd}/tmp/RUN*log
rm -f ${cwd}/tmp/RUN_FD_FAILED

touch ${cwd}/tmp/log1.out
cp -f scripts/bacula-dir.conf.testrunscript $conf/bacula-dir.conf

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

start_test

sed "s!/opt/bacula!$cwd!" $cwd/build/scripts/MaximumConcurrentJobPerLevel > $tmp/asleep.sh
chmod +x $tmp/asleep.sh

cat <<EOF > $tmp/exit.sh
#!/bin/sh
exit 2
EOF

chmod +x $tmp/exit.sh

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
label volume=TestVolume001 pool=Scratch
setdebug dir level=200 tags=scheduler trace=1
@$out ${cwd}/tmp/RUN_FD_WARNING.log
run job=RUN_FD_WARNING yes
wait
messages
@sleep 1
@$out ${cwd}/tmp/RUN_ALL_OK.log
run job=RUN_ALL_OK yes
wait
messages
@sleep 1
@$out ${cwd}/tmp/RUN_FD_FAILED.log
run job=RUN_FD_FAILED yes
wait
messages
@sleep 1
@$out ${cwd}/tmp/RUN_DIR_FAILED.log
run job=RUN_DIR_FAILED yes
wait
messages
@sleep 1
@$out ${cwd}/tmp/RUN_FD_FAILED2.log
run job=RUN_FD_FAILED2 yes
wait
messages
@sleep 1
@$out ${cwd}/tmp/RUN_RESTO.log
restore where=${cwd}/tmp/bacula-restores select all done yes
wait
messages
st dir
@$out ${cwd}/tmp/RUN_JOBDEFS.log
run job=RUN_JOBDEFS yes
wait
messages
@$out $tmp/RUN_WAITQ_ASLEEP.log
run job=RUN_WAITQ_ASLEEP yes
@sleep 1
run job=RUN_WAITQ_ASLEEP yes
@sleep 1
run job=RUN_WAITQ_ASLEEP yes
@sleep 2
.status dir running
@sleep 2
.status dir running
@sleep 2
.status dir running
@sleep 2
.status dir running
@sleep 2
.status dir running
@sleep 2
.status dir running
@sleep 2
.status dir running
wait
messages
.jlist jobid=8
.jlist jobid=9
.jlist jobid=10
@$out $tmp/RUN_WAITQ_EXIT.log
run job=RUN_WAITQ_EXIT yes
wait
messages
.jlist jobid=11
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
stop_bacula

dstat=0
bstat=0
rstat=0
export dstat
export bstat
export rstat

grep 'shell command: run BeforeJob "/bin/echo RunBeforeJob"' ${cwd}/tmp/RUN_ALL_OK.log >/dev/null
a=$?
grep 'ClientRunBeforeJob: ClientRunBeforeJob' ${cwd}/tmp/RUN_ALL_OK.log >/dev/null
b=$?
grep 'shell command: run ClientAfterJob "/bin/echo ClientRunAfterJob' ${cwd}/tmp/RUN_ALL_OK.log >/dev/null
c=$?
grep 'shell command: run AfterJob "/bin/echo RunAfterJob' ${cwd}/tmp/RUN_ALL_OK.log >/dev/null
d=$?
grep 'ClientRunBefore1' ${cwd}/tmp/RUN_ALL_OK.log >/dev/null && \
grep 'ClientRunBefore2' ${cwd}/tmp/RUN_ALL_OK.log >/dev/null && \
grep 'ClientRunBefore3' ${cwd}/tmp/RUN_ALL_OK.log >/dev/null
e=$?

if [ $a = 0 -a $b = 0 -a $c = 0 -a $d = 0 -a $e = 0 ]
then
   [ "$debug" = 1 ] && echo RUN_ALL_OK ok
else
   echo "RUN_ALL_OK in error a=$a b=$b c=$c d=$d e=$e"
   bstat=1
fi

grep 'shell command: run BeforeJob "/bin/false RUN_DIR_FAILED"' ${cwd}/tmp/RUN_DIR_FAILED.log >/dev/null
a=$?
grep 'AfterJob: RunAfterFailedJob' ${cwd}/tmp/RUN_DIR_FAILED.log >/dev/null
b=$?
if [ $a = 0 -a $b = 0 ]
then
   [ "$debug" = 1 ] && echo RUN_DIR_FAILED ok
else
   echo "RUN_DIR_FAILED in error a=$a b=$b"
   bstat=1
fi

grep 'shell command: run ClientBeforeJob "/bin/false RUN_FD_FAILED1"' ${cwd}/tmp/RUN_FD_FAILED.log >/dev/null
a=$?
grep 'shell command: run ClientBeforeJob "/bin/false RUN_FD_FAILED2"' ${cwd}/tmp/RUN_FD_FAILED.log >/dev/null
b=$?
grep 'shell command: run ClientBeforeJob "/bin/false RUN_FD_FAILED3"' ${cwd}/tmp/RUN_FD_FAILED.log >/dev/null
c=$?
grep 'shell command: run AfterJob "/bin/echo RunAfterFailedJob"' ${cwd}/tmp/RUN_FD_FAILED.log >/dev/null
d=$?
grep 'touching' ${cwd}/tmp/RUN_FD_FAILED.log >/dev/null
e=$?
grep '\*\*\* Backup Error \*\*\*' ${cwd}/tmp/RUN_FD_FAILED.log >/dev/null
f=$?
if [ $a = 0 -a $b = 0 -a $c = 0 -a $d = 0 -a $e = 0 -a $f = 0 ]
then
   [ "$debug" = 1 ] && echo RUN_FD_FAILED ok
else
   echo "RUN_FD_FAILED in error a=$a b=$b c=$c d=$d e=$e f=$f"
   bstat=1
fi

grep 'shell command: run ClientBeforeJob "/bin/false RUN_FD_FAILED1"' ${cwd}/tmp/RUN_FD_FAILED2.log >/dev/null
a=$?
grep 'shell command: run ClientBeforeJob "/bin/false RUN_FD_FAILED2"' ${cwd}/tmp/RUN_FD_FAILED2.log >/dev/null
b=$?
grep 'shell command: run ClientBeforeJob "/bin/false RUN_FD_FAILED3"' ${cwd}/tmp/RUN_FD_FAILED2.log >/dev/null
c=$?
grep 'shell command: run AfterJob "/bin/echo RunAfterFailedJob"' ${cwd}/tmp/RUN_FD_FAILED2.log >/dev/null
d=$?
grep  '\*\*\* Backup Error \*\*\*' ${cwd}/tmp/RUN_FD_FAILED2.log >/dev/null
e=$?
grep 'RUN_FD_FAILED4' ${cwd}/tmp/RUN_FD_FAILED2.log >/dev/null
f=$?
if [ $a = 0 -a $b != 0 -a $c = 0 -a $d = 0 -a $e = 0 -a $f != 0 ]
then
   [ "$debug" = 1 ] && echo RUN_FD_FAILED ok
else
   echo "RUN_FD_FAILED2 in error a=$a b=$b c=$d d=$d e=$e"
   bstat=1
fi

grep 'shell command: run ClientBeforeJob "/bin/false RUN_FD_WARNING"' ${cwd}/tmp/RUN_FD_WARNING.log >/dev/null
a=$?
grep 'RunAfterFailedJob' ${cwd}/tmp/RUN_FD_WARNING.log >/dev/null
b=$?
grep 'Backup OK -- with warnings' ${cwd}/tmp/RUN_FD_WARNING.log >/dev/null
c=$?
if [ $a = 0 -a $b != 0 -a $c = 0 ]
then
   [ "$debug" = 1 ] && echo RUN_FD_WARNING ok
else
   echo "RUN_FD_WARNING in error a=$a b=$b c=$c"
   bstat=1
fi

grep 'ClientBeforeRestore' ${cwd}/tmp/RUN_RESTO.log >/dev/null
a=$?
grep 'ClientAfterRestore' ${cwd}/tmp/RUN_RESTO.log >/dev/null
b=$?
grep 'DirBeforeRestore' ${cwd}/tmp/RUN_RESTO.log >/dev/null
c=$?
grep 'DirAfterRestore' ${cwd}/tmp/RUN_RESTO.log >/dev/null
d=$?
if [ $a = 0 -a $b = 0 -a $c = 0 -a $d = 0 ]
then
   [ "$debug" = 1 ] && echo RUN_RESTO ok
else
   echo "RUN_RESTO in error a=$a b=$b c=$d d=$d"
   rstat=1
fi

grep '{' $tmp/RUN_WAITQ_ASLEEP.log | perl -MJSON -e '
$j1 = decode_json(<>);
$j2 = decode_json(<>);
$j3 = decode_json(<>);

# Sort the 3 jobs to determine the first one
@js = sort { $a->{data}->[0]->{starttime} cmp $b->{data}->[0]->{starttime} } ($j1, $j2, $j3);
unless ($js[0]->{data}->[0]->{endtime} lt $js[1]->{data}->[0]->{starttime}) {
       print "1 $js[0]->{data}->[0]->{endtime} lt $js[1]->{data}->[0]->{starttime}\n";
  exit 1;
}
unless ($js[1]->{data}->[0]->{endtime} lt $js[2]->{data}->[0]->{starttime}) {
       print "2 $js[1]->{data}->[0]->{endtime} lt $js[2]->{data}->[0]->{starttime}\n";
  exit 1;
}
exit 0;
'

if [ $? -ne 0 ]; then
    print_debug "ERROR: Incorrect job order reported in $tmp/RUN_WAITQ_ASLEEP.log"
    estat=1
fi

grep '{' $tmp/RUN_WAITQ_EXIT.log | perl -MJSON -e '
$j1 = decode_json(<>);
exit ($j1->{data}->[0]->{jobstatus} eq "A");
'
if [ $? -ne 1 ]; then
    print_debug "ERROR: Incorrect job status reported in $tmp/RUN_WAITQ_EXIT.log"
    estat=1
fi

end_test
