#!/bin/sh
#
# Do some tests with statistics commands
#
TestName="stats-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
echo "s% QueryFile%Statistics Retention = 1 sec; QueryFile%g" >> $outf
cp ${cwd}/bin/bareos-dir.conf ${cwd}/tmp/1
sed -f ${outf} ${cwd}/tmp/1 >${cwd}/bin/bareos-dir.conf

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label volume=TestVolume001 storage=File pool=Full
run job=$JobName yes
wait
messages
update stats days=1
sql
SELECT 'FOUND' AS test FROM JobHisto;

run job=$JobName yes
wait
messages
@sleep 5
@$out ${cwd}/tmp/log2.out
update stats
sql
SELECT 'FOUND' AS test FROM JobHisto;

list jobs
prune stats yes
@$out ${cwd}/tmp/log3.out
sql
SELECT 'FOUND' AS test FROM JobHisto;

quit
END_OF_DATA

run_bareos

dstat=0
rstat=0
bstat=0

check_for_zombie_jobs storage=File
stop_bareos


COUNT=`grep FOUND ${cwd}/tmp/log1.out | wc -l`
if [ $COUNT -ne 1 ]; then
    print_debug "Bad count in JobHisto table"
    bstat=1
fi

COUNT=`grep FOUND ${cwd}/tmp/log2.out | wc -l`
if [ $COUNT -ne 3 ]; then
    print_debug "Bad count in JobHisto table"
    bstat=2
fi

COUNT=`grep FOUND ${cwd}/tmp/log3.out | wc -l`
if [ $COUNT -ne 1 ]; then
    print_debug "Bad count in JobHisto table"
    bstat=3
fi

end_test
