#!/bin/bash
# CUSTOM_BUILD_HARDENED
export LDFLAGS="${LDFLAGS:-} -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wl,-z,ibt -Wl,-z,shstk"
# CUSTOM_BUILD_NUMJOBS_PATCHED

# AMOS build script for php-zmq
# PHP bindings for ZeroMQ, built against the system php-config.
#
# Modified 2026: builds from the havennow/php-zmq fork instead of the
# abandoned PECL 1.1.3 tarball. The fork includes PHP 8.5 compatibility
# (commit 498b5b9, May 2026) and the GCC 15 prototype fixes
# (commit from Jan 2025).

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=php-zmq
SRCNAM=zmq
VERSION=${VERSION:-1.1.3}
BUILD=${BUILD:-1}
PKGTYPE=${PKGTYPE:-txz}

# Directory the tarball extracts to (the fork ships as php-zmq/)
SRCDIR=php-zmq

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686 -fhardened"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686 -fhardened"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC -fhardened"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2 -fhardened"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCDIR
tar xf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCDIR
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

PHP_CONFIG=${PHP_CONFIG:-/usr/bin/php-config}
if [ ! -x "$PHP_CONFIG" ]; then
  echo "ERROR: $PHP_CONFIG not found or not executable" >&2
  exit 1
fi

/usr/bin/phpize

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --with-php-config=$PHP_CONFIG \
  --build=$ARCH-slackware-linux

make

EXTENSION_DIR="$PKG/$($PHP_CONFIG --extension-dir)"
INCLUDE_DIR="$PKG/$($PHP_CONFIG --include-dir)"
mkdir -p $EXTENSION_DIR $INCLUDE_DIR $PKG/etc/php.d

make \
  EXTENSION_DIR=$EXTENSION_DIR \
  phpincludedir=$INCLUDE_DIR \
  DESTDIR=$PKG \
  install

# Enable the extension. No ini ships with the fork, so we write one;
# doinst.sh's config() moves it into place on install.
echo "; Enable the zmq extension module" > $PKG/etc/php.d/zmq.ini.new
echo "extension=zmq.so"                    >> $PKG/etc/php.d/zmq.ini.new

find $PKG -print0 | xargs -0 file \
  | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
for doc in ChangeLog CREDITS LICENSE README; do
  if [ -f "$doc" ]; then
    cp -a "$doc" $PKG/usr/doc/$PRGNAM-$VERSION/
  fi
done
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

rm -f $PKG/usr/lib*/*.la

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
sed "s|LIBDIRSUFFIX|$LIBDIRSUFFIX|" $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD.$PKGTYPE
