#!/bin/bash

set -e
. "$(dirname "$0")"/vars.sh

# Send notification email using a script on the backup host
# First argument is our hostname, second argument is destination;
# mail body is provided on stdin.

if tty -s ; then
    echo 'Refusing to read mail body from terminal'
    exit 1
fi

SUMMARY="$1"
EMAIL="$2"

# Remote notify.sh wants subject as first line, not as an argument,
# since it's a bit messy to pass complex strings through ssh command
# lines.
( echo "backup $HOSTNAME: $SUMMARY" ; cat ) | \
    ssh \
	-F "$SSH/config" \
	-i "$SSH/id_ecdsa_notify" \
	"$BACKUP_USER@$BACKUP_HOST" \
	borg/notify.sh "$EMAIL"
