#! /bin/bash
# Copyright © 2018 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 "| Failing hook"
set +e
STDERR=${WORKSPACE}/got/overridden-stderr.txt STDOUT=${WORKSPACE}/got/overridden-stdout.txt PRE_BACKUP_HOOK_STATUS=1 PRE_BACKUP_HOOK_STDERR=failhook-stderr RUN=failhook RSBACKUP_TIME=315532800 s ${RSBACKUP} --backup  host1:volume1
status=$?
set -e
# If the hook fails then rsbackup should fail too
if [ $status != 1 ]; then
  echo >&2 "FAILED: rsbackup run with failing hook exited with status $status"
  exit 1
fi
absent ${WORKSPACE}/failhook-dev-pre.ran
absent ${WORKSPACE}/failhook-dev-post.ran
exists ${WORKSPACE}/failhook-pre.acted
absent ${WORKSPACE}/failhook-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}/store1/host1/volume3
absent ${WORKSPACE}/store2/host1/volume2
absent ${WORKSPACE}/store2/host1/volume3
compare ${srcdir:-.}/expect/issue43/failhookerr.txt ${WORKSPACE}/got/overridden-stderr.txt
compare ${srcdir:-.}/expect/issue43/failhookout.txt ${WORKSPACE}/got/overridden-stdout.txt
rm -f ${WORKSPACE}/hookdata

echo "| Temporarily failed hook"
STDERR=${WORKSPACE}/got/overridden-stderr.txt STDOUT=${WORKSPACE}/got/overridden-stdout.txt PRE_BACKUP_HOOK_STATUS=75 PRE_BACKUP_HOOK_STDERR=tempfailhook-stderr RUN=tempfailhook RSBACKUP_TIME=315532800 s ${RSBACKUP} --backup host1:volume1
absent ${WORKSPACE}/tempfailhook-dev-pre.ran
absent ${WORKSPACE}/tempfailhook-dev-post.ran
exists ${WORKSPACE}/tempfailhook-pre.acted
absent ${WORKSPACE}/tempfailhook-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}/store1/host1/volume3
absent ${WORKSPACE}/store2/host1/volume2
absent ${WORKSPACE}/store2/host1/volume3
compare ${srcdir:-.}/expect/issue43/tempfailhook.txt ${WORKSPACE}/got/overridden-stderr.txt
compare ${srcdir:-.}/expect/issue43/failhookout.txt ${WORKSPACE}/got/overridden-stdout.txt
