#!/bin/bash

# php.SlackBuild
# Build and package mod_php on Slackware.
# by:  David Cantrell <david@slackware.com>
# Modified for PHP 4+ by volkerdi@slackware.com
# Modified for ARMedslack by Stuart Winter <mozes@armedslack.org>
# 24-Oct-2004

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

# PINE's IMAP client is used.  This version number will need
# to be changed when PINE is upgraded.
PINEVER=4.64

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

############# Build PINE ######################################
# We need to compile Pine to get c-client.a for IMAP support:
#IMAPLIBDIR=$TMPBUILD/c-client
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
  # ** This code is taken from pine.SlackBuild -- remember to update it if
  #     anything significat changes in the build script: **
  ( mkdir -p $IMAPLIBDIR/{lib,include}
    tar jxvvf $CWD/../pine/pine$PINEVER.tar.bz2 
    cd pine*
    slackhousekeeping
    ./build slx SSLLIB=/usr/lib \
                SSLDIR=/etc/ssl \
                SSLCERTS=/etc/ssl/certs \
                SSLINCLUDE=/usr/include/openssl \
                SSLTYPE=unix \
                OPTIMIZE="$SLKCFLAGS" \
                DEBUG="$SLKCFLAGS" \
               'LDAPLIBS=-lldap -llber' \
               'LDAPCFLAGS=-DENABLE_LDAP' || failmake
    cd imap/c-client
    strip -g c-client.a
    cp -vfaL c-client.a $IMAPLIBDIR/lib
    cp -vfaL *.h $IMAPLIBDIR/include  )
fi

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

# Configure function:
php_configure() {
EXTENSION_DIR=/usr/lib/php/extensions \
CFLAGS="$SLKCFLAGS" \
./configure $* \
   --prefix=/usr \
   --mandir=/usr/man \
   --sysconfdir=/etc \
   --with-config-file-path=/etc/apache \
   --enable-safe-mode \
   --with-openssl \
   --with-mhash \
   --enable-bcmath \
   --with-bz2 \
   --with-pic \
   --enable-calendar \
   --enable-ctype \
   --with-gdbm \
   --with-db4 \
   --with-imap-ssl=$IMAPLIBDIR \
   --with-imap=$IMAPLIBDIR \
   --enable-dbase \
   --enable-ftp \
   --with-iconv \
   --with-exif \
   --enable-exif \
   --with-gd \
   --enable-gd-native-ttf \
   --with-jpeg-dir=/usr \
   --with-png \
   --with-ttf \
   --with-freetype-dir=/usr \
   --with-gmp \
   --with-mysql=shared,/usr \
   --with-gettext=shared,/usr \
   --with-expat-dir=/usr \
   --with-xml \
   --enable-wddx \
   --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 \
   --enable-mbstring \
   --with-curl=/usr \
   --with-dom \
   --with-pcre-regex=/usr \
   --with-ldap \
   # --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
}

function extract_php_source() {
  cd $TMPBUILD
  # Extract PHP source:
  rm -rf php-*
  tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2

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

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

  # Patch ini files:
  zcat $CWD/php.ini.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch

  # Fix a UTF-8 parsing overflow:
  zcat $CWD/php4.utf8.diff.gz | patch -p1 --verbose --backup --suffix=.orig || failpatch

  # Apply patches. These come from Debian's diff:
  if [ $PORTARCH = arm ]; then

     # 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
     bzcat $PORTCWD/sources/001-libtool_fixes.patch.bz2   | patch -p1 --fuzz=5 --verbose || failpatch
     bzcat $PORTCWD/sources/033-we_WANT_libtool.patch.bz2 | patch -p1 --fuzz=5 --verbose || failpatch
     # Patch code for little-endian ARM with no VFP:
     bzcat $PORTCWD/sources/044-strtod_arm_fix.patch.bz2  | patch -p1 --fuzz=5 --verbose || failpatch
 
     # 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
}

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

# A trick from DaMouse to enable building php into $PKG.
# We'll remove this later on.
mkdir -p $PKG/etc/apache
install -m644 $PORTCWD/httpd.conf $PKG/etc/apache

################## Build the Apache module of PHP ################

# Extract the PHP source archive:
extract_php_source

echo ":::::Building Apache module:::::"

# Configure:
php_configure \
   --disable-static \
   --enable-discard-path \
   --with-apxs=/usr/sbin/apxs || failconfig

# Build:
make || failmake

# Install into package:
make install INSTALL_ROOT=$PKG || failinstall
#make distclean

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

################## Build the standalone interpreter ################

# Extract the source:
extract_php_source

echo ":::::Building standalone interpreter:::::"

# Configure:
php_configure \
   --enable-force-cgi-redirect \
   --enable-fastcgi \
   --enable-pcntl \
   --enable-sigchild || failconfig

# Build:
make || failmake

# Install into package:
make install-cli INSTALL_ROOT=$PKG
chmod 755 $PKG/usr/bin/php

# Install the CGI/FastCGI interpreter:
install -m755 sapi/cgi/php $PKG/usr/bin/php-cgi

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

# Fix $PKG/usr/lib/php perms:
find $PKG/usr/lib/php -perm 666 -print0 | xargs -0 chmod 644 

# PHP 4.4.2 seems to be throwing some junk in the root directory:
( cd $PKG
  rm -rf .channels .depdb .depdblock .filemap .lock .registry )

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a CODING_STANDARDS CREDITS EXTENSIONS INSTALL LICENSE NEWS README* TODO* \
      sapi/cgi/README.FastCGI \
      $PKG/usr/doc/$PACKAGE-$VERSION

# Install the sample config stuff:
mkdir -p $PKG/etc/apache
cp -favv php.ini-dist php.ini-recommended $PKG/etc/apache
cp -favv $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*

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
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

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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