#!/bin/bash
# php2shell 修補：用原来嘅 configure 旗標編譯 PHP 8.4.27（只做 configure，唔碰 live）
set -euo pipefail
V=8.4.27
SRC=/usr/src/php-$V
LOG=/root/php${V//./}_build.log

echo "== 下載官方源碼 =="
cd /usr/src
if [ ! -f php-$V.tar.gz ]; then
  curl -sSL -o php-$V.tar.gz https://www.php.net/distributions/php-$V.tar.gz
fi
echo "-- sha256（同 php.net 公告核對）--"
sha256sum php-$V.tar.gz
curl -sSL https://www.php.net/release-info/php-$V 2>/dev/null | head -5 || true

echo "== 解壓 =="
rm -rf "$SRC"
tar xzf php-$V.tar.gz -C /usr/src
cd "$SRC"

echo "== configure（旗標與現有 8.4.26 完全一致）=="
./configure \
  '--prefix=/www/server/php/84' \
  '--with-config-file-path=/www/server/php/84/etc' \
  '--enable-fpm' \
  '--with-fpm-user=www' \
  '--with-fpm-group=www' \
  '--enable-mysqlnd' \
  '--with-mysqli=mysqlnd' \
  '--with-pdo-mysql=mysqlnd' \
  '--with-iconv-dir' \
  '--with-freetype' \
  '--with-jpeg' \
  '--with-zlib' \
  '--with-libxml-dir=/usr' \
  '--enable-xml' \
  '--disable-rpath' \
  '--enable-bcmath' \
  '--enable-shmop' \
  '--enable-sysvsem' \
  '--enable-inline-optimization' \
  '--with-curl' \
  '--enable-mbregex' \
  '--enable-mbstring' \
  '--enable-intl' \
  '--enable-pcntl' \
  '--enable-ftp' \
  '--enable-gd' \
  '--with-openssl' \
  '--with-mhash' \
  '--enable-sockets' \
  '--with-gettext' \
  '--disable-fileinfo' \
  '--enable-opcache' \
  '--with-sodium=/usr/local/libsodium' \
  '--with-webp' \
  > "$LOG" 2>&1
echo "configure exit=$?"
echo "-- configure 尾段 --"
tail -12 "$LOG"
echo "-- 有無 missing/error --"
grep -inE 'configure: error|not found|missing' "$LOG" | head -20 || echo "(none)"
echo "-- 已启用嘅模組清單 --"
grep -A2 '^Configure Command' /dev/null 2>/dev/null || true
