#!/bin/sh

. scripts/functions

#
# Run a simple backup using the Sparse option
#   then restore it.
#
TestName="sparse-file-test"
JobName=SparseTest

scripts/cleanup
scripts/copy-test-confs

# 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

SPARSEFILE="${BackupDirectory}/gigaslam.gif"

start_test

# create a sparse file
scripts/create_sparse_file.sh ${SPARSEFILE} 100M
if [ $? != 0 ]; then
    set_error "create_sparse_file failed."
    rm -f ${SPARSEFILE}
    exit 1
fi

if [ ! -e "${SPARSEFILE}" ]; then
    set_error "Failed to create sparse file."
    exit 1
fi

cat >${cwd}/tmp/bconcmds <<END_OF_DATA
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName yes
wait
messages
list volumes
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bareos-restores select storage=File
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=File
stop_bareos

check_two_logs
check_restore_diff "${BackupDirectory}"

size_orig=`get_file_size ${SPARSEFILE}`
size=`get_file_size ${cwd}/tmp/bareos-restores/${SPARSEFILE}`
if [ -z "$size" ]; then
    set_error "Failed to determine size of restored sparse file."
elif [ $size -gt 120 ]; then
    set_error "Restored sparse file ${SPARSEFILE} is too big (${size}K > 120K)"
fi

print_debug "size orig:     $size_orig"
print_debug "size restored: $size"

rm -f ${SPARSEFILE}

end_test
