#! /usr/bin/env bash
# Copyright © 2011, 2012, 2014 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 "| Create backup"
RSBACKUP_TODAY=1980-01-01 s ${RSBACKUP} --backup --text ${WORKSPACE}/got/11.txt --html ${WORKSPACE}/got/11.html
# volume1: 1980-01-01
# volume2: 1980-01-01
exists ${WORKSPACE}/logs/1980-01-01-device1-host1-volume1.log
exists ${WORKSPACE}/logs/1980-01-01-device1-host1-volume2.log
exists ${WORKSPACE}/logs/1980-01-01-device2-host1-volume1.log
exists ${WORKSPACE}/logs/1980-01-01-device2-host1-volume2.log
exists ${WORKSPACE}/store1/host1/volume1/1980-01-01
exists ${WORKSPACE}/store1/host1/volume2/1980-01-01
exists ${WORKSPACE}/store2/host1/volume1/1980-01-01
exists ${WORKSPACE}/store2/host1/volume1/1980-01-01
compare ${srcdir:-.}/expect/11.txt ${WORKSPACE}/got/11.txt
compare ${srcdir:-.}/expect/11.html ${WORKSPACE}/got/11.html

echo "| Edit config"
sed < ${WORKSPACE}/config > ${WORKSPACE}/config.new 's/^ *volume volume2.*//;s/^ *min-backups 2//'
mv ${WORKSPACE}/config.new ${WORKSPACE}/config

echo "| --dry-run should do nothing"
RSBACKUP_TODAY=1980-01-01 RUN=dryrun s ${RSBACKUP} --retire --dry-run --text ${WORKSPACE}/got/12.txt --html ${WORKSPACE}/got/12.html host1:volume2
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}/logs/1980-01-01-device1-host1-volume1.log
exists ${WORKSPACE}/logs/1980-01-01-device1-host1-volume2.log
exists ${WORKSPACE}/logs/1980-01-01-device2-host1-volume1.log
exists ${WORKSPACE}/logs/1980-01-01-device2-host1-volume2.log
exists ${WORKSPACE}/store1/host1/volume1/1980-01-01
exists ${WORKSPACE}/store1/host1/volume2/1980-01-01
exists ${WORKSPACE}/store2/host1/volume1/1980-01-01
exists ${WORKSPACE}/store2/host1/volume1/1980-01-01
compare ${srcdir:-.}/expect/12.txt ${WORKSPACE}/got/12.txt
compare ${srcdir:-.}/expect/12.html ${WORKSPACE}/got/12.html

echo "| retire volume2"
RSBACKUP_TODAY=1980-01-01 RUN=retire s ${RSBACKUP} --verbose --retire --text ${WORKSPACE}/got/13.txt --html ${WORKSPACE}/got/13.html host1:volume2
exists ${WORKSPACE}/retire-dev-pre.ran
exists ${WORKSPACE}/retire-dev-post.ran
exists ${WORKSPACE}/retire-dev-pre.acted
exists ${WORKSPACE}/retire-dev-post.acted
exists ${WORKSPACE}/logs/1980-01-01-device1-host1-volume1.log
absent ${WORKSPACE}/logs/1980-01-01-device1-host1-volume2.log
exists ${WORKSPACE}/logs/1980-01-01-device2-host1-volume1.log
absent ${WORKSPACE}/logs/1980-01-01-device2-host1-volume2.log
exists ${WORKSPACE}/store1/host1/volume1/1980-01-01
absent ${WORKSPACE}/store1/host1/volume2
exists ${WORKSPACE}/store2/host1/volume1/1980-01-01
absent ${WORKSPACE}/store2/host1/volume2
compare ${srcdir:-.}/expect/13.txt ${WORKSPACE}/got/13.txt
compare ${srcdir:-.}/expect/13.html ${WORKSPACE}/got/13.html

echo "| No negative selections for retire"
if RSBACKUP_TODAY=1980-01-01 RUN=retire s ${RSBACKUP} --verbose --retire --force host1 -host1:volume3 2>${WORKSPACE}/errors; then
  echo "Unexpected success"
  exit 1
fi

echo "| Cannot retire all hosts"
if RSBACKUP_TODAY=1980-01-01 RUN=retire s ${RSBACKUP} --verbose --retire --force '*' 2>${WORKSPACE}/errors; then
  echo "Unexpected success"
  exit 1
fi

echo "| Edit config"
sed < ${WORKSPACE}/config > ${WORKSPACE}/config.new 's/^ *volume volume2.*//;s/^ *min-backups 2//'
mv ${WORKSPACE}/config.new ${WORKSPACE}/config

echo "| Retire all volumes"
RSBACKUP_TODAY=1980-01-01 RUN=retire s ${RSBACKUP} --verbose --retire --force --text ${WORKSPACE}/got/22.txt --html ${WORKSPACE}/got/22.html host1
exists ${WORKSPACE}/retire-dev-pre.ran
exists ${WORKSPACE}/retire-dev-post.ran
exists ${WORKSPACE}/retire-dev-pre.acted
exists ${WORKSPACE}/retire-dev-post.acted
absent ${WORKSPACE}/logs/1980-01-01-device1-host1-volume1.log
absent ${WORKSPACE}/logs/1980-01-01-device1-host1-volume2.log
absent ${WORKSPACE}/logs/1980-01-01-device2-host1-volume1.log
absent ${WORKSPACE}/logs/1980-01-01-device2-host1-volume2.log
absent ${WORKSPACE}/store1/host1/volume1
absent ${WORKSPACE}/store1/host1/volume2
absent ${WORKSPACE}/store2/host1/volume1
absent ${WORKSPACE}/store2/host1/volume2
compare ${srcdir:-.}/expect/22.txt ${WORKSPACE}/got/22.txt
compare ${srcdir:-.}/expect/22.html ${WORKSPACE}/got/22.html

cleanup
