#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory then create some           
#   new files, do an Incremental and restore those two files.
#
# This script uses the virtual disk autochanger and two drives
#
TestName="2drive-incremental-2disk"
JobName="2drive2disk"
. scripts/functions

scripts/cleanup
scripts/copy-2disk-drive-confs
scripts/prepare-disk-changer
CLIENT=2drive2disk

change_jobname localhost-fd $JobName
start_test

echo "${cwd}/tmp/build" >${cwd}/tmp/file-list
if test ! -d ${cwd}/tmp/build ; then
   mkdir -p ${cwd}/tmp/build
fi
cp -p ${cwd}/build/src/dird/*.c ${cwd}/tmp/build
cd ${cwd}/tmp
echo "${cwd}/tmp/build/ficheriro1.txt" >restore-list
echo "${cwd}/tmp/build/ficheriro2.txt" >>restore-list
cd ${cwd}

# Turn off Prefer Mounted Volumes so we use 2 drives
outf="${cwd}/tmp/sed_tmp"
echo "s%# Prefer Mounted Volumes%  Prefer Mounted Volumes%g" >${outf}
cp ${cwd}/bin/bacula-dir.conf ${cwd}/tmp/1
# Comment the next line out to write everything to one drive
#  otherwise, it writes the two jobs to different drives
sed -f ${outf} ${cwd}/tmp/1 >${cwd}/bin/bacula-dir.conf

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
setdebug trace=1 level=3 dir
setdebug trace=1 level=3 storage=tape
setdebug trace=1 level=3 client=2drive2disk 
label storage=tape volume=TestVolume001 slot=1 Pool=Default drive=0
label storage=tape volume=TestVolume002 slot=2 Pool=Default drive=1
status storage=tape
setdebug trace=1 level=200 storage=tape
run job=NightlySave yes
run job=NightlySave yes
run job=NightlySave yes
run job=NightlySave yes
run job=NightlySave yes
wait jobid=2
status storage=tape
list volumes
list jobs
messages
wait
list volumes
list jobs
status storage=tape
messages
quit
END_OF_DATA

run_bacula
echo "ficheriro1.txt" >${cwd}/tmp/build/ficheriro1.txt
echo "ficheriro2.txt" >${cwd}/tmp/build/ficheriro2.txt


cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
@# Force Incremental on the second Volume
update volume=TestVolume001 VolStatus=Used
status storage=tape
list volumes
@#setdebug level=120 storage=tape
run level=Incremental job=NightlySave yes
wait
list volumes
status storage=tape
messages
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bacula-restores 
7
<${cwd}/tmp/restore-list

yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=tape

stop_bacula

check_two_logs
# The restore should read from TestVolume002, which was on drive 1
grep TestVolume002 ${cwd}/tmp/log2.out >/dev/null 2>&1
# Note rstat may already be set, so we don't just store into it
if [ $? != 0 ] ; then
   rstat=$?
fi

#
# Delete .c files because we will only restored the txt files
#
rm -f ${cwd}/tmp/build/*.c
check_restore_tmp_build_diff

end_test
