#! /usr/bin/env bash
# Copyright © Richard Kettlewell.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
set -e
. ${srcdir:-.}/setup.sh

setup

echo "| --dry-run should do nothing"
RUN=dryrun RSBACKUP_TIME=315532800 s ${RSBACKUP} --dry-run --backup --text ${WORKSPACE}/got/dryrun.txt --html ${WORKSPACE}/got/dryrun.html host1:volume1
exists ${WORKSPACE}/dryrun-dev-pre.ran
exists ${WORKSPACE}/dryrun-dev-post.ran
absent ${WORKSPACE}/dryrun-dev-pre.acted
absent ${WORKSPACE}/dryrun-dev-post.acted
exists ${WORKSPACE}/dryrun-pre.ran
exists ${WORKSPACE}/dryrun-post.ran
absent ${WORKSPACE}/dryrun-pre.acted
absent ${WORKSPACE}/dryrun-post.acted
absent ${WORKSPACE}/store1/host1/volume1/1980-01-01T00:00:00
absent ${WORKSPACE}/store1/host1/volume1/1980-01-01T00:00:00.incomplete
absent ${WORKSPACE}/store1/host1/volume2
absent ${WORKSPACE}/store2/host1
compare ${srcdir:-.}/expect/backup/dryrun.txt ${WORKSPACE}/got/dryrun.txt
compare ${srcdir:-.}/expect/backup/dryrun.html ${WORKSPACE}/got/dryrun.html
absent ${WORKSPACE}/logs/backups.db # not created with --dry-run

echo "| --latest before any backups made should fail"
fails ${RSBACKUP} --latest host1:volume1

echo "| Create backup for one volume"
RUN=volume1 RSBACKUP_TIME=315532800 s ${RSBACKUP} --backup --text ${WORKSPACE}/got/onevolume.txt --html ${WORKSPACE}/got/onevolume.html host1:volume1
exists ${WORKSPACE}/volume1-dev-pre.ran
exists ${WORKSPACE}/volume1-dev-post.ran
exists ${WORKSPACE}/volume1-dev-pre.ran
exists ${WORKSPACE}/volume1-dev-post.ran
exists ${WORKSPACE}/volume1-pre.acted
exists ${WORKSPACE}/volume1-post.acted
exists ${WORKSPACE}/volume1-pre.acted
exists ${WORKSPACE}/volume1-post.acted
compare ${WORKSPACE}/volume1 ${WORKSPACE}/store1/host1/volume1/1980-01-01T00:00:00
absent ${WORKSPACE}/store1/host1/volume1/1980-01-01T00:00:00.incomplete
absent ${WORKSPACE}/store1/host1/volume2
absent ${WORKSPACE}/store1/host1/volume3
absent ${WORKSPACE}/store2/host1/volume2
absent ${WORKSPACE}/store2/host1/volume3
compare ${srcdir:-.}/expect/backup/onevolume.txt ${WORKSPACE}/got/onevolume.txt
compare ${srcdir:-.}/expect/backup/onevolume.html ${WORKSPACE}/got/onevolume.html
exists ${WORKSPACE}/logs/backups.db # default database path

echo "| --latest for one backup of one volume"
cat > ${WORKSPACE}/expect-latest1.txt << EOF
${WORKSPACE}/store2/host1/volume1/1980-01-01T00:00:00
EOF
s ${RSBACKUP} --latest host1:volume1 > ${WORKSPACE}/got/latest1.txt
compare ${WORKSPACE}/expect-latest1.txt ${WORKSPACE}/got/latest1.txt

echo "| Create backup for one host"
RUN=host1 RSBACKUP_TIME=315619200 s ${RSBACKUP} --backup --text ${WORKSPACE}/got/onehost.txt --html ${WORKSPACE}/got/onehost.html host1
exists ${WORKSPACE}/host1-pre.acted
exists ${WORKSPACE}/host1-post.acted
compare ${WORKSPACE}/volume1 ${WORKSPACE}/store1/host1/volume1/1980-01-02T00:00:00
absent ${WORKSPACE}/store1/host1/volume1/1980-01-02T00:00:00.incomplete
compare ${WORKSPACE}/volume2 ${WORKSPACE}/store1/host1/volume2/1980-01-02T00:00:00
absent ${WORKSPACE}/store1/host1/volume2/1980-01-02T00:00:00.incomplete
absent ${WORKSPACE}/store1/host1/volume3
compare ${WORKSPACE}/volume3 ${WORKSPACE}/store2/host1/volume3/1980-01-02T00:00:00
absent ${WORKSPACE}/store2/host1/volume3/1980-01-02T00:00:00.incomplete
compare ${srcdir:-.}/expect/backup/onehost.txt ${WORKSPACE}/got/onehost.txt
compare ${srcdir:-.}/expect/backup/onehost.html ${WORKSPACE}/got/onehost.html

echo "| --latest for one backup of both volumes"
cat > ${WORKSPACE}/expect-latest2.txt <<EOF
${WORKSPACE}/store2/host1/volume1/1980-01-02T00:00:00
${WORKSPACE}/store2/host1/volume2/1980-01-02T00:00:00
${WORKSPACE}/store2/host1/volume3/1980-01-02T00:00:00
EOF
s ${RSBACKUP} --latest host1:volume1 host1:volume2 host1:volume3 > ${WORKSPACE}/got/latest2.txt
compare ${WORKSPACE}/expect-latest2.txt ${WORKSPACE}/got/latest2.txt

echo "| Create backup for everything"
RUN=all RSBACKUP_TIME=315705600 s ${RSBACKUP} --backup --text ${WORKSPACE}/got/everything.txt --html ${WORKSPACE}/got/everything.html
exists ${WORKSPACE}/all-pre.acted
exists ${WORKSPACE}/all-post.acted
compare ${WORKSPACE}/volume1 ${WORKSPACE}/store1/host1/volume1/1980-01-03T00:00:00
absent ${WORKSPACE}/store1/host1/volume1/1980-01-03T00:00:00.incomplete
compare ${WORKSPACE}/volume2 ${WORKSPACE}/store1/host1/volume2/1980-01-03T00:00:00
absent ${WORKSPACE}/store1/host1/volume2/1980-01-03T00:00:00.incomplete
absent ${WORKSPACE}/store1/host1/volume3
compare ${WORKSPACE}/volume3 ${WORKSPACE}/store2/host1/volume3/1980-01-03T00:00:00
absent ${WORKSPACE}/store2/host1/volume3/1980-01-03T00:00:00.incomplete
compare ${srcdir:-.}/expect/backup/everything.txt ${WORKSPACE}/got/everything.txt
compare ${srcdir:-.}/expect/backup/everything.html ${WORKSPACE}/got/everything.html

echo "| Backup should be clean"
s ${RSBACKUP} --check-unexpected > ${WORKSPACE}/got/check-clean.txt
touch ${WORKSPACE}/expect-clean.txt
compare ${WORKSPACE}/expect-clean.txt ${WORKSPACE}/got/check-clean.txt

echo "| Dirty backups should be detected"
touch ${WORKSPACE}/store1/bogus
touch ${WORKSPACE}/store1/host1/bogus
touch ${WORKSPACE}/store1/host1/volume1/bogus
s ${RSBACKUP} --check-unexpected > ${WORKSPACE}/got/check-dirty.txt
cat > ${WORKSPACE}/expect-dirty.txt <<EOF
${WORKSPACE}/store1/bogus
${WORKSPACE}/store1/host1/bogus
${WORKSPACE}/store1/host1/volume1/bogus
EOF
compare ${WORKSPACE}/expect-dirty.txt ${WORKSPACE}/got/check-dirty.txt

echo "| Combine --check-unexpected and -0"
s ${RSBACKUP} -0 --check-unexpected > ${WORKSPACE}/got/check-dirty.txt
echo -ne "${WORKSPACE}/store1/bogus\\0${WORKSPACE}/store1/host1/bogus\\0${WORKSPACE}/store1/host1/volume1/bogus\\0" > ${WORKSPACE}/expect-dirty.txt
compare ${WORKSPACE}/expect-dirty.txt ${WORKSPACE}/got/check-dirty.txt

cleanup
