# remove-autorestic — fully decommission autorestic on a host.
#
# Why: autorestic was the original ESH home-lab backup tool (manual install
# at /usr/local/bin/autorestic, daily + monthly systemd timers). The new
# two-layer pipeline (PBS for VM images via the hypervisor, structured
# restic profiles via configs/restic/<host>/profiles.yaml) covers
# everything autorestic was doing, so it's redundant — and the typo
# `D:escription` in autorestic-backup.timer caught during the
# traefik-postboot install on esh-docker-vm prompted ripping it out
# rather than fixing it.
#
# Hosts in scope (autorestic detected 2026-04-26):
#   esh-docker-vm, esh-vm-db
# Skips on hosts without it (every step is `creates:`-/`when:`-gated).
#
# What this DOES remove:
#   - /etc/systemd/system/autorestic-{backup,prune}.{service,timer}
#   - /usr/local/bin/autorestic
#
# What this DOES NOT touch (decide separately):
#   - /srv/backups/autorestic/.autorestic.yml — the config; archival
#     value (records what was being backed up + the restic key)
#   - /mnt/backup/restic/repo/esh — historical autorestic snapshots
#     (real backup data; user can prune with `restic forget` once they're
#     comfortable the new pipeline has equivalent coverage)
#
# Usage:
#   scripts/elway esh-docker-vm --playbook playbooks/remove-autorestic.yaml
#   scripts/elway esh-vm-db --playbook playbooks/remove-autorestic.yaml

steps:
  - name: Stop + disable autorestic-backup.timer
    shell: systemctl disable --now autorestic-backup.timer
    sudo: true
    when: "systemctl list-unit-files autorestic-backup.timer --no-legend 2>/dev/null | grep -q ."

  - name: Stop + disable autorestic-prune.timer
    shell: systemctl disable --now autorestic-prune.timer
    sudo: true
    when: "systemctl list-unit-files autorestic-prune.timer --no-legend 2>/dev/null | grep -q ."

  - name: Stop autorestic-backup.service (if running)
    # Should be a no-op after disable --now above, but a long-running
    # backup invocation triggered manually could still be live. Safe
    # to call on a stopped unit.
    shell: systemctl stop autorestic-backup.service 2>/dev/null || true
    sudo: true

  - name: Stop autorestic-prune.service (if running)
    shell: systemctl stop autorestic-prune.service 2>/dev/null || true
    sudo: true

  - name: Remove autorestic systemd unit files
    # Glob, not a list of explicit paths. A previous version used
    # `rm -f path1 \` + newline + `path2`, which gets bitten by YAML
    # plain-scalar folding: the backslash + newline collapses to a
    # literal `\ ` and only the first path gets removed. The glob is
    # also idempotent on hosts where the files are already gone.
    shell: rm -f /etc/systemd/system/autorestic-*.service /etc/systemd/system/autorestic-*.timer
    sudo: true

  - name: systemctl daemon-reload (drop the removed units from systemd's view)
    shell: systemctl daemon-reload
    sudo: true

  - name: systemctl reset-failed (clear any leftover failed-state for autorestic-*)
    shell: systemctl reset-failed 'autorestic-*' 2>/dev/null || true
    sudo: true

  - name: Remove the autorestic binary
    shell: rm -f /usr/local/bin/autorestic
    sudo: true
    removes: /usr/local/bin/autorestic

verify:
  - name: No autorestic systemd units remain
    shell: |
      n=$(ls /etc/systemd/system/autorestic-* 2>/dev/null | wc -l)
      [ "$n" = "0" ]
    changed_when: "false"

  - name: No autorestic binary on PATH
    # Quoted because YAML treats a leading `!` as a tag indicator —
    # unquoted, the parser strips it and the verify ends up running
    # `command -v autorestic` (exits non-zero when autorestic is gone,
    # which is the OPPOSITE of what we want here).
    shell: "! command -v autorestic >/dev/null"
    changed_when: "false"

  - name: systemd no longer knows about autorestic units
    shell: |
      n=$(systemctl list-unit-files 'autorestic-*' --no-legend 2>/dev/null | wc -l)
      [ "$n" = "0" ]
    changed_when: "false"
