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

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT_DIR"

stopped=1

if command -v docker >/dev/null 2>&1 || command -v docker-compose >/dev/null 2>&1; then
  ./scripts/docker-compose-local.sh down >/dev/null 2>&1 || true
  stopped=0
fi

if command -v pg_ctlcluster >/dev/null 2>&1; then
  pg_ctlcluster 17 main stop >/dev/null 2>&1 || true
  pg_ctlcluster 16 main stop >/dev/null 2>&1 || true
  stopped=0
fi

if command -v systemctl >/dev/null 2>&1; then
  systemctl stop postgresql >/dev/null 2>&1 || true
  systemctl --user stop postgresql >/dev/null 2>&1 || true
  stopped=0
fi

if command -v service >/dev/null 2>&1; then
  service postgresql stop >/dev/null 2>&1 || true
  stopped=0
fi

if [ "$stopped" -eq 1 ]; then
  echo "No supported local Postgres stop method found."
  exit 1
fi

echo "Local Postgres stop attempted."
