#!/bin/sh
set -e

# live-proxy 一键卸载脚本（OpenWrt procd + Ubuntu/Debian systemd 通用）
# 默认仅移除服务、程序与防火墙规则，保留全部配置与数据库：
#   wget -qO- https://tools.168668518.com/live-proxy/uninstall.sh | sh
#   curl -fsSL https://tools.168668518.com/live-proxy/uninstall.sh | sh
#
# 彻底卸载并删除全部数据与日志（不可恢复）：
#   wget -qO- https://tools.168668518.com/live-proxy/uninstall.sh | PURGE_DATA=1 sh
#
# 可选环境变量：
#   PURGE_DATA=1     同时删除数据目录与日志目录（默认 0 保留）
#   KEEP_FIREWALL=1  保留已有防火墙规则（默认 0 自动清理）

PURGE_DATA="${PURGE_DATA:-0}"
KEEP_FIREWALL="${KEEP_FIREWALL:-0}"

if [ "$(id -u)" -ne 0 ]; then
    echo "错误：请使用 root 权限运行本脚本（如 sudo sh）"
    exit 1
fi

if [ -f /etc/openwrt_release ] || [ -f /etc/rc.common ]; then
    echo "==> 识别为 OpenWrt 环境，正在卸载 live-proxy 服务..."
    DATA_DIR="${DATA_DIR:-/etc/live-proxy}"
    LOG_DIR="${LOG_DIR:-/tmp/live-proxy}"

    if [ -x /etc/init.d/live-proxy ]; then
        /etc/init.d/live-proxy stop 2>/dev/null || true
        /etc/init.d/live-proxy disable 2>/dev/null || true
    fi
    rm -f /etc/init.d/live-proxy /usr/bin/live-proxy

    if [ "$KEEP_FIREWALL" != "1" ] && command -v uci >/dev/null 2>&1 && [ -f /etc/config/firewall ]; then
        if uci -q get firewall.live_proxy >/dev/null 2>&1; then
            uci delete firewall.live_proxy
            uci commit firewall && /etc/init.d/firewall reload >/dev/null 2>&1 || true
            echo "==> 已移除 OpenWrt 防火墙规则 (firewall.live_proxy)"
        fi
    fi

    if [ "$PURGE_DATA" = "1" ]; then
        rm -rf "$DATA_DIR" "$LOG_DIR"
        echo "==> 已彻底删除数据目录 ($DATA_DIR) 与日志目录 ($LOG_DIR)"
    else
        echo "==> 已保留数据目录: $DATA_DIR（如需彻底删除请执行 PURGE_DATA=1）"
    fi

    echo "==> live-proxy OpenWrt 卸载完成！"
else
    echo "==> 识别为 Linux (Ubuntu/Debian) 环境，正在卸载 live-proxy 服务..."
    DATA_DIR="${DATA_DIR:-/var/lib/live-proxy}"
    LOG_DIR="${LOG_DIR:-/var/log/live-proxy}"
    SERVICE_FILE="/etc/systemd/system/live-proxy.service"
    PORT="${PORT:-}"

    if [ -z "$PORT" ] && [ -f "$SERVICE_FILE" ]; then
        PORT="$(grep -E '^Environment=PORT=' "$SERVICE_FILE" | head -n 1 | cut -d'=' -f3 | tr -cd '0-9')"
    fi
    PORT="${PORT:-8008}"

    if command -v systemctl >/dev/null 2>&1; then
        systemctl stop live-proxy 2>/dev/null || true
        systemctl disable live-proxy 2>/dev/null || true
    fi
    rm -f "$SERVICE_FILE" /usr/local/bin/live-proxy
    if command -v systemctl >/dev/null 2>&1; then
        systemctl daemon-reload 2>/dev/null || true
        systemctl reset-failed live-proxy 2>/dev/null || true
    fi

    if [ "$KEEP_FIREWALL" != "1" ]; then
        if command -v ufw >/dev/null 2>&1 && ufw status 2>/dev/null | grep -qi "Status: active"; then
            ufw delete allow "${PORT}/tcp" >/dev/null 2>&1 || true
            echo "==> 已尝试移除 UFW 规则 TCP/$PORT"
        elif command -v firewall-cmd >/dev/null 2>&1 && firewall-cmd --state >/dev/null 2>&1; then
            firewall-cmd --permanent --remove-port="${PORT}/tcp" >/dev/null 2>&1 && firewall-cmd --reload >/dev/null 2>&1 || true
            echo "==> 已尝试移除 firewalld 规则 TCP/$PORT"
        fi
    fi

    if [ "$PURGE_DATA" = "1" ]; then
        rm -rf "$DATA_DIR" "$LOG_DIR"
        echo "==> 已彻底删除数据目录 ($DATA_DIR) 与日志目录 ($LOG_DIR)"
    else
        echo "==> 已保留数据目录: $DATA_DIR 与日志目录: $LOG_DIR（如需彻底删除请执行 PURGE_DATA=1）"
    fi

    echo "==> live-proxy Linux 卸载完成！"
fi
