# Maintainer: Pierre Schmitz <pierre@archlinux.de>
# PGO Version: Laio Seman <laio [at] ieee.org>

pkgbase=php
pkgname=('php'
         'php-cgi'
         'php-apache'
         'php-fpm'
         'php-embed'
         'php-phpdbg'
         'php-dblib'
         'php-enchant'
         'php-gd'
         'php-sodium'
         'php-odbc'
         'php-pgsql'
         'php-snmp'
         'php-sqlite'
         'php-tidy'
         'php-xsl')
pkgver=8.5.9
pkgrel=1
arch=('x86_64')
license=('PHP-3.01')
pkgdesc='A general-purpose scripting language that is especially suited to web development'
url='https://www.php.net/'
makedepends=('apache' 'enchant' 'gd' 'gmp' 'icu' 'libsodium' 'libxslt' 'libzip' 'net-snmp' 'argon2'
             'postgresql-libs' 'sqlite' 'systemd' 'tidy' 'unixodbc' 'curl' 'libtool' 'postfix' 'freetds' 'pcre2'
             'oniguruma')
checkdepends=('procps-ng')
source=("https://php.net/distributions/${pkgbase}-${pkgver}.tar.xz"
        'apache.patch' 'apache.conf' 'php-fpm.patch' 'disable-deepbind.patch' 'php-fpm.tmpfiles' 'php.ini.patch' 'php-cachy.patch' '0005-Enable-hugepages.patch'
        'https://raw.githubusercontent.com/SergiX44/php-benchmark-script/master/bench.php')
sha256sums=('0db7855f25bcd0ab1d592cdb35e284d6f6a5d2ae0f6f621122e364cc39b708f4'
            '74d33ad3e9fa2f0aaf6ceeddd59ec78d35e66673db74b1053cc3e6105478ba8a'
            '4a2add00d93fa991ccdf6356090264c1059c79935642afff6e8d4a2107fa037e'
            '78b8074461cdfc5d4e0ba428a8387f308d60720c8ad35c6176801e5088f02090'
            'a57e4f2e7b12f745472ab7ca242e4f2f310d32a2bcaf304a4e98020c335e2ec2'
            '640dba0d960bfeaae9ad38d2826d3f6b5d6c175a4d3e16664eefff29141faad5'
            '3cb032e719fe40963258616dcfe4937a6043042a31faef417c05dcb6e7295094'
            '0db0cc8b673890dc4a143b6f3ad9ad6498e0cbc2850f82526e7bbc4918990bd1'
            'faee19fff5f554638b28d3277388d6f3604738e8a2670f3e55562947e5678bbc'
            'b3210ff1b679f03a3bac409b9da491def692254f8354e4d09515cd2409fea23f')
validpgpkeys=('1198C0117593497A5EC5C199286AF1F9897469DC'
              '39B641343D8C104B2B146DC3F9C39DC0B9698544'
              'E60913E4DF209907D8E30D96659A97C9CF2A795A')
_interpreter=${pkgver%.*}
options=('!lto')


prepare() {
    cd "${srcdir}/${pkgbase}-${pkgver}"

    patch -p0 -i "${srcdir}/apache.patch"
    patch -p0 -i "${srcdir}/php-fpm.patch"
    patch -p0 -i "${srcdir}/php.ini.patch"

       # Cachy Patches
    patch -p0 -i "${srcdir}/php-cachy.patch"
#    patch -Np1 -i "${srcdir}/disable-deepbind.patch"
    patch -Np1 -i "${srcdir}/0005-Enable-hugepages.patch"
    autoconf

    # Disable failing tests
    rm tests/output/stream_isatty_*.phpt
    rm Zend/tests/arginfo_zpp_mismatch*.phpt
}

build() {

export CFLAGS+=" -fno-semantic-interposition"
export CXXLAGS+=" -fno-semantic-interposition"

    local _phpconfig="--srcdir=../${pkgbase}-${pkgver} \
        --config-cache \
        --prefix=/usr \
        --sbindir=/usr/bin \
        --sysconfdir=/etc/php \
        --localstatedir=/var \
        --with-layout=GNU \
        --with-config-file-path=/etc/php \
        --with-config-file-scan-dir=/etc/php/conf.d \
        --disable-rpath \
        --mandir=/usr/share/man \
        "

        local _phpextensions="\
                --enable-bcmath=shared \
                --enable-calendar=shared \
                --enable-dba=shared \
                --enable-exif=shared \
                --enable-ftp=shared \
                --enable-gd=shared \
                --enable-intl=shared \
                --enable-mbstring \
                --enable-pcntl \
                --enable-shmop=shared \
                --enable-soap=shared \
                --enable-sockets=shared \
                --enable-sysvmsg=shared \
                --enable-sysvsem=shared \
                --enable-sysvshm=shared \
                --with-bz2=shared \
                --with-curl=shared \
                --with-enchant=shared \
                --with-external-gd \
                --with-external-pcre \
                --with-ffi=shared \
                --with-gdbm \
                --with-gettext=shared \
                --with-gmp=shared \
                --with-iconv=shared \
                --with-ldap=shared \
                --with-ldap-sasl \
                --with-mhash \
                --with-mysql-sock=/run/mysqld/mysqld.sock \
                --with-mysqli=shared \
                --with-openssl \
                --with-openssl-argon2 \
                --with-password-argon2 \
                --with-pdo-dblib=shared,/usr \
                --with-pdo-mysql=shared \
                --with-pdo-odbc=shared,unixODBC,/usr \
                --with-pdo-pgsql=shared \
                --with-pdo-sqlite=shared \
                --with-pgsql=shared \
                --with-readline \
                --with-snmp=shared \
                --with-sodium=shared \
                --with-sqlite3=shared \
                --with-tidy=shared \
                --with-unixODBC=shared \
                --with-xsl=shared \
                --with-zip=shared \
                --with-zlib \
                "


    EXTENSION_DIR=/usr/lib/php/modules
    export EXTENSION_DIR

    mkdir -p "${srcdir}/build"
    cd "${srcdir}/build"
    ln -s "../${pkgbase}-${pkgver}/configure"
    ./configure ${_phpconfig} \
        --enable-cgi \
        --enable-fpm \
        --with-fpm-systemd \
        --with-fpm-acl \
        --with-fpm-user=http \
        --with-fpm-group=http \
        --enable-embed=shared \
        ${_phpextensions}
    make prof-gen

#   make test || true

    # Run benchmark for PGO
    PHP_BIN_DIR="${srcdir}/build/sapi/cli"
    cd "${srcdir}"
    ${PHP_BIN_DIR}/php bench.php
    ${PHP_BIN_DIR}/php bench.php --multiplier=2
    ${PHP_BIN_DIR}/php bench.php --multiplier=4
    ${PHP_BIN_DIR}/php bench.php --multiplier=8

    cd "${srcdir}/build"
    make clean
    make prof-use

    # apache
    # reuse the previous run; this will save us a lot of time
    cp -a "${srcdir}/build" "${srcdir}/build-apache"
    cd "${srcdir}/build-apache"
    ./configure ${_phpconfig} \
        --with-apxs2 \
        ${_phpextensions}
    make

    # phpdbg
    cp -a "${srcdir}/build" "${srcdir}/build-phpdbg"
    cd "${srcdir}/build-phpdbg"
    ./configure ${_phpconfig} \
        --enable-phpdbg \
        ${_phpextensions}
    make
}

#check() {
#   cd "${srcdir}/build"
#
#   # Check if sendmail was configured correctly (FS#47600)
#   sapi/cli/php -n -r 'echo ini_get("sendmail_path");' | grep -q '/usr/bin/sendmail'
#
#   export REPORT_EXIT_STATUS=1
#   export NO_INTERACTION=1
#   export SKIP_ONLINE_TESTS=1
#   export SKIP_SLOW_TESTS=1
#   export TEST_PHP_ARGS="-j$(nproc)"
#   export TESTS='tests Zend'
#
#   make test
#}

package_php() {
	pkgdesc='A general-purpose scripting language that is especially suited to web development'
	depends=('libxml2' 'curl' 'libzip' 'pcre2' 'argon2' 'oniguruma')
	provides=("php-interpreter=${_interpreter}" "php-intl=${pkgver}")
	replaces=('php-intl')
	conflicts=('php-intl')
	backup=('etc/php/php.ini')

	cd "${srcdir}/build"
	make -j1 INSTALL_ROOT="${pkgdir}" install-{modules,cli,build,headers,programs,pharcmd}
	install -D -m644 "${srcdir}/${pkgbase}-${pkgver}/php.ini-production" "${pkgdir}/etc/php/php.ini"
	install -d -m755 "${pkgdir}/etc/php/conf.d/"

	# remove static modules
	rm -f "${pkgdir}/usr/lib/php/modules/"*.a
	# remove modules provided by sub packages
	rm -f "${pkgdir}/usr/lib/php/modules/"{enchant,gd,sodium,odbc,pdo_dblib,pdo_odbc,pgsql,pdo_pgsql,snmp,sqlite3,pdo_sqlite,tidy,xsl}.so
}

package_php-cgi() {
	pkgdesc='CGI and FCGI SAPI for PHP'
	depends=('php')
	provides=("php-cgi-interpreter=${_interpreter}")

	cd "${srcdir}/build"
	make -j1 INSTALL_ROOT="${pkgdir}" install-cgi
}

package_php-apache() {
	pkgdesc='Apache SAPI for PHP'
	depends=('php' 'apache')
	backup=('etc/httpd/conf/extra/php_module.conf')
	provides=("php-apache-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build-apache/libs/libphp.so" "${pkgdir}/usr/lib/httpd/modules/libphp.so"
	install -D -m644 "${srcdir}/apache.conf" "${pkgdir}/etc/httpd/conf/extra/php_module.conf"
}

package_php-fpm() {
	pkgdesc='FastCGI Process Manager for PHP'
	depends=('php' 'systemd')
	backup=('etc/php/php-fpm.conf' 'etc/php/php-fpm.d/www.conf')
	provides=("php-fpm-interpreter=${_interpreter}")
	options=('!emptydirs')

	cd "${srcdir}/build"
	make -j1 INSTALL_ROOT="${pkgdir}" install-fpm
	install -D -m644 sapi/fpm/php-fpm.service "${pkgdir}/usr/lib/systemd/system/php-fpm.service"
	install -D -m644 "${srcdir}/php-fpm.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/php-fpm.conf"
}

package_php-embed() {
	pkgdesc='Embedded PHP SAPI library'
	depends=('php')
	provides=("php-embed-interpreter=${_interpreter}")
	options=('!emptydirs')

	cd "${srcdir}/build"
	make -j1 INSTALL_ROOT="${pkgdir}" PHP_SAPI=embed install-sapi
}

package_php-phpdbg() {
	pkgdesc='Interactive PHP debugger'
	depends=('php')
	provides=("php-phpdbg-interpreter=${_interpreter}")
	options=('!emptydirs')

	cd "${srcdir}/build-phpdbg"
	make -j1 INSTALL_ROOT="${pkgdir}" install-phpdbg
}

package_php-dblib() {
	pkgdesc='dblib module for PHP'
	depends=('php' 'freetds')
	provides=("php-dblib-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/pdo_dblib.so" "${pkgdir}/usr/lib/php/modules/pdo_dblib.so"
}

package_php-enchant() {
	pkgdesc='enchant module for PHP'
	depends=('php' 'enchant')
	provides=("php-enchant-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/enchant.so" "${pkgdir}/usr/lib/php/modules/enchant.so"
}

package_php-gd() {
	pkgdesc='gd module for PHP'
	depends=('php' 'gd')
	provides=("php-gd-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/gd.so" "${pkgdir}/usr/lib/php/modules/gd.so"
}

package_php-sodium() {
	pkgdesc='sodium module for PHP'
	depends=('php' 'libsodium')
	provides=("php-sodium-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/sodium.so" "${pkgdir}/usr/lib/php/modules/sodium.so"
}

package_php-odbc() {
	pkgdesc='ODBC modules for PHP'
	depends=('php' 'unixodbc')
	provides=("php-odbc-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/odbc.so" "${pkgdir}/usr/lib/php/modules/odbc.so"
	install -D -m755 "${srcdir}/build/modules/pdo_odbc.so" "${pkgdir}/usr/lib/php/modules/pdo_odbc.so"
}

package_php-pgsql() {
	pkgdesc='PostgreSQL modules for PHP'
	depends=('php' 'postgresql-libs')
	provides=("php-pgsql-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/pgsql.so" "${pkgdir}/usr/lib/php/modules/pgsql.so"
	install -D -m755 "${srcdir}/build/modules/pdo_pgsql.so" "${pkgdir}/usr/lib/php/modules/pdo_pgsql.so"
}

package_php-snmp() {
	pkgdesc='snmp module for PHP'
	depends=('php' 'net-snmp')
	provides=("php-snmp-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/snmp.so" "${pkgdir}/usr/lib/php/modules/snmp.so"
}

package_php-sqlite() {
	pkgdesc='sqlite module for PHP'
	depends=('php' 'sqlite')
	provides=("php-sqlite-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/sqlite3.so" "${pkgdir}/usr/lib/php/modules/sqlite3.so"
	install -D -m755 "${srcdir}/build/modules/pdo_sqlite.so" "${pkgdir}/usr/lib/php/modules/pdo_sqlite.so"
}

package_php-tidy() {
	pkgdesc='tidy module for PHP'
	depends=('php' 'tidy')
	provides=("php-tidy-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/tidy.so" "${pkgdir}/usr/lib/php/modules/tidy.so"
}

package_php-xsl() {
	pkgdesc='xsl module for PHP'
	depends=('php' 'libxslt')
	provides=("php-xsl-interpreter=${_interpreter}")

	install -D -m755 "${srcdir}/build/modules/xsl.so" "${pkgdir}/usr/lib/php/modules/xsl.so"
}
