#!/bin/sh
#
# Copyright (C) 2000-2026 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of n files then restore those files.
#  Note, you can create one or more files named 1, 2, or aaa
#  that will be backed up first (due to sorting).  This way,
#  you can control exactly what file(s) is being saved and
#  its contents.
# In addition you may comment out or leave in two lines below that run
#  Bacula a second time to test whether Bacula can stop, restart,
#  and then correctly append to the same database.
#
dump_headers() 
{
   echo " "
   od -a -A d tmp/TestVolume001
   echo "======================="
   echo " "
   od -a -A d tmp/TestVolume001.add
   echo "======================="
   echo " "
}
TestName="n-files-test"
JobName=Simple
. scripts/functions

${rscripts}/cleanup
${rscripts}/copy-test-confs
echo "${tmpsrc}" >${tmp}/file-list
mkdir -p ${tmpsrc}
# Copy n files for backup
n=2000
ls ${src}/src/dird/xxx | sort | head -$n  >${tmp}/restore-list
#ls ${src}/src/dird/* | sort | head -$n  >${tmp}/restore-list
for i in `cat ${tmp}/restore-list` ; do
  cp -p $i ${tmpsrc}
  ls -l $i
done
# Get path to temp source files to backup
ls ${tmpsrc}/* >${tmp}/restore-list
cd ${cwd}

#change_jobname CompressedTest $JobName
start_test

cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@$out ${tmp}/log1.out
@#setdebug level=200 storage=File
@#setdebug level=200 client
@#setdebug level=100 director
label storage=File volume=TestVolume001
label storage=File volume=TestVolume002
END_OF_DATA

# Do Volume label
run_bacula

#dump_headers

cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@$out ${tmp}/log1.out
@#setdebug level=200 storage=File
run level=Incremental job=$JobName yes
wait
@#run level=Full job=$JobName yes
status client
wait
sql
select * from JobMedia;

messages
quit
END_OF_DATA

# Do backup(s)
run_bconsole


# Remove the next four commands to run only a single execution
stop_bacula
#dump_headers
#cp ${tmpsrc}/1.c ${tmpsrc}/2.c
#ls ${tmpsrc}/* >${tmp}/restore-list
#touch ${tmpsrc}/1.c
#cat ${src}/src/dird/dird.c >>${src}/src/dird/xxx
run_bacula

#dump_headers

cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@# 
@# now do a restore
@#
@$out ${tmp}/log2.out
setdebug level=50 storage=File
@#setdebug level=100 client
@#restore where=${tmp}/bacula-restores storage=File file=<${tmp}/restore-list yes
restore where=${tmp}/bacula-restores storage=File select all done yes
wait
@#status client
@#status storage=File
messages
quit
END_OF_DATA

# Do restore
run_bconsole
check_for_zombie_jobs storage=File
stop_bacula

#dump_headers

check_two_logs
#
check_restore_tmp_build_diff
end_test
