#!/usr/bin/env bash set -euo pipefail C_RESET="\033[0m"; C_DIM="\033[2m"; C_BOLD="\033[1m" C_RED="\033[31m"; C_GRN="\033[32m"; C_BLU="\033[34m"; C_YEL="\033[33m" info(){ echo -e "${C_BLU}➜${C_RESET} $*"; } ok(){ echo -e "${C_GRN}✓${C_RESET} $*"; } warn(){ echo -e "${C_YEL}!${C_RESET} $*"; } fail(){ echo -e "${C_RED}✗${C_RESET} $*"; } trap 'fail "An unexpected error occurred. Exiting."' ERR PANEL_ROOT="/var/www/snowpanel" PANEL_PUBLIC="$PANEL_ROOT/public" STATE_DIR="/var/lib/snowpanel" LOG_DIR="/var/log/snowpanel" ETC_APP="/etc/snowpanel" NGX_SITE_AVAIL="/etc/nginx/sites-available/snowpanel" NGX_SITE_ENABL="/etc/nginx/sites-enabled/snowpanel" SUDOERS_FILE="/etc/sudoers.d/snowpanel" COLLECTOR_BIN="/usr/local/bin/snowpanel-collect.py" LOGDUMP_BIN="/usr/local/bin/snowpanel-logdump" SVC="/etc/systemd/system/snowpanel-collector.service" TIMER="/etc/systemd/system/snowpanel-collector.timer" SF_DROPIN_DIR="/etc/systemd/system/snowflake-proxy.service.d" SF_ACCOUNTING="$SF_DROPIN_DIR/10-accounting.conf" PKGS=("snowflake-proxy" "nginx" "php-fpm" "php-cli" "php-json" "php-curl" "php-zip" "php-common" "php-opcache") YES=0 PURGE=0 for arg in "${@:-}"; do case "$arg" in -y|--yes) YES=1 ;; -h|--help) echo "Usage: sudo bash uninstall.sh [--yes]" exit 0 ;; *) ;; esac done if [[ $EUID -ne 0 ]]; then fail "Run as root (sudo)."; exit 1; fi echo -e "${C_BOLD}Uninstalling SnowPanel...${C_RESET}" if [[ $YES -ne 1 ]]; then read -r -p "This will remove SnowPanel files/configs. Continue? [y/N] " ans case "${ans,,}" in y|yes) ;; *) warn "Aborted by user."; exit 0 ;; esac fi if [[ $YES -ne 1 ]]; then echo echo -e "${C_DIM}Optional:${C_RESET} Purge snowflake/nginx/php packages." read -r -p "Also purge packages and autoremove? [y/N] " pans case "${pans,,}" in y|yes) PURGE=1 ;; *) PURGE=0 ;; esac else PURGE=0 fi info "Stopping timers" systemctl disable --now snowpanel-collector.timer 2>/dev/null || true systemctl disable --now snowpanel-collector.service 2>/dev/null || true ok "Timers stopped" info "Removing systemd units" rm -f "$SVC" "$TIMER" systemctl daemon-reload ok "Units removed" info "Removing helper binaries" rm -f "$COLLECTOR_BIN" "$LOGDUMP_BIN" ok "Helpers removed" info "Removing Nginx site" rm -f "$NGX_SITE_ENABL" "$NGX_SITE_AVAIL" if nginx -t >/dev/null 2>&1; then systemctl reload nginx || true; fi ok "Nginx site removed" info "Removing app files" rm -rf "$PANEL_ROOT" "$STATE_DIR" "$LOG_DIR" "$ETC_APP" ok "Files removed" info "Removing accounting drop-in" rm -f "$SF_ACCOUNTING" systemctl daemon-reload systemctl restart snowflake-proxy 2>/dev/null || true ok "Drop-in removed" info "Removing sudoers" rm -f "$SUDOERS_FILE" ok "Sudoers removed" if [[ $PURGE -eq 1 ]]; then info "Purging packages" apt-get purge -y "${PKGS[@]}" || true apt-get autoremove -y || true ok "Packages purged" fi echo echo -e "${C_BOLD}All clean!${C_RESET}" if [[ $PURGE -eq 1 ]]; then echo -e "SnowPanel removed and packages were purged." else echo -e "SnowPanel files removed; packages remain." fi