#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Check that messed up hard and soft links will be correctly restored.
# Make a copy of directory "weird-files", delete a hard and a soft link,
# replace them with 2 soft links to a non existing file, restore and compare
# weird-files2-test use testls instead of diff.pl like in weird-files2-test
#
TestName="weird-files2-test"
JobName=weird-files2
. scripts/functions

if test ! -d weird-files ; then
   echo " "
   echo "weird files not configured. Test not run."
   exit 0
fi
cwd=`pwd`
scripts/cleanup
scripts/copy-test-confs
rm -rf weird-files2
cp -Rp weird-files weird-files2
echo "${cwd}/weird-files2" >${cwd}/tmp/file-list
cd weird-files2
#perl socket.pl     # we no longer restore sockets
cd ..

change_jobname NightlySave $JobName
start_test

#bin/testls weird-files2 | grep -v sock-file >${cwd}/tmp/original
bin/testls weird-files2 | sort -k 8 >${cwd}/tmp/original

cat <<END_OF_DATA >tmp/bconcmds
@output /dev/null
messages
@$out tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName yes
wait
messages
@output
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File 
#
# Now mess up the a hard link, and a soft link
#
cd weird-files2
rm -f hard-file2
ln hard-file3 hard-file2
rm -f soft-file2
ln -s soft-file3 soft-file2
cd ${cwd}
cat <<END_OF_DATA >tmp/bconcmds
@output /dev/null
messages
@# 
@# now do a restore
@#
@$out tmp/log2.out
restore where= storage=File
5
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA
ls -la weird-files2/
run_bconsole
check_for_zombie_jobs storage=File 
stop_bacula

bin/testls weird-files2 | sort -k 8 >${cwd}/tmp/restored

check_two_logs
diff -u ${cwd}/tmp/original ${cwd}/tmp/restored 2>&1 >/dev/null
#diff -u ${cwd}/tmp/original ${cwd}/tmp/restored
dstat=$?
if test $dstat -ne 0; then
   diff -u ${cwd}/tmp/original ${cwd}/tmp/restored
fi

end_test
