#!/usr/bin/env bash # BackTunnel uninstaller (with optional PURGE=1 to remove shared defaults) set -euo pipefail PREFIX=${PREFIX:-/usr} DESTDIR=${DESTDIR:-} PURGE=${PURGE:-0} # set PURGE=1 to remove /usr/share/backtunnel and /etc/backtunnel BINDIR="$DESTDIR$PREFIX/bin" MANDIR="$DESTDIR$PREFIX/share/man/man1" KIO_SM="$DESTDIR$PREFIX/share/kio/servicemenus" KSVC5="$DESTDIR$PREFIX/share/kservices5/ServiceMenus" APPDIR="$DESTDIR$PREFIX/share/applications" BCOMP="$DESTDIR$PREFIX/share/bash-completion/completions" SHARE_DIR="$DESTDIR$PREFIX/share/backtunnel" ETC_DIR="$DESTDIR/etc/backtunnel" say() { printf '[BackTunnel] %s\n' "$*"; } say_warn() { printf '[BackTunnel] WARN: %s\n' "$*" >&2; } # Portable directory prune: remove dir if empty, then move up until stop boundary prune_dir() { local dir="${1%/}" local stop="${2%/}" while [[ -n "$dir" && "$dir" != "/" && "$dir" != "$stop" ]]; do rmdir "$dir" 2>/dev/null || break dir="$(dirname "$dir")" done } # Friendly notice if uninstalling the live system without root if [[ -z "$DESTDIR" && ${EUID:-$(id -u)} -ne 0 ]]; then say_warn "Running without root; some files may not be removed due to permissions." fi say "Uninstalling from PREFIX=${PREFIX} DESTDIR=${DESTDIR} (PURGE=${PURGE})" # --- Remove binaries --- rm -f "$BINDIR/backtunnel-share" \ "$BINDIR/backtunnel-access" \ "$BINDIR/backtunnel-auth-setup" \ "$BINDIR/backtunnel-share-gui" \ "$BINDIR/backtunnel-access-gui" \ "$BINDIR/backtunnel-open-term" \ "$BINDIR/backtunnel-share-tui" \ "$BINDIR/backtunnel-access-tui" # Optionally remove helper that may be present on some installs rm -f "$BINDIR/backtunnel-init" # --- Man page --- rm -f "$MANDIR/backtunnel.1" # --- Bash completions --- rm -f "$BCOMP/backtunnel-share" \ "$BCOMP/backtunnel-access" # --- Dolphin service menus (Plasma 6 + legacy) --- rm -f "$KIO_SM/backtunnel_share.desktop" \ "$KIO_SM/backtunnel_access.desktop" \ "$KSVC5/backtunnel_share.desktop" \ "$KSVC5/backtunnel_access.desktop" # --- Optional desktop launcher --- rm -f "$APPDIR/backtunnel.desktop" # --- Shared defaults (only if PURGE=1) --- if [[ "$PURGE" = "1" ]]; then say "Purging shared defaults under $SHARE_DIR and $ETC_DIR" rm -f "$SHARE_DIR/profiles.ini" 2>/dev/null || true rm -f "$ETC_DIR/profiles.ini" 2>/dev/null || true # Remove directories if empty (and prune empty parents up to safe boundaries) prune_dir "$SHARE_DIR" "$DESTDIR$PREFIX/share" prune_dir "$ETC_DIR" "$DESTDIR/etc" else # Optionally clean up empty share dir if package manager removed files already prune_dir "$SHARE_DIR" "$DESTDIR$PREFIX/share" say "Keeping shared defaults: $SHARE_DIR/ and $ETC_DIR/ (set PURGE=1 to remove)" fi # --- Refresh desktop/KDE cache (best-effort, skip during packaging) --- if [[ -z "$DESTDIR" ]]; then if command -v update-desktop-database >/dev/null 2>&1; then update-desktop-database -q || true fi if command -v kbuildsycoca6 >/dev/null 2>&1; then kbuildsycoca6 --noincremental >/dev/null 2>&1 || true elif command -v kbuildsycoca5 >/dev/null 2>&1; then kbuildsycoca5 --noincremental >/dev/null 2>&1 || true fi fi say "Uninstall complete."