#!/bin/sh
# Readiness: does the FastCGI socket accept a connection?
#
# The socket path comes from the pool configuration, not from an assumption --
# "listen" in pool.d/www.conf (default /run/php/php8.5-fpm.sock, checked
# 24.09.2026). A "test -S" is NOT enough: a socket file can exist without
# anyone listening on it (php-fpm does not clean it up on a hard exit).
#
# The connection is made with s6-ipcclient from s6 -- no new package needed,
# and it speaks Unix sockets, which /dev/tcp in the shell cannot. cgi-fcgi
# would be the other way and needs libfcgi-bin.

set -u

POOLDIR=${PHP_FPM_POOLDIR:-/etc/php/8.5/fpm/pool.d}
SOCK=''

for f in "$POOLDIR"/*.conf; do
    [ -r "$f" ] || continue
    SOCK=$(sed -n 's/^[[:space:]]*listen[[:space:]]*=[[:space:]]*\(\/[^[:space:];]*\).*/\1/p' "$f" 2>/dev/null | head -n1)
    [ -n "$SOCK" ] && break
done
[ -n "${SOCK:-}" ] || SOCK=/run/php/php8.5-fpm.sock

[ -S "$SOCK" ] || exit 1
exec s6-ipcclient -- "$SOCK" /bin/true
