#!/bin/sh
# cPanel-safe mail pipe runner for Asana Form Builder inbound replies.
# This wrapper finds a PHP CLI binary and executes inbound-mail-pipe.php.

SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
PHP_SCRIPT="$SCRIPT_DIR/inbound-mail-pipe.php"

if [ ! -f "$PHP_SCRIPT" ]; then
  echo "[afb-inbound-pipe] Missing script: $PHP_SCRIPT" >&2
  exit 75
fi

for PHP_BIN in \
  /usr/local/bin/php \
  /opt/cpanel/ea-php83/root/usr/bin/php \
  /opt/cpanel/ea-php82/root/usr/bin/php \
  /opt/cpanel/ea-php81/root/usr/bin/php \
  /usr/bin/php \
  /bin/php
do
  if [ -x "$PHP_BIN" ]; then
    exec "$PHP_BIN" -q "$PHP_SCRIPT"
  fi
done

if command -v php >/dev/null 2>&1; then
  exec "$(command -v php)" -q "$PHP_SCRIPT"
fi

echo "[afb-inbound-pipe] Could not locate a PHP CLI binary." >&2
exit 75
