#!/usr/bin/env bash
# GoCDN 卸载脚本（可选保留数据）
# 用法: ./uninstall.sh [--keep-data]
set -e
[ "$(id -u)" -eq 0 ] || { echo "请使用 root 运行"; exit 1; }

KEEP=0
[ "$1" = "--keep-data" ] && KEEP=1

systemctl disable --now gocdn-agent 2>/dev/null || true
systemctl disable --now gocdn-master 2>/dev/null || true
rm -f /etc/systemd/system/gocdn-agent.service /etc/systemd/system/gocdn-master.service
systemctl daemon-reload

openresty -s quit 2>/dev/null || true

if [ "$KEEP" -eq 1 ]; then
  echo "已停止服务，保留数据: /usr/local/gocdn"
else
  rm -rf /usr/local/gocdn
  echo "已停止服务并删除 /usr/local/gocdn"
fi
echo "GoCDN 卸载完成。"
