#!/bin/sh
#
# Build and package mod_php on Slackware.
# by:  David Cantrell <david@slackware.com>
# Modified for PHP 4+ by volkerdi@slackware.com
CWD=`pwd`
TMP=${TMP:-/tmp}
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
PKG=$TMP/package-php

VERSION=4.3.11
PINE=4.60
ARCH=${ARCH:-i486}
BUILD=4

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

# we need to compile Pine to get c-client.a for IMAP support:
IMAPLIBDIR=/usr/local/lib/c-client
if [ -r $IMAPLIBDIR/lib/c-client.a ]; then
  echo "Using IMAP library:"
  ls -l $IMAPLIBDIR/lib/c-client.a
  sleep 5
else
  ( cd $CWD/../pine ; ./pine.SlackBuild )
  ( cd $TMP/pine${PINE}/imap/c-client
    strip -g c-client.a
    mkdir -p $IMAPLIBDIR/lib
    cp c-client.a $IMAPLIBDIR/lib
    mkdir -p $IMAPLIBDIR/include
    cp *.h $IMAPLIBDIR/include
  )
fi

php_configure() {
EXTENSION_DIR=/usr/lib/php/extensions \
CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr \
  $* \
  --sysconfdir=/etc \
  --enable-discard-path \
  --with-config-file-path=/etc/apache \
  --enable-safe-mode \
  --with-openssl \
  --enable-bcmath \
  --with-bz2 \
  --with-pic \
  --enable-calendar \
  --enable-ctype \
  --with-gdbm \
  --with-db3 \
  --with-imap-ssl=$IMAPLIBDIR \
  --with-imap=$IMAPLIBDIR \
  --enable-dbase \
  --enable-ftp \
  --with-iconv \
  --with-gd \
  --enable-gd-native-ttf \
  --with-jpeg-dir=/usr \
  --with-png \
  --with-gmp \
  --with-mysql=shared,/usr \
  --with-xml=shared,/usr \
  --with-pcre-regex=/usr \
  --with-gettext=shared,/usr \
  --with-mm=/usr \
  --enable-trans-sid \
  --enable-shmop \
  --enable-sockets \
  --with-regex=php \
  --enable-sysvsem \
  --enable-sysvshm \
  --enable-yp \
  --enable-memory-limit \
  --with-tsrm-pthreads \
  --enable-shared \
  --disable-debug \
  --with-zlib=/usr
  # --with-mod_charset    # only for Russian patched Apache
  # --with-readline=/usr  # this is only for the CGI version
  # --with-ttf            # this links with the shlib, need X for that
  # --with-java           # no thanks
  # --with-dom            # requires libxml >= 2.2.7
  # --with-curl           # no requests for this, so we'll avoid the dependancy
}

# compile php
rm -rf $PKG
mkdir -p $PKG/etc/apache
# A trick from DaMouse to enable building php into $PKG.
# We'll remove this later on.
cat /etc/apache/httpd.conf.default > $PKG/etc/apache/httpd.conf
cd $TMP
rm -rf php-$VERSION
tar xjvf $CWD/php-$VERSION.tar.bz2

# Add missing(?) PEAR modules back:
( cd php-$VERSION/pear/packages
  cp -a $CWD/pear/* .
  bzip2 -d *.bz2
)

cd php-$VERSION
# Fixup perms/owners:
chown -R root.root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -name "*.h" -exec chmod 644 {} \;
# Sometimes they ship a few of these:
find . -name "*.orig" -exec rm {} \;
# Patch ini files:
zcat $CWD/php.ini.diff.gz | patch -p1 --verbose --backup --suffix=.orig

# Make the Apache module version of PHP:
php_configure --disable-static --with-apxs=/usr/sbin/apxs
make -j3
make install INSTALL_ROOT=$PKG
make distclean

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

# Make the standalone interpreter:
php_configure --enable-force-cgi-redirect --enable-fastcgi --enable-pcntl --enable-sigchild
make -j3
make install-cli INSTALL_ROOT=$PKG
chmod 755 $PKG/usr/bin/php
chown root.bin $PKG/usr/bin/php

# Fix $PKG/usr/lib/php perms:
( cd $PKG/usr/lib/php
  find . -perm 666 -exec chmod 644 {} \;
)

mkdir -p $PKG/usr/doc/php-$VERSION
cp -a \
  CODING_STANDARDS CREDITS EXTENSIONS INSTALL LICENSE NEWS README* TODO* \
  sapi/cgi/README.FastCGI \
  $PKG/usr/doc/php-$VERSION
chown -R root.root $PKG/usr/doc/php-$VERSION

mkdir -p $PKG/etc/apache
cp -a php.ini-dist php.ini-recommended $PKG/etc/apache
cp -a $CWD/mod_php.conf.example $PKG/etc/apache
chmod 644 $PKG/etc/apache/*
chown root.root $PKG/etc/apache/*

rm -f $PKG/etc/apache/httpd*

chown -R root:bin $PKG/usr/bin

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

gzip -9 $PKG/usr/man/man?/*.?

mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

#if [ -d "$IMAPLIBDIR" ]; then
#  ( cd $IMAPLIBDIR && rm -rf * )
#  rmdir $IMAPLIBDIR
#fi

cd $PKG
makepkg -l y -c n $TMP/php-$VERSION-$ARCH-$BUILD.tgz