#!/usr/bin/env bash
set -euo pipefail

PID_DIR="${PID_DIR:-/var/run/dubbo-dev-proxy}"
MAP_FILE="${MAP_FILE:-/opt/dubbo-dev-proxy/dubbo-proxy.map}"

systemctl stop dubbo-dev-proxy.timer 2>/dev/null || true
systemctl stop dubbo-dev-proxy.service 2>/dev/null || true

while IFS= read -r line || [[ -n "$line" ]]; do
  [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue
  IFS='|' read -r port _ _ <<< "$line"
  pid_file="${PID_DIR}/${port}.pid"
  if [[ -f "$pid_file" ]]; then
    pid="$(cat "$pid_file" 2>/dev/null || true)"
    if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null; then
      kill "$pid" 2>/dev/null || true
    fi
    rm -f "$pid_file"
  fi
done < "$MAP_FILE"

echo "dubbo-dev-proxy 已停止"
