#!/bin/bash set -e INSTALL_PREFIX="${INSTALL_PREFIX:-/usr}" BIN_DIR="$INSTALL_PREFIX/bin" COMPLETION_DIR="$INSTALL_PREFIX/share/zsh/site-functions" # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- info() { echo "[uninstall] $*"; } success() { echo "[uninstall] $*"; } warn() { echo "[uninstall] WARNING: $*" >&2; } die() { echo "[uninstall] ERROR: $*" >&2; exit 1; } require_root() { if [[ $EUID -ne 0 ]]; then die "This script must be run as root (use sudo)." fi } detect_init() { if command -v systemctl &>/dev/null && systemctl --version &>/dev/null 2>&1; then echo "systemd" elif [[ -f /sbin/openrc-run ]] || [[ -f /usr/sbin/openrc-run ]] || command -v rc-service &>/dev/null; then echo "openrc" else echo "none" fi } # --------------------------------------------------------------------------- # Service removal # --------------------------------------------------------------------------- remove_systemd() { local service_file="$HOME/.config/systemd/user/hush.service" info "Stopping and disabling systemd user service..." if [[ $EUID -eq 0 && -n "$SUDO_USER" ]]; then sudo -u "$SUDO_USER" bash -c " export HOME=$(getent passwd $SUDO_USER | cut -d: -f6) export XDG_RUNTIME_DIR=/run/user/$(id -u $SUDO_USER) if systemctl --user is-active --quiet hush.service 2>/dev/null; then systemctl --user stop hush.service fi if systemctl --user is-enabled --quiet hush.service 2>/dev/null; then systemctl --user disable hush.service fi rm -f \$HOME/.config/systemd/user/hush.service systemctl --user daemon-reload " else if systemctl --user is-active --quiet hush.service 2>/dev/null; then systemctl --user stop hush.service fi if systemctl --user is-enabled --quiet hush.service 2>/dev/null; then systemctl --user disable hush.service fi rm -f "$HOME/.config/systemd/user/hush.service" systemctl --user daemon-reload fi success "systemd user service removed." } remove_openrc() { info "Stopping and removing OpenRC service..." if rc-service hush status &>/dev/null; then rc-service hush stop || true fi if rc-update show default 2>/dev/null | grep -q hush; then rc-update del hush default fi rm -f /etc/init.d/hush rm -f /etc/conf.d/hush success "OpenRC service removed." } # --------------------------------------------------------------------------- # Runtime artifact cleanup # --------------------------------------------------------------------------- remove_runtime_artifacts() { local run_user="${SUDO_USER:-$USER}" local uid uid=$(id -u "$run_user" 2>/dev/null || echo "") if [[ -n "$uid" ]]; then local sock_path="/run/user/$uid/hush.sock" if [[ -S "$sock_path" ]]; then info "Removing socket file: $sock_path" rm -f "$sock_path" fi fi # Also clean up /tmp fallback socket rm -f /tmp/hush.sock local data_home if [[ -n "$SUDO_USER" ]]; then local user_home user_home=$(getent passwd "$SUDO_USER" | cut -d: -f6) data_home="${XDG_DATA_HOME:-$user_home/.local/share}" else data_home="${XDG_DATA_HOME:-$HOME/.local/share}" fi local lock_file="$data_home/hush/daemon.lock" if [[ -f "$lock_file" ]]; then info "Removing lock file: $lock_file" rm -f "$lock_file" rmdir --ignore-fail-on-non-empty "$data_home/hush" 2>/dev/null || true fi } # --------------------------------------------------------------------------- # Config removal (optional) # --------------------------------------------------------------------------- remove_config() { local user_home if [[ -n "$SUDO_USER" ]]; then user_home=$(getent passwd "$SUDO_USER" | cut -d: -f6) else user_home="$HOME" fi local config_dir="$user_home/.config/hush" if [[ -d "$config_dir" ]]; then echo "" read -r -p "[uninstall] Remove configuration directory $config_dir? [y/N] " answer if [[ "$answer" =~ ^[Yy]$ ]]; then rm -rf "$config_dir" success "Configuration directory removed." else info "Configuration directory kept at $config_dir." fi fi } # --------------------------------------------------------------------------- # Main # --------------------------------------------------------------------------- main() { local init_system init_system=$(detect_init) require_root # Stop and remove service case "$init_system" in systemd) info "Detected init system: systemd" remove_systemd ;; openrc) info "Detected init system: OpenRC" remove_openrc ;; none) warn "Could not detect a supported init system — skipping service removal." ;; esac # Remove binary info "Removing binary..." rm -f "$BIN_DIR/hush" success "Binary removed." # Remove zsh completion if [[ -f "$COMPLETION_DIR/_hush" ]]; then info "Removing zsh completion..." rm -f "$COMPLETION_DIR/_hush" success "Zsh completion removed." fi # Clean up runtime artifacts remove_runtime_artifacts # Optionally remove user config remove_config echo "" success "Uninstall complete." echo "" } main "$@"