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

cd "$(dirname "$0")"

PID_FILE="ouptsw/wechat_algorithm_live.pid"
if [[ ! -f "$PID_FILE" ]]; then
  printf 'WeChat algorithm live monitor is not running: missing %s\n' "$PID_FILE"
  exit 0
fi

PID="$(cat "$PID_FILE")"
if [[ -z "$PID" ]] || ! kill -0 "$PID" 2>/dev/null; then
  rm -f "$PID_FILE"
  printf 'WeChat algorithm live monitor is not running. Removed stale pid file.\n'
  exit 0
fi

kill "$PID"
sleep 1
rm -f "$PID_FILE"
printf 'WeChat algorithm live monitor stopped: pid=%s\n' "$PID"
