#!/bin/sh
#
# This script uses the virtual disk autochanger
#
TestName="multi-storage-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-2disk-confs
scripts/prepare-disk-changer

# create a new bareos-sd.conf
BAREOS_SD2_CONF=$conf/bareos-sd2.conf
BAREOS_SD2_PORT="1${BAREOS_SD_PORT}"
perl -ne '
if (/SDPort = /) { $_ =~ s/(\d+)/1$1/;}
if (/Name = .+?-sd/) { $_ =~ s/-sd/-sd2/;}
if (/WorkingDirectory/) { $_ =~ s/"$/\/StorageDaemon2"/; }
print;
' $conf/bareos-sd.conf > $BAREOS_SD2_CONF

mkdir -p ${working}/StorageDaemon2


perl -ne '
if (/^Storage \{/) { $in=1; $nb++; }
if (/^}/) { $in=0 }
if (/SDPort = (\d+)/ && $in) {if ($nb == 2) { $_ = "  SDPort = 1$1\n"; }}
print;
' $conf/bareos-dir.conf > $tmp/1
cp $tmp/1 $conf/bareos-dir.conf
change_jobname $JobName


disable_plugins


# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list


start_test

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=tape volume=TestVolume001 Pool=Default slot=1 drive=0
label storage=File volume=TestVolume002 Pool=Default
run job=$JobName storage=tape yes
wait
messages
quit
END_OF_DATA


BAREOS_CONFIG_DIR=$BAREOS_SD2_CONF BAREOS_SD_PORT=$BAREOS_SD2_PORT $scripts/bareos-ctl-sd start
run_bareos

echo "ficheriro1.txt" >${BackupDirectory}/ficheriro1.txt
echo "ficheriro2.txt" >${BackupDirectory}/ficheriro2.txt


cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
@# Force Incremental on the second Volume
run level=Incremental storage=File job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
setdebug trace=1 level=110 client
setdebug trace=1 level=110 director
restore where=${cwd}/tmp/bareos-restores select all done yes
wait
messages
quit
END_OF_DATA

run_bconsole

check_for_zombie_jobs storage=File
check_for_zombie_jobs storage=tape
stop_bareos
BAREOS_CONFIG_DIR=$BAREOS_SD2_CONF BAREOS_SD_PORT=$BAREOS_SD2_PORT $scripts/bareos-ctl-sd stop

check_two_logs
check_restore_diff

end_test
