pkgbase=php
pkgname=('php' 'php-cgi' 'php-pear')
pkgver=5.3.3
pkgrel=3
_suhosinver=${pkgver}-0.9.10
pkgdesc='A high-level scripting language'
arch=('i686' 'x86_64')
license=('PHP')
url='http://www.php.net'
backup=('etc/php/php.ini')
install=php.install
depends=('glibc' 'readline' 'ncurses' 'libxml2' 'pcre')
#makedepends=('apache' 'imap' 'postgresql-libs' 'mysql' 'libldap' \
#             'libpng' 'libjpeg' 'sqlite3' 'unixodbc' 'net-snmp' \
#       	'libmcrypt' 'tidyhtml' 'aspell' 'libtool' 'freetype2' 'libjpeg' \
#             'curl' 'libxslt' 'pam' 'openssl' 'bzip2' 'gdbm' 'db>=4.7')

makedepends=('mysql' 'libpng' 'libjpeg' 'sqlite3' 'libmcrypt' 'libtool' \
             'freetype2' 'libjpeg' 'curl' 'libxslt' 'pam' 'openssl' 'bzip2' \
             'gdbm' 'db>=4.8.26')
options=('emptydirs')
source=("http://www.php.net/distributions/${pkgname}-${pkgver}.tar.bz2" \
        "http://download.suhosin.org/suhosin-patch-${_suhosinver}.patch.gz" \
        'php.ini' 'apache.conf')

build() {
	phpconfig="--srcdir=${srcdir}/${pkgbase}-${pkgver} \
        --prefix=/usr \
	--sysconfdir=/etc/php \
	--with-layout=GNU \
	--with-config-file-path=/etc/php \
	--with-config-file-scan-dir=/etc/php/conf.d \
	--enable-inline-optimization \
	--disable-debug \
	--disable-rpath \
	--disable-static \
	--enable-shared \
        --mandir=/usr/share/man \
        --without-pear \
	"

	phpextensions="--with-openssl=shared \
	--with-zlib=shared \
	--enable-bcmath=shared \
	--with-bz2=shared \
	--enable-calendar=shared \
	--with-curl=shared \
	--enable-dba=shared \
	--without-db2 \
	--without-db3 \
	--with-db4=shared \
	--with-gdbm=shared \
	--enable-exif=shared \
	--enable-ftp=shared \
	--with-gd=shared \
	--enable-gd-native-ttf \
	--with-jpeg-dir=shared,/usr \
	--with-png-dir=shared,/usr \
	--with-gettext=shared \
	--without-imap \
	--without-imap-ssl \
	--without-ldap \
	--enable-mbstring=shared \
	--with-mcrypt=shared \
	--with-mysql=shared \
	--with-mysql-sock=/tmp/mysql.sock \
	--with-mysql=shared \
	--with-mysqli=shared \
	--without-unixODBC \
	--enable-pdo=shared \
	--with-pdo-mysql=shared \
	--with-pdo-sqlite=shared \
	--without-pdo-odbc \
	--without-pdo-pgsql \
	--with-sqlite=shared \
	--without-pgsql \
	--enable-shmop=shared \
	--without-snmp \
	--enable-soap=shared \
	--enable-sqlite-utf8=shared \
	--enable-sysvmsg=shared \
	--enable-sysvsem=shared \
	--enable-sysvshm=shared \
	--without-tidy \
	--with-xsl=shared \
	--enable-zip=shared \
	--enable-posix=shared \
	--enable-sockets=shared \
	--enable-xml \
	--enable-session=shared \
	--with-regex=php \
	--with-pcre-regex=/usr \
	--enable-mbstring=all \
	--enable-mbregex \
	--enable-json=shared \
	--with-iconv=shared \
	--with-xmlrpc=shared \
	--without-pspell \
	--with-freetype-dir=shared,/usr \
	"
        EXTENSION_DIR=/usr/lib/php/modules
        export EXTENSION_DIR
        PEAR_INSTALLDIR=/usr/share/pear
        export PEAR_INSTALLDIR

	cd ${srcdir}/${pkgbase}-${pkgver}

	# apply suhosin patch
	patch -p1 -i ${srcdir}/suhosin-patch-${_suhosinver}.patch || return 1

	# php
        mkdir ${srcdir}/build-php
        cd ${srcdir}/build-php
        ln -s ../${pkgbase}-${pkgver}/configure
	./configure ${phpconfig} \
		--disable-cgi \
		--with-readline \
		--enable-pcntl \
		${phpextensions} || return 1
	make || return 1

	# cgi and fcgi
        cp -a ${srcdir}/build-php ${srcdir}/build-cgi
        cd ${srcdir}/build-cgi
	./configure ${phpconfig} \
                --disable-cli \
                --enable-cgi \
		${phpextensions} || return 1
	make || return 1

        # pear
        cp -a ${srcdir}/build-php ${srcdir}/build-pear
        cd ${srcdir}/build-pear
        ./configure ${phpconfig} \
                --disable-cgi \
                --with-readline \
                --enable-pcntl \
                --with-pear \
                ${phpextensions}
        make || return 1
}

package_php() {
        pkgdesc='An HTML-embedded scripting language'
        depends=('pcre' 'libxml2' 'bzip2' 'openssl')
        replaces=('php')
        provides=('php')
        backup=('etc/php/php.ini')

	cd ${srcdir}/build-php
	make INSTALL_ROOT=${pkgdir} install || return 1
        install -d -m755 ${pkgdir}/usr/share/pear
        # fix broken link
        ln -sf phar.phar $pkgdir/usr/bin/phar
        # install php.ini
        install -D -m644 ${srcdir}/php.ini ${pkgdir}/etc/php/php.ini
        install -d -m755 ${pkgdir}/etc/php/conf.d/

        # remove static modules
        rm -f ${pkgdir}/usr/lib/php/modules/*.a
        # remove modules provided by sub packages
        rm -f ${pkgdir}/usr/lib/php/modules/{curl,enchant,gd,gmp,intl,ldap,mcrypt,mssql,odbc,pdo_odbc,pgsql,pdo_pgsql,pspell,snmp,sqlite3,pdo_sqlite,tidy,xsl}.so
}

package_php-cgi() {
        pkgdesc='CGI and FCGI SAPI for PHP'
        depends=('php' 'pcre' 'libxml2')

        install -D -m755 ${srcdir}/build-cgi/sapi/cgi/php-cgi ${pkgdir}/usr/bin/php-cgi
}

package_php-pear() {
        pkgdesc='PHP Extension and Application Repository'
        depends=('php' 'bash')
        backup=('etc/php/pear.conf')

        cd ${srcdir}/build-pear
        make install-pear INSTALL_ROOT=${pkgdir} || return 1
        local i
        while read i; do
                [ ! -e "$i" ] || rm -rf "$i"
        done < <(find ${pkgdir} -name '.*')
}

md5sums=('21ceeeb232813c10283a5ca1b4c87b48'
         'b66b27c43b1332400ef8982944c3b95b'
         '8ced1a21457e3d513425617667eb8588'
         '96ca078be6729b665be8a865535a97bf')