#!/bin/sh
API="http://127.0.0.1:28081/api/v1"
AUTH="admin:ikuai8.com"
# 删测试节点
for nm in gv-hk gv-tw fake-socks-19999 test-fake-socks test-fake-sock hk tw hk-verify tw-verify; do
  ids=$(curl -s -u "$AUTH" "$API/nodes" | grep -o "\"id\":[0-9]*,\"name\":\"$nm\"" | grep -o '^[0-9]*' 2>/dev/null)
  for id in $ids; do curl -s -u "$AUTH" -X DELETE "$API/nodes?ids=$id" >/dev/null; done
done
# 更稳妥：按名字里的测试前缀删
node_ids=$(curl -s -u "$AUTH" "$API/nodes" | grep -oE '"id":[0-9]+,"name":"(gv-|fake-|test-|hk|tw)' | grep -oE '"id":[0-9]+' | grep -oE '[0-9]+')
for id in $node_ids; do curl -s -u "$AUTH" -X DELETE "$API/nodes?ids=$id" >/dev/null; done
# 删测试订阅
sub_ids=$(curl -s -u "$AUTH" "$API/subscriptions" | grep -oE '"id":[0-9]+,"name":"(pf-ns-|ns-)' | grep -oE '"id":[0-9]+' | grep -oE '[0-9]+')
for id in $sub_ids; do curl -s -u "$AUTH" -X DELETE "$API/subscriptions?id=$id&delete_nodes=1" >/dev/null; done
# 删测试代理服务
svc_ids=$(curl -s -u "$AUTH" "$API/proxy-services" | grep -oE '"id":[0-9]+,"name":"(hk-relay|relay-)' | grep -oE '"id":[0-9]+' | grep -oE '[0-9]+')
for id in $svc_ids; do curl -s -u "$AUTH" -X DELETE "$API/proxy-services?ids=$id" >/dev/null; done
# 删测试规则(target=gvhk/gvtw等)
echo "剩余节点: $(curl -s -u "$AUTH" "$API/nodes" | grep -oc '"id":')"
echo "剩余订阅: $(curl -s -u "$AUTH" "$API/subscriptions" | grep -oc '"id":')"
echo "CLEANUP_DONE"
