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

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_FILE="${ROOT}/.orchestration/pids"

if [[ -f "${PID_FILE}" ]]; then
  source "${PID_FILE}"
  for pid in "${frontend:-}" "${engine:-}" "${backend:-}"; do
    if [[ -n "${pid}" ]] && kill -0 "${pid}" >/dev/null 2>&1; then
      echo "Stopping pid ${pid}..."
      kill "${pid}" || true
    fi
  done
  rm -f "${PID_FILE}"
else
  echo "No pid file found."
fi

echo "Stopping PostgreSQL (docker)..."
docker compose stop postgres >/dev/null 2>&1 || true
