#!/bin/sh
# Bereitschaft: nimmt der FastCGI-Socket eine Verbindung an?
#
# Der Socket-Pfad kommt aus der Pool-Konfiguration, nicht aus einer Annahme --
# "listen" in pool.d/www.conf (Vorgabe /run/php/php8.5-fpm.sock, nachgesehen
# 24.09.2026). Ein "test -S" genuegt NICHT: eine Socketdatei kann liegen, ohne
# dass jemand darauf hoert (php-fpm raeumt sie beim harten Ende nicht auf).
#
# Verbunden wird mit s6-ipcclient aus s6 -- kein neues Paket noetig, und es
# spricht Unix-Sockets, was /dev/tcp in der Shell nicht kann. cgi-fcgi waere der
# andere Weg und braucht 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
