Improve script robustness and portability for logging, terminal execution, and uninstallation workflows.

This commit is contained in:
2025-09-20 08:53:18 +02:00
parent 608a6a371f
commit c46a1da405
2 changed files with 68 additions and 15 deletions

View File

@@ -16,6 +16,22 @@ 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})"
@@ -27,6 +43,8 @@ rm -f "$BINDIR/backtunnel-share" \
"$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"
@@ -42,28 +60,32 @@ rm -f "$KIO_SM/backtunnel_share.desktop" \
"$KSVC5/backtunnel_access.desktop"
# --- Optional desktop launcher ---
rm -f "$APPDIR/backtunnel.desktop" || true
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)
rmdir -p --ignore-fail-on-non-empty "$SHARE_DIR" 2>/dev/null || true
rmdir -p --ignore-fail-on-non-empty "$ETC_DIR" 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
rmdir -p --ignore-fail-on-non-empty "$SHARE_DIR" 2>/dev/null || true
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) ---
command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database -q || true
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
# --- 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."