#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# php.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# Build and package mod_php on Slackware.
# by:  David Cantrell <david@slackware.com>
# Modified for PHP 4-5 by volkerdi@slackware.com
# Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2017, 2019, 2020, 2021  Patrick Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds
#slackset_var_cwds # For building from /patches

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Additional config options:
### export SLKCONFARGS="--with-sqlite3=shared,/usr"

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm|aarch64)  export SLKCFLAGS="$SLKCFLAGS -fPIC";;
   *)            export SLKCFLAGS="-O2" ;;
esac

################################################################

# GCC 14 "fix":
SLKCFLAGS="$SLKCFLAGS -Wno-error=incompatible-pointer-types"

# Generic "kitchen sink" configure function, with as many things as possible (and
# maybe then some ;-) compiled as shared extensions:
php_configure() {
LDADD="-ldl -lpthread" \
LIBS="-ldl -lpthread" \
LDFLAGS="-ldl -lpthread" \
EXTENSION_DIR=/usr/lib/php/extensions \
CFLAGS="$SLKCFLAGS $LDADD -DU_DEFINE_FALSE_AND_TRUE=1" \
CXXFLAGS="$SLKCFLAGS -DU_USING_ICU_NAMESPACE=1 -DU_DEFINE_FALSE_AND_TRUE=1" \
./configure --prefix=/usr \
  $* \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-libdir=lib${LIBDIRSUFFIX} \
   --localstatedir=/var \
   --sysconfdir=/etc \
   --datarootdir=/usr/share \
   --datadir=/usr/share \
   --infodir=/usr/info \
   --mandir=/usr/man \
   --with-apxs2=/usr/bin/apxs \
   --enable-fpm \
   --with-fpm-user=apache \
   --with-fpm-group=apache \
   --enable-pcntl \
   --enable-mbregex \
   --enable-tokenizer=shared \
   --with-config-file-scan-dir=/etc/php.d \
   --with-config-file-path=/etc \
   --with-layout=PHP \
   --disable-sigchild \
   --with-libxml \
   --with-expat \
   --enable-simplexml \
   --enable-xmlreader=shared \
   --enable-dom=shared \
   --enable-filter \
   --disable-debug \
   --with-openssl=shared \
   --with-kerberos \
   --with-external-pcre \
   --with-zlib=shared,/usr \
   --enable-bcmath=shared \
   --with-bz2=shared,/usr \
   --enable-calendar=shared \
   --enable-ctype=shared \
   --with-curl=shared \
   --enable-dba=shared \
   --with-gdbm=/usr \
   --with-db4=/usr \
   --enable-exif=shared \
   --enable-ftp=shared \
   --enable-gd=shared \
   --with-external-gd \
   --with-jpeg \
   --with-xpm \
   --with-gettext=shared,/usr \
   --with-gmp=shared,/usr \
   --with-iconv=shared \
   --with-ldap=shared \
   --enable-mbstring=shared \
   --enable-mysqlnd=shared \
   --with-mysqli=shared,mysqlnd \
   --with-mysql-sock=/var/run/mysql/mysql.sock \
   --with-iodbc=shared,/usr \
   --enable-pdo=shared \
   --with-pdo-mysql=shared,mysqlnd \
   --with-pdo-sqlite=shared,/usr \
   --with-pdo-odbc=shared,iODBC,/usr \
   --with-pspell=shared,/usr \
   --with-enchant=shared,/usr \
   --enable-shmop=shared \
   --with-snmp=shared,/usr \
   --enable-soap=shared \
   --enable-sockets \
   --with-sqlite3=shared \
   --enable-sysvmsg \
   --enable-sysvsem \
   --enable-sysvshm \
   --with-xsl=shared,/usr \
   --with-zip=shared \
   --enable-shared=yes \
   --enable-static=no \
   --with-gnu-ld \
   --with-pic \
   --enable-intl=shared \
   --enable-opcache \
   --enable-phpdbg \
   --with-sodium \
   --with-password-argon2 \
   --without-readline \
   --with-libedit \
   --with-pear \
   --with-tidy=shared \
   --build=${SLK_ARCH_BUILD} || exit 1
}

# Function to extract source:
# On ARM, with PHP4 I found that I had to begin from scratch for every
# build of php.  With php5 I don't know but I'm sure it's just fine to
# continue with this trend of starting afresh -- it just takes longer.
function extract_php_source() {
  cd $TMPBUILD
  # Extract PHP source:
  rm -rf php-*
  tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig) || exit 1

  cd $PKGNAM-$VERSION || exit 1
  slackhousekeeping
  find . -name "*.orig" -print0 | xargs -0 rm -f

  # Apply patches:

  # Patch ini files:
  cat $CWD/php.ini-development.diff | patch -p1 --verbose || exit 1
  cat $CWD/php.ini-development.diff | patch -p1 --verbose php.ini-production || exit 1
  cat $CWD/php-fpm.conf.diff | patch -p1 --verbose || exit 1

  # Install the build folder into /usr/lib$LIBDIRSUFFIX/php/build
  # and adapt phpize accordingly:
  sed -i "s|build$|php/build|" scripts/Makefile.frag
  sed -i "s|build\"$|php/build\"|" scripts/phpize.in

  # Apply patches. These come from Debian's diff:
#  if [ $ARCH = arm ]; then
#
#     # Unpack the patch set:
#     tar xvvf $PORTCWD/sources/php*debian* || exit 1
#
#    for pf in \
#        0001-libtool_fixes.patch \
#        0004-libtool2.2.patch \
#        0005-we_WANT_libtool.patch \
#        ; do
#          auto_apply_patch debian/patches/$pf || exit 1
#    done

#     # PHP ships with libtool 1.4.3, which is buggy on ARM, so we force it to
#     # rebuild the configure system with the native autoconf tools
#     # Force the rebuild of the libtool components:
#     rm -f aclocal.m4 config.sub config.guess ltmain.sh
#     #libtoolize --force # we used to need this for older versions of php but
#     # now the Debian patch applied above forces it anyway.
#     ./buildconf --force
#
#  fi
}

##################################################################
mkdir -p $PKG/etc/httpd
mkdir -p $PKG/etc/php.d
# A trick from DaMouse to enable building php into $PKG.
# We'll remove this later on.
cat /etc/httpd/original/httpd.conf > $PKG/etc/httpd/httpd.conf
if [ ! -e /etc/httpd/original/httpd.conf ]; then
  echo "FATAL:  no /etc/httpd/original/httpd.conf found."
  exit 1
fi
##################################################################

################## Build Apache module & CGI interpreter ################

# Extract the PHP source archive:
extract_php_source

echo ":::::Building Apache2 module:::::"
# Make the Apache2 module version of PHP:
php_configure || exit 1
make $NUMJOBS LDFLAGS="-lpthread -ldl" || make LDFLAGS="-lpthread -ldl" || failmake
make install INSTALL_ROOT=$PKG || failinstall

# Don't include the c-client library in php-config output:
sed -i "s| -L/usr/local/lib${LIBDIRSUFFIX}/c-client/lib${LIBDIRSUFFIX}||g" $PKG/usr/bin/php-config
sed -i "s|  -lc-client||g" $PKG/usr/bin/php-config

mkdir -p $PKG/etc/{rc.d,php-fpm.d}
install -vpm644 sapi/fpm/init.d.php-fpm $PKG/etc/rc.d/rc.php-fpm.new

################### Clean up the installation ###################

# PHP (used to) install Pear with some strange permissions.
chmod 755 $PKG/usr/bin/pear

# PHP sometimes puts junk in the root directory:
( cd $PKG
  rm -rvf .channels .depdb .depdblock .filemap .lock .registry
)

# We do not package static extension libraries:
rm -f $PKG/usr/lib${LIBDIRSUFFIX}/php/extensions/*.a

# Fix $PKG/usr/lib/php perms:
( cd $PKG/usr/lib${LIBDIRSUFFIX}/php
  find . \
   \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
   -exec chmod 755 {} \; -o \
   \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
   -exec chmod 644 {} \;
)

# Copy docs:
mkdir -pm755 $PKG/usr/doc/php-$VERSION
cp -fav \
  CODING_STANDARDS* CONTRIBUTING* EXTENSIONS* LICENSE* NEWS* README* UPGRADING* \
  $PKG/usr/doc/$PKGNAM-$VERSION

mkdir -pm755 $PKG/etc/httpd
sed -e "s#lib/httpd#lib${LIBDIRSUFFIX}/httpd#" $CWD/mod_php.conf.example > $PKG/etc/httpd/mod_php.conf.new
chmod 644 $PKG/etc/httpd/*
chown root:root $PKG/etc/httpd/*

cp -a php.ini-development php.ini-production $PKG/etc
chmod 644 $PKG/etc/*
chmod 755 $PKG/etc/php.d $PKG/etc/php-fpm.d $PKG/etc/httpd
chown root:root $PKG/etc/*

# This can go now.
rm -f $PKG/etc/httpd/httpd*

# These are misplaced:
mkdir -pm755 $PKG/usr/man/man1
mv -f $PKG/usr/php/man/man1/* $PKG/usr/man/man1/
rmdir $PKG/usr/php

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Session directory for PHP - needs special permissions, therefore
# goes after the 'slackslack' call.
mkdir -pm770 $PKG/var/lib/php
chown root:apache $PKG/var/lib/php

# /extra has packages in separate dirs:
#export PKGSTORE=$PKGSTORE/../extra/
#export PKGSERIES=$PKGNAM

slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
