#!/usr/bin/env bash
# set -euo pipefail

mkdir -p ~/.restore-info/tmp
chmod 700 ~/.restore-info

# restic-do exec init
# restic-do exec mount /path/to-restorepoint (mkdir first)

export AWS_ACCESS_KEY_ID=$(pash show personal-backups/aws-key-id)
export AWS_SECRET_ACCESS_KEY=$(pash show personal-backups/aws-access-key)

repo=$(cat ~/.dotfiles/.restic/.repo-name)
script_started=$(date +%Y-%m-%d_%H%M%S)

# $RESTIC_PASSWORD_FILE or $RESTIC_PASSWORD_COMMAND
if [[ -f "$HOME/.dotfiles/.restic/.repo-pass" ]]; then
	export RESTIC_PASSWORD_FILE="$HOME/.dotfiles/.restic/.repo-pass"
else
	export RESTIC_PASSWORD_COMMAND="pash show personal-backups/repo-pass"
fi

# set email vars here
[ -f ~/.dotfiles/.restic/.env ] && source ~/.dotfiles/.restic/.env

# Check internet connectivity
echo "checking internet connectivity, pinging google.com"
ping -c 1 google.com
internet_check="$?"
if [[ $internet_check != 0 ]]; then
	echo "You may not be connected to the internet"
	notify-send -u critical "The backup was unable to run:" "you are not connected to the internet"
	zenity --error --text "The backup was unable to run: you are not connected to the internet"
	exit 1
fi

# Check if metered
# https://github.com/erikw/restic-systemd-automatic-backup
# http://opensource.org/licenses/BSD-3-Clause
systemctl is-active dbus.service >/dev/null 2>&1 || exit 0
systemctl is-active NetworkManager.service >/dev/null 2>&1 || exit 0

metered_status=$(dbus-send --system --print-reply=literal \
	--system --dest=org.freedesktop.NetworkManager \
	/org/freedesktop/NetworkManager \
	org.freedesktop.DBus.Properties.Get \
	string:org.freedesktop.NetworkManager string:Metered \
	| grep -o ".$")

if [[ $metered_status =~ (1|3) ]]; then
	echo Current connection is metered
	notify-send -u critical "The backup was unable to run:" "your internet connection is metered"
	zenity  --error --text "The backup was unable to run: your internet connection is metered"
	exit 1
fi

case "$1" in
	backup)
		cd ~ || exit 1

		# No longer echo "exporting list of packages"
        # clear existing: rm ~/.restore-info/packages-*
		
		# No longer echo "exporting dnf repos list"
		# clear existing rm ~/.restore-info/dnfrepos-*

		echo "starting backup"
		restic backup -r "$repo" \
			--one-file-system \
			--exclude-caches --exclude-file ~/.dotfiles/.restic/ignore.txt \
			--exclude-if-present .exclude_from_backup \
			~ -v > ~/.restore-info/tmp/backup-log-${script_started}.txt

		restic_exit_code="$?"
		backup_stopped=$(date +%k%M%S)

		echo "sending email report"
		cat <<EOMAIL | msmtp --read-envelope-from $BACKUP_EMAIL_TO
To: $BACKUP_EMAIL_TO
From: $BACKUP_EMAIL_FROM
Subject: Backup report $script_started exiting $restic_exit_code

Stopped $backup_stopped

$(cat ~/.restore-info/tmp/backup-log-${script_started}.txt)
EOMAIL
	;;
	maintenance)
		restic forget -r "$repo" -v --prune \
			--keep-last 2 --keep-daily 7 --keep-weekly 5 --keep-monthly 6 --keep-yearly 3 \
			> ~/.restore-info/tmp/maint-forget-log-${script_started}.txt
		forget_exit_code="$?"

		restic -r "$repo" check --read-data-subset=10% \
			> ~/.restore-info/tmp/maint-check-log-${script_started}.txt
		check_exit_code="$?"

		maint_done=$(date +%k%M%S)

		echo "sending email report"
		cat <<EOMAIL | msmtp --read-envelope-from $BACKUP_EMAIL_TO
To: $BACKUP_EMAIL_TO
From: $BACKUP_EMAIL_FROM
Subject: Backup maintenance report: $script_started

- Finished $maint_done
- Forgetting exited $forget_exit_code
- Checking exiting $check_exit_code

$(cat ~/.restore-info/tmp/maint-forget-log-${script_started}.txt)

$(cat ~/.restore-info/tmp/maint-check-log-${script_started}.txt)
EOMAIL
		;;
	exec)
		shift
		restic -r "$repo" "$@" ;;
esac