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

#
#  Test file relocation feature
#

TestName="regexwhere-test"
JobName=backup

. scripts/functions

scripts/cleanup
scripts/copy-test-confs

/bin/cp -f scripts/bacula-dir.conf.regexwhere bin/bacula-dir.conf

#
# Remove the !restored from the messages resource because
#  this test needs the restored listing to verify the
#  restore results
#
outf="${cwd}/tmp/sed_tmp"
echo "s%, \!restored%%g" >${outf}
cp ${cwd}/bin/bacula-fd.conf ${cwd}/tmp/1
sed -f ${outf} ${cwd}/tmp/1 >${cwd}/bin/bacula-fd.conf

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

start_test

cat <<END_OF_DATA >tmp/bconcmds
@$out tmp/RUN_backup
messages
label volume=TestVolume001
run job=backup yes
wait
messages
@##################################
@$out tmp/RUN_JobA
restore
5
cd ${cwd}/build/po
m *.po
done
1
yes
wait
messages
@##################################
@$out tmp/RUN_JobB
restore
5
cd ${cwd}/build/po
m *.po
done
2
yes
wait
messages
@##################################
@$out tmp/RUN_JobC
restore
5
cd ${cwd}/build/po
m *.po
done
3
yes
wait
messages
@##################################
@$out tmp/RUN_JobD
restore
5
cd ${cwd}/build/po
m *.po
done
4
yes
wait
messages
@##################################
@$out tmp/RUN_JobE
restore
5
cd ${cwd}/build/po
m *.po
done
5
yes
wait
messages
@##################################
@$out tmp/RUN_JobF
restore
5
cd ${cwd}/build/po
m *.po
done
6
yes
wait
messages
@##################################
@$out tmp/RUN_JobG
restore strip_prefix="${cwd}" add_prefix="${tmp}/bacula-restore.$$"
5
cd ${cwd}/build/po
m *.po
done
7
yes
wait
messages
@##################################
@$out tmp/RUN_JobH
restore add_suffix=.old
5
cd ${cwd}/build/po
m *.po
done
7
yes
wait
messages
@##################################
@$out tmp/RUN_JobI
restore regexwhere="!Po!old!i,!old!po!,!\$!.old!"
5
cd ${cwd}/build/po
m *.po
done
7
yes
wait
messages
@##################################
@$out tmp/RUN_JobJ
restore
5
cd ${cwd}/build/po
m *.po
done
7
mod
@## File Relocation -> add suffix -> test -> accept
10
3
.old
5
/etc/passwd
.
6
@## Where 
m
9
tmp/bacula-restore-it
m
@## use add suffix
10
3
.old
6
yes
wait
messages
@sleep 1
quit
END_OF_DATA

run_bacula

stop_bacula


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

J=JobA
grep "Restore OK"  tmp/RUN_$J > /dev/null && \
grep "${cwd}/build/po/fr.old.po" tmp/RUN_$J > /dev/null

if [ $? = 0 ] 
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi

J=JobB
grep "Restore OK"  tmp/RUN_$J > /dev/null && \
grep "tmp/bacula-restore${cwd}/po/fr.po.old" tmp/RUN_$J > /dev/null
# $cwd begins with /
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi

J=JobC
grep "Restore OK"  tmp/RUN_$J > /dev/null && \
grep "tmp/bacula-restore${cwd}/build/po/fr.po" tmp/RUN_$J > /dev/null
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi

J=JobD
grep "Restore Error"  tmp/RUN_$J > /dev/null
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi

J=JobE
grep "Restore OK"  tmp/RUN_$J > /dev/null && \
grep "${cwd}/tmp/fr.po"  tmp/RUN_$J > /dev/null
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi

J=JobF
grep "Restore OK"  tmp/RUN_$J > /dev/null && \
grep "${cwd}/build/po/fr.po" tmp/RUN_$J > /dev/null
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi

J=JobG
grep "Restore OK"  tmp/RUN_$J > /dev/null && \
grep "tmp/bacula-restore.$$/build/po/fr.po" tmp/RUN_$J > /dev/null
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi
rm -rf ${tmp}/bacula-restore.$$

J=JobH
grep "Restore OK"  tmp/RUN_$J > /dev/null && \
grep "${cwd}/build/po/fr.po.old" tmp/RUN_$J > /dev/null
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi

J=JobI
grep "Restore OK"  tmp/RUN_$J > /dev/null && \
grep "${cwd}/build/po/fr.po.old" tmp/RUN_$J > /dev/null
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi


J=JobJ
grep "Restore OK"      tmp/RUN_$J > /dev/null && \
grep "/etc/passwd.old" tmp/RUN_$J > /dev/null && \
grep "Where: *tmp/bacula-restore-it" tmp/RUN_$J > /dev/null && \
grep "${cwd}/build/po/fr.po.old" tmp/RUN_$J > /dev/null
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo $J ok
else
   echo "$J in error"
   rstat=1
fi

# test with bregtest

find ${cwd}/build > tmp/list
# thanks to BSD like system for that...
sed -e 's/[Rr][Ee][Aa][Dd][Mm][Ee]/readme/' -e 's/\([ch]\)$/.old.\1/' tmp/list > tmp/list.sed
./build/src/tools/bregtest -s -f tmp/list -e '/readme/readme/i,/([ch])$/.old.$1/' > tmp/list.bsed

diff tmp/list.bsed tmp/list.sed >/dev/null 2>&1
if [ $? = 0 ]
then
    [ "$debug" = 1 ] && echo bregtest ok
else
   echo "bregtest test in error"
   rstat=1

fi

rm -f $cwd/build/po/*old*

end_test
