#!/usr/bin/env bash

set -e
set -o pipefail

case "$1" in
    clean)
        cat <<'EOF'
Backups for guix.wugi.info:
EOF
        sudo --login                                                            \
             RESTIC_PASSWORD="$(pass show wugi.info/restic/all)"                \
             RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/guixsd}"       \
             "$HOME/.guix-profile/bin/restic" --json snapshots                  \
            | jq -c --raw-output '. | reverse.[] | [.time, .tags]'              \
            | cat -n                                                            \
            | tac
        echo sudo --login                                                   \
             'RESTIC_PASSWORD="$(pass show wugi.info/restic/all)"'          \
             'RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/guixsd}"' \
             "$HOME/.guix-profile/bin/restic"                               \
             forget --prune --keep-last N
        cat <<'EOF'

Backups for windows.local:
EOF
        sudo --login                                                            \
             RESTIC_PASSWORD="$(sudo cat /etc/guix/secrets/windows)"            \
             RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/win10}"        \
             "${HOME}/.guix-profile/bin/restic" --json snapshots                \
            | jq -c --raw-output '. | reverse.[] | [.time, .tags]'              \
            | cat -n                                                            \
            | tac                                                               \
            | grep null                                                         \
            | tail -5
        echo sudo --login                                                   \
             'RESTIC_PASSWORD="$(sudo cat /etc/guix/secrets/windows)"'      \
             'RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/win10}"'  \
             "$HOME/.guix-profile/bin/restic"                               \
             forget --prune --keep-last N
        cat <<'EOF'

Backups for ntfsgames:
EOF
        sudo --login                                                            \
             RESTIC_PASSWORD="$(sudo cat /etc/guix/secrets/windows)"            \
             RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/ntfsgames}"    \
             "${HOME}/.guix-profile/bin/restic" --json snapshots                \
            | jq -c --raw-output '. | reverse.[] | [.time, .tags]'              \
            | cat -n                                                            \
            | tac                                                               \
            | grep null                                                         \
            | tail -5
        echo sudo --login                                                       \
             'RESTIC_PASSWORD="$(sudo cat /etc/guix/secrets/windows)"'          \
             'RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/ntfsgames}"'  \
             "$HOME/.guix-profile/bin/restic"                                   \
             forget --prune --keep-last N
        ;;
    *)
        sudo --login                                                        \
             RESTIC_PASSWORD="$(pass show wugi.info/restic/all)"            \
             RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/guixsd}"   \
             "$HOME/.guix-profile/bin/restic" "$@"
        # shellcheck disable=SC2016
        cat <<'EOF'

Restore "${HOME}/.mozilla" backup example:
    sudo --login RESTIC_PASSWORD="$(pass show wugi.info/restic/all)" RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/guixsd}" "${HOME}/.guix-profile/bin/restic" restore latest --include "${HOME}/.mozilla" --target /

List Windows snapshots:
    sudo --login RESTIC_PASSWORD="$(sudo cat /etc/guix/secrets/windows)" RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/win10}" "${HOME}/.guix-profile/bin/restic" snapshots

Restore Windows snapshot:
    RESTIC_PASSWORD="$(sudo cat /etc/guix/secrets/windows)" RESTIC_REPOSITORY="${RESTIC_REPOSITORY:-/srv/backup/win10}" "${HOME}/.guix-profile/bin/restic" dump latest win10.img | dd of=/dev/lvm1/win10 bs=4M
EOF
        ;;
esac
