#!/usr/bin/env bash # ============================================================================== # nx9-wg Uninstaller # ============================================================================== # Safely removes the nx9-wg service and binary while preserving user data # and configuration by default. Supports --purge for total teardown. # ============================================================================== set -euo pipefail BIN_PATH="/usr/local/bin/nx9-wg" CONF_DIR="/etc/nx9-wg" DATA_DIR="/var/lib/nx9-wg" LOG_DIR="/var/log/nx9-wg" SERVICE_PATH="/etc/systemd/system/nx9-wg.service" PURGE=0 FORCE=0 usage() { cat <&2 usage ;; esac done log() { echo -e "\033[1;34m[INFO]\033[0m $*" } warn() { echo -e "\033[1;33m[WARN]\033[0m $*" } error() { echo -e "\033[1;31m[ERROR]\033[0m $*" >&2 exit 1 } if [[ "${EUID}" -ne 0 ]]; then error "This uninstaller must be run as root (or via sudo)." fi # 1. Stop and Disable systemd Service if command -v systemctl >/dev/null 2>&1; then if systemctl is-active --quiet nx9-wg 2>/dev/null; then log "Stopping nx9-wg service..." systemctl stop nx9-wg || true fi if systemctl is-enabled --quiet nx9-wg 2>/dev/null; then log "Disabling nx9-wg service..." systemctl disable nx9-wg || true fi fi # 2. Remove systemd Unit File if [[ -f "${SERVICE_PATH}" ]]; then log "Removing systemd unit file ${SERVICE_PATH}..." rm -f "${SERVICE_PATH}" if command -v systemctl >/dev/null 2>&1; then systemctl daemon-reload || true fi fi # 3. Remove Binary Executable if [[ -f "${BIN_PATH}" ]]; then log "Removing binary ${BIN_PATH}..." rm -f "${BIN_PATH}" fi # 4. Handle Purge vs Data Preservation if [[ "${PURGE}" -eq 1 ]]; then if [[ "${FORCE}" -eq 0 ]]; then echo -e "\033[1;31mWARNING: --purge will permanently delete all configuration, database state, and backups!\033[0m" read -r -p "Type 'DELETE-ALL-DATA' to confirm: " CONFIRM if [[ "${CONFIRM}" != "DELETE-ALL-DATA" ]]; then error "Purge aborted by user. Preserving configuration and data directories." fi fi log "Purging configuration directory ${CONF_DIR}..." rm -rf "${CONF_DIR}" log "Purging data directory ${DATA_DIR}..." rm -rf "${DATA_DIR}" log "Purging log directory ${LOG_DIR}..." rm -rf "${LOG_DIR}" log "All nx9-wg files purged." else log "Preserved configuration: ${CONF_DIR}" log "Preserved database & backups: ${DATA_DIR}" log "To remove them, re-run with: sudo bash uninstall.sh --purge" fi log "nx9-wg uninstalled successfully." EOF