#!/usr/bin/env bash
# Stop whatever is listening on the Next.js web port (default 3001).
set -euo pipefail
PORT="${WEB_PORT:-3001}"
PIDS=$(lsof -nP -iTCP:"$PORT" -sTCP:LISTEN -t 2>/dev/null || true)
if [ -z "$PIDS" ]; then
  echo "Nothing listening on port $PORT"
  exit 0
fi
echo "Stopping process(es) on port $PORT: $PIDS"
kill $PIDS
sleep 1
if lsof -nP -iTCP:"$PORT" -sTCP:LISTEN -t >/dev/null 2>&1; then
  echo "Force killing..."
  kill -9 $(lsof -nP -iTCP:"$PORT" -sTCP:LISTEN -t)
fi
echo "Port $PORT is free"
