#! /usr/bin/env bash
# Copyright © 2011, 2012 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

## Check that min-backups is honored correctly

echo "| Create backup"
RSBACKUP_TODAY=1980-01-01 s ${RSBACKUP} --backup --text got/1.txt --html got/1.html
compare volume1 store1/host1/volume1/1980-01-01
compare volume2 store1/host1/volume2/1980-01-01
compare ${srcdir:-.}/expect/1.txt got/1.txt
compare ${srcdir:-.}/expect/1.html got/1.html

echo "| Null prune"
RUN=null RSBACKUP_TODAY=1980-02-01 s ${RSBACKUP} --prune --text got/2.txt --html got/2.html
absent null-pre.ran
absent null-pos.ran
absent null-dev-pre.ran
absent null-dev-post.ran
absent null-dev-pre.acted
absent null-dev-post.acted
compare volume1 store1/host1/volume1/1980-01-01
compare volume2 store1/host1/volume2/1980-01-01
compare ${srcdir:-.}/expect/2.txt got/2.txt
compare ${srcdir:-.}/expect/2.html got/2.html

echo "| Create second backup"
RSBACKUP_TODAY=1980-01-02 s ${RSBACKUP} --backup --text got/20.txt --html got/20.html
# volume1: 1980-01-01 1980-01-02
# volume2: 1980-01-01 1980-01-02
compare volume1 store1/host1/volume1/1980-01-02
compare volume2 store1/host1/volume2/1980-01-02
compare ${srcdir:-.}/expect/20.txt got/20.txt
compare ${srcdir:-.}/expect/20.html got/20.html

echo "| --dry-run should do nothing"
RUN=dryrun RSBACKUP_TODAY=1980-02-01 s ${RSBACKUP} --prune --dry-run --text got/3.txt --html got/3.html
absent dryrun-pre.ran
absent dryrun-post.ran
exists dryrun-dev-pre.ran
exists dryrun-dev-post.ran
absent dryrun-dev-pre.acted
absent dryrun-dev-post.acted
compare volume1 store1/host1/volume1/1980-01-01
compare volume2 store1/host1/volume2/1980-01-01
compare volume1 store1/host1/volume1/1980-01-02
compare volume2 store1/host1/volume2/1980-01-02
compare ${srcdir:-.}/expect/3.txt got/3.txt
compare ${srcdir:-.}/expect/3.html got/3.html

echo "| Prune affecting volume1"
RUN=prune1 RSBACKUP_TODAY=1980-02-01 s ${RSBACKUP} --prune --text got/4.txt --html got/4.html
absent prune1-pre.ran
absent prune1-post.ran
exists prune1-dev-pre.ran
exists prune1-dev-post.ran
exists prune1-dev-pre.acted
exists prune1-dev-post.acted
absent store1/host1/volume1/1980-01-01
compare volume2 store1/host1/volume2/1980-01-01
compare volume1 store1/host1/volume1/1980-01-02
compare volume2 store1/host1/volume2/1980-01-02
sed < got/4.txt > got/4sed.txt "s,${PWD},<SRCDIR>,g"
sed < got/4.html > got/4sed.html "s,${PWD},<SRCDIR>,g"
compare ${srcdir:-.}/expect/4.txt got/4sed.txt
compare ${srcdir:-.}/expect/4.html got/4sed.html
# volume1:            1980-01-02
# volume2: 1980-01-01 1980-01-02

## Check that prune-age is honored correctly

echo "| Create third backup"
RSBACKUP_TODAY=1980-01-03 s ${RSBACKUP} --backup --text got/5.txt --html got/5.html
# volume1:            1980-01-02 1980-01-03
# volume2: 1980-01-01 1980-01-02 1980-01-03
RSBACKUP_TODAY=1980-01-03 s ${RSBACKUP} --prune
absent store1/host1/volume1/1980-01-01
compare volume2 store1/host1/volume2/1980-01-01
compare volume1 store1/host1/volume1/1980-01-02
compare volume2 store1/host1/volume2/1980-01-02
compare volume1 store1/host1/volume1/1980-01-03
compare volume2 store1/host1/volume2/1980-01-03
sed < got/5.txt > got/5sed.txt "s,${PWD},<SRCDIR>,g"
sed < got/5.html > got/5sed.html "s,${PWD},<SRCDIR>,g"
compare ${srcdir:-.}/expect/5.txt got/5sed.txt
compare ${srcdir:-.}/expect/5.html got/5sed.html

echo "| Prune affecting unselected volume"
RUN=prune2 RSBACKUP_TODAY=1980-01-04 s ${RSBACKUP} --prune --text got/6.txt --html got/6.html host1:volume1
absent store1/host1/volume1/1980-01-01
absent prune2-dev-pre.ran
absent prune2-dev-post.ran
absent prune2-dev-pre.acted
absent prune2-dev-post.acted
compare volume2 store1/host1/volume2/1980-01-01
compare volume1 store1/host1/volume1/1980-01-02
compare volume2 store1/host1/volume2/1980-01-02
compare volume1 store1/host1/volume1/1980-01-03
compare volume2 store1/host1/volume2/1980-01-03
sed < got/6.txt > got/6sed.txt "s,${PWD},<SRCDIR>,g"
sed < got/6.html > got/6sed.html "s,${PWD},<SRCDIR>,g"
compare ${srcdir:-.}/expect/6.txt got/6sed.txt
compare ${srcdir:-.}/expect/6.html got/6sed.html

echo "| Prune affecting volume2"
RUN=prune3 RSBACKUP_TODAY=1980-01-04 s ${RSBACKUP} --prune --text got/7.txt --html got/7.html host1:volume2
absent store1/host1/volume1/1980-01-01
absent store1/host1/volume2/1980-01-01
exists prune3-dev-pre.ran
exists prune3-dev-post.ran
exists prune3-dev-pre.acted
exists prune3-dev-post.acted
compare volume1 store1/host1/volume1/1980-01-02
compare volume2 store1/host1/volume2/1980-01-02
compare volume1 store1/host1/volume1/1980-01-03
compare volume2 store1/host1/volume2/1980-01-03
sed < got/7.txt > got/7sed.txt "s,${PWD},<SRCDIR>,g"
sed < got/7.html > got/7sed.html "s,${PWD},<SRCDIR>,g"
compare ${srcdir:-.}/expect/7.txt got/7sed.txt
compare ${srcdir:-.}/expect/7.html got/7sed.html

cleanup
