scripts/sepolia/systemd/uninstall.sh38 lines · 2138 bytes
| 1 | #!/usr/bin/env bash |
| 2 | # Stop, disable and remove the Upgrade Airlock user units installed by install.sh (current user only, no sudo). |
| 3 | # |
| 4 | # scripts/sepolia/systemd/uninstall.sh # keeps the env file and the state dir (logs) |
| 5 | # scripts/sepolia/systemd/uninstall.sh --purge # also removes the env file; the state dir is only printed |
| 6 | # DRY_RUN=1 scripts/sepolia/systemd/uninstall.sh # print what it would do |
| 7 | set -euo pipefail |
| 8 | UNITS=(airlock-listener.service airlock-sweep.timer airlock-sweep.service) |
| 9 | UNIT_DIR=${SYSTEMD_USER_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user} |
| 10 | ENV_FILE=${ENV_FILE:-${XDG_CONFIG_HOME:-$HOME/.config}/upgrade-airlock/airlock.env} |
| 11 | PURGE= |
| 12 | for a in "$@"; do |
| 13 | case "$a" in |
| 14 | --purge) PURGE=1 ;; |
| 15 | -h | --help) sed -n '2,6p' "$0"; exit 0 ;; |
| 16 | *) echo "error: unknown option $a" >&2; exit 2 ;; |
| 17 | esac |
| 18 | done |
| 19 | run() { if [ -n "${DRY_RUN:-}" ]; then echo "+ $*"; else "$@"; fi; } |
| 20 | [ "$(id -u)" != 0 ] || { echo "error: run this as the user that installed the units, not as root" >&2; exit 1; } |
| 21 | |
| 22 | STATE= |
| 23 | if [ -f "$ENV_FILE" ]; then STATE=$(sed -n 's/^AIRLOCK_STATE=//p' "$ENV_FILE" | tail -n1); fi |
| 24 | if command -v systemctl >/dev/null && { [ -n "${DRY_RUN:-}" ] || systemctl --user show-environment >/dev/null 2>&1; }; then |
| 25 | run systemctl --user disable --now airlock-listener.service airlock-sweep.timer || true |
| 26 | run systemctl --user stop airlock-sweep.service || true |
| 27 | fi |
| 28 | for u in "${UNITS[@]}"; do |
| 29 | if [ -f "$UNIT_DIR/$u" ]; then run rm -f "$UNIT_DIR/$u"; [ -n "${DRY_RUN:-}" ] || echo "removed $UNIT_DIR/$u"; fi |
| 30 | done |
| 31 | if command -v systemctl >/dev/null && { [ -n "${DRY_RUN:-}" ] || systemctl --user show-environment >/dev/null 2>&1; }; then |
| 32 | run systemctl --user daemon-reload |
| 33 | run systemctl --user reset-failed airlock-listener.service airlock-sweep.service 2>/dev/null || true |
| 34 | fi |
| 35 | if [ -n "$PURGE" ] && [ -f "$ENV_FILE" ]; then run rm -f "$ENV_FILE"; [ -n "${DRY_RUN:-}" ] || echo "removed $ENV_FILE"; fi |
| 36 | [ -z "$PURGE" ] && [ -f "$ENV_FILE" ] && echo "kept $ENV_FILE (--purge removes it)" |
| 37 | [ -n "$STATE" ] && echo "kept the state dir $STATE (logs; delete it yourself when no longer needed)" |
| 38 | exit 0 |