# How PHP is built:
# It is a loop of pain, because PHP would never allow building
# a full package in one go.
# Smart developers of Arch Linux invented a way to sequentially
# build different flavors of PHP, therefore allowing a full
# PHP package to be shipped.
# PHP clears all .so files found in SRCDIR when 'make clean'.
# To stop installed extension .so files from being removed
# (because PKGDIR in a subdirectory of SRCDIR), we first install
# all files into a TMPPKGDIR and move files into PKGDIR at last.

abinfo "Preparing the environment ..."
export EXTENSION_DIR=/usr/lib/php/modules
export PEAR_INSTALLDIR=/usr/share/pear
TMPPKGDIR="$(realpath ../tmpabdist)"
mkdir -vp "$TMPPKGDIR"

abinfo "Building and installing PHP ..."
"$SRCDIR"/configure \
    "${AUTOTOOLS_DEF[@]}" ${AUTOTOOLS_AFTER[@]} \
    --without-pear \
    --disable-cgi \
    --enable-pcntl
make
make INSTALL_ROOT="$TMPPKGDIR" install
install -dvm755 "$TMPPKGDIR"/usr/share/pear
install -Dvm644 "$SRCDIR"/php.ini-production \
    "$TMPPKGDIR"/etc/php/php.ini
install -dvm755 "$TMPPKGDIR"/etc/php/conf.d/
rm -v "$TMPPKGDIR"/usr/bin/phar
ln -sv phar.phar \
    "$TMPPKGDIR"/usr/bin/phar
make distclean

abinfo "Building and installing PHP with Pear module ..."
"$SRCDIR"/configure \
    "${AUTOTOOLS_DEF[@]}" ${AUTOTOOLS_AFTER[@]} \
    --with-pear
make
make install-pear INSTALL_ROOT="$TMPPKGDIR"
abinfo "Dropping dot files ..."
rm -rv "$TMPPKGDIR"/usr/share/pear/.{channels,filemap,lock,registry}
rm -rv "$TMPPKGDIR"/.{channels,depdb,depdblock,filemap,lock,registry}
make distclean

abinfo "Building and installing PHP with CGI and FCGI executable ..."
"$SRCDIR"/configure \
    "${AUTOTOOLS_DEF[@]}" ${AUTOTOOLS_AFTER[@]} \
    --disable-cli \
    --enable-cgi
make
install -Dvm755 "$SRCDIR"/sapi/cgi/php-cgi \
    "$TMPPKGDIR"/usr/bin/php-cgi
make distclean

abinfo "Building and installing PHP with Apache/HTTPD module ..."
"$SRCDIR"/configure \
    "${AUTOTOOLS_DEF[@]}" ${AUTOTOOLS_AFTER[@]} \
    --disable-cli \
    --with-apxs2
make
install -Dvm755 "$SRCDIR"/libs/libphp.so \
    "$TMPPKGDIR"/usr/lib/httpd/modules/libphp.so
make distclean

abinfo "Building and installing PHP with FPM module ..."
"$SRCDIR"/configure \
    "${AUTOTOOLS_DEF[@]}" ${AUTOTOOLS_AFTER[@]} \
    --disable-cli \
    --enable-cgi \
    --enable-fpm \
    --with-fpm-systemd \
    --with-fpm-user=http \
    --with-fpm-group=http
make
make install-fpm INSTALL_ROOT="$TMPPKGDIR" -j1
install -Dvm755 "$SRCDIR"/sapi/fpm/php-fpm.service \
    "$TMPPKGDIR"/usr/lib/systemd/system/php-fpm.service
install -Dvm755 "$SRCDIR"/sapi/fpm/php-fpm \
    "$TMPPKGDIR"/usr/bin/php-fpm
install -Dvm644 "$SRCDIR"/sapi/fpm/php-fpm.8 \
    "$TMPPKGDIR"/usr/share/man/man8/php-fpm.8
install -Dvm644 "$SRCDIR"/sapi/fpm/php-fpm.conf \
    "$TMPPKGDIR"/etc/php/php-fpm.conf
install -dvm755 "$TMPPKGDIR"/etc/php/php-fpm.d
make distclean

abinfo "Setting user/group in /etc/php/php-fpm.d/www.conf ..."
abwarn "FIXME: Why doesn't --with-fpm-{user,group} work?"
sed -e 's|nobody|http|g' \
    -i "$TMPPKGDIR"/etc/php/php-fpm.d/www.conf

abinfo "Building and installing PHP with Embed module ..."
"$SRCDIR"/configure \
    "${AUTOTOOLS_DEF[@]}" ${AUTOTOOLS_AFTER[@]} \
    --disable-cli \
    --enable-embed=shared
make
install -Dvm755 "$SRCDIR"/libs/libphp.so \
    "$TMPPKGDIR"/usr/lib/libphp.so
install -Dvm644 "$SRCDIR"/sapi/embed/php_embed.h \
    "$TMPPKGDIR"/usr/include/php/sapi/embed/php_embed.h
make distclean

abinfo "Moving /var/run => /run ..."
mv -v "$TMPPKGDIR"/{var/,}run

abinfo "Moving files from TMPPKGDIR to PKGDIR ..."
mkdir -vp "$PKGDIR"
mv -v "$TMPPKGDIR"/* "$PKGDIR"/
