# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 92995 2012-05-13 18:25:30Z ryandesign@macports.org $

PortSystem              1.0

name                    php52
conflicts               php5 php5-devel php5-gd php5-iconv php5-mbstring php5-sqlite
version                 5.2.17
revision                5
set major               [lindex [split ${version} .] 0]
set my_name             php${major}
dist_subdir             ${my_name}
categories              lang php www
maintainers             ryandesign jwa
homepage                http://www.php.net/
master_sites            php
distname                php-${version}
use_bzip2               yes
platforms               darwin freebsd
use_parallel_build      yes

description \
    PHP: Hypertext Preprocessor

long_description \
    PHP is a widely-used general-purpose scripting language \
    that is especially suited for developing web sites, but can also \
    be used for command-line scripting.

checksums               [suffix ${distname}] \
                        sha1    d68f3b09f766990d815a3c4c63c157db8dab8095 \
                        rmd160  567fa8d718b93fb83a89494c83a8bec224ac99e9

depends_build \
    port:pkgconfig \
    port:autoconf213

depends_lib \
    path:bin/gsed:gsed \
    port:libtool \
    port:libxml2 \
    port:libxslt \
    port:openssl \
    port:zlib \
    port:bzip2 \
    port:libiconv \
    port:expat \
    port:gettext \
    port:tiff \
    port:mhash \
    port:libmcrypt \
    port:curl \
    port:pcre \
    port:jpeg \
    port:libpng \
    port:freetype

# Use -p1 to accommodate the Suhosin patch
patch.pre_args          -p1
patchfiles \
    patch-scripts-php-config.in.diff \
    patch-pcre-8.30.diff \
    patch-bind.diff

use_autoconf            yes
autoconf.cmd            ${prefix}/bin/autoconf213

set phpinidir           ${prefix}/etc/${my_name}
set extraphpinidir      ${prefix}/var/db/${my_name}

destroot.keepdirs       ${destroot}${extraphpinidir}

configure.args \
    --mandir=${prefix}/share/man \
    --infodir=${prefix}/share/info \
    --with-config-file-path=${phpinidir} \
    --with-config-file-scan-dir=${extraphpinidir} \
    --enable-calendar \
    --with-iconv=${prefix} \
    --enable-exif \
    --enable-ftp \
    --enable-wddx\
    --with-zlib=${prefix} \
    --with-bz2=${prefix} \
    --without-mysql \
    --without-sqlite \
    --without-pdo-sqlite \
    --with-libxml-dir=${prefix} \
    --with-gettext=${prefix} \
    --with-libexpat-dir=${prefix} \
    --with-xmlrpc \
    --enable-soap \
    --enable-bcmath \
    --enable-mbstring \
    --enable-dba \
    --enable-zip \
    --with-openssl=${prefix} \
    --with-mhash=${prefix} \
    --with-mcrypt=${prefix} \
    --with-mime-magic \
    --with-xsl=${prefix} \
    --with-curl=${prefix} \
    --with-pcre-regex=${prefix} \
    --with-gd \
    --with-jpeg-dir=${prefix} \
    --with-png-dir=${prefix} \
    --enable-gd-native-ttf \
    --without-pear \
    --with-freetype-dir=${prefix}

configure.universal_args-delete --disable-dependency-tracking

if {${os.platform} == "darwin" && ${os.major} >= 10} {
    configure.env-append \
        LIBS=-lresolv
}

platform macosx {
    configure.args-append \
        --with-ldap=/usr \
        --with-kerberos=/usr \
        --with-iodbc=/usr
}

variant no_web conflicts apache apache2 fastcgi description {Don't include any web server support} {}

# Build an Apache 1 module. On Mac OS X, it uses Apple's provided Apache 1 server.
# On other platforms, the MacPorts apache port is used. Keep the options here
# in sync with those in the relevant part of the post-destroot phase.
variant apache conflicts apache2 no_web description {Add Apache 1 web server module} {
    if { ! [variant_isset macosx] && (![info exists os.subplatform] || ${os.subplatform} != "macosx") } {
        depends_lib-append \
            port:apache
        configure.args-append \
            --with-apxs=${prefix}/sbin/apxs
    } else {
        destroot.violate_mtree yes
        configure.args-append \
            --with-apxs=/usr/sbin/apxs
    }
}

# Build an Apache 2.2 module. Keep the options here in sync with those in the
# relevant part of the post-destroot phase.
variant apache2 conflicts apache no_web description {Add Apache 2.2 web server module} {
    pre-configure {
        # Checking for mod_cgi.so is a convenient way to verify apache2 is using its
        # +preforkmpm variant. (+eventmpm and +workermpm instead provide mod_cgid.so.)
        if {![file exists ${prefix}/apache2/modules/mod_cgi.so]} {
            ui_error "To use ${name} with the +apache2 variant, apache2 must be installed with the +preforkmpm variant."
            return -code error "incompatible apache2 installation"
        }
    }
    destroot.violate_mtree yes
    depends_lib-append \
        port:apache2
    configure.args-append \
        --with-apxs2=${prefix}/apache2/bin/apxs
}

# Build a FastCGI binary. Keep the options here in sync with those in the
# relevant part of the post-destroot phase.
variant fastcgi conflicts no_web description {Add FastCGI web server binary} {
    if { ![variant_isset apache] && ![variant_isset apache2] } {
        configure.args-append \
            --enable-fastcgi \
            --enable-force-cgi-redirect
    }
}

variant gmp description {Add GNU MP multiprocessing functions} {
    depends_lib-append \
        port:gmp
    configure.args-append \
        --with-gmp=${prefix}
}

variant dbase description {Add dBase file format support} {
    configure.args-append \
        --enable-dbase
}

variant imap description {Add IMAP protocol support} {
    depends_lib-append \
        port:cclient
    configure.args-append \
        --with-imap=${prefix} \
        --with-imap-ssl=${prefix}
}

variant pspell description {Add pspell spell-checking functions} {
    depends_lib-append \
        port:aspell
    configure.args-append \
        --with-pspell=${prefix}
}

variant tidy description {Add Tidy support} {
    depends_lib-append \
        port:tidy
    configure.args-append \
        --with-tidy=${prefix}
}

variant mssql description {Add MS-SQL server support} {
    depends_lib-append \
        port:freetds
    configure.args-append \
        --with-mssql=${prefix} \
        --with-pdo-dblib=${prefix}
    
    # See http://bugs.php.net/bug.php?id=44991
    post-patch {
        reinplace "s|tds.h|sybdb.h|g"               ${worksrcpath}/ext/mssql/config.m4
        reinplace "s|libtds.a|libsybdb.a|g"         ${worksrcpath}/ext/mssql/config.m4
        reinplace "s|libtds.so|libsybdb.dylib|g"    ${worksrcpath}/ext/mssql/config.m4
    }
}

variant snmp conflicts macports_snmp description {Add SNMP support using Apple SNMP} {
    # This compiles PHP5 with SNMP linked against Apple's included NET-SNMP.
    configure.args-append \
        --with-snmp=/usr
}

variant macports_snmp conflicts snmp description {Add SNMP support using MacPorts SNMP} {
    # This compiles PHP with SNMP linked against MacPorts' NET-SNMP.
    depends_lib-append \
        port:net-snmp
    configure.args-append \
        --with-snmp=${prefix}
}

variant mysql4 conflicts mysql5 description {Add MySQL 4 support} {
    depends_lib-append \
        port:mysql4
    configure.args-delete \
        --without-mysql
    configure.args-append \
        --with-mysql=${prefix} \
        --with-pdo-mysql=${prefix}
}

variant mysql5 conflicts mysql4 description {Add MySQL 5 support} {
    depends_lib-append \
        path:bin/mysql_config5:mysql5
    configure.args-delete \
        --without-mysql
    configure.args-append \
        --with-mysql=${workpath}/mysql5 \
        --with-pdo-mysql=${prefix}/bin/mysql_config5 \
        --with-mysql-sock=${prefix}/var/run/mysql5/mysqld.sock \
        --with-mysqli=${prefix}/bin/mysql_config5
    post-extract {
        file mkdir "${workpath}/mysql5"
        file link -symbolic "${workpath}/mysql5/lib" "${prefix}/lib/mysql5"
        file link -symbolic "${workpath}/mysql5/include" "${prefix}/include/mysql5"
    }
}

variant oracle description {Add Oracle oci8 database functions with the Oracle Instant Client} {
    patchfiles-append \
        patch-ext-pdo_oci-config.m4.diff
    depends_lib-append \
        port:oracle-instantclient
    configure.args-append \
        --with-oci8=instantclient,${prefix}/lib/oracle \
        --with-pdo-oci=instantclient,${prefix}/lib/oracle,10.1
}

variant postgresql82 conflicts postgresql83 postgresql84 description {Add PostgreSQL 8.2 support} {
    depends_lib-append \
        port:postgresql82
    configure.args-append \
        --with-pgsql=${prefix}/lib/postgresql82/bin \
        --with-pdo-pgsql=${prefix}/lib/postgresql82/bin
}

variant postgresql83 conflicts postgresql82 postgresql84 description {Add PostgreSQL 8.3 support} {
    depends_lib-append \
        port:postgresql83
    configure.args-append \
        --with-pgsql=${prefix}/lib/postgresql83/bin \
        --with-pdo-pgsql=${prefix}/lib/postgresql83/bin
}

variant postgresql84 conflicts postgresql82 postgresql83 description {Add PostgreSQL 8.4 support} {
    depends_lib-append \
        port:postgresql84
    configure.args-append \
        --with-pgsql=${prefix}/lib/postgresql84/bin \
        --with-pdo-pgsql=${prefix}/lib/postgresql84/bin
}

# note that standard sqlite support uses the built-in sqlite2 libraries
# while pdo sqlite support uses the sqlite3 libraries from the sqlite3 port
variant sqlite description {Add SQLite support} {
    depends_lib-append \
        port:sqlite3
    configure.args-delete \
        --without-sqlite \
        --without-pdo-sqlite
    configure.args-append \
        --with-sqlite \
        --with-pdo-sqlite=${prefix} \
        --enable-sqlite-utf8
}

variant ipc description {Add semaphore, shared memory and IPC functions} {
    configure.args-append \
        --enable-shmop \
        --enable-sysvsem \
        --enable-sysvshm \
        --enable-sysvmsg
}

variant pcntl description {Add process control functions} {
    configure.args-append \
        --enable-pcntl
}

if {![variant_isset apache] && ![variant_isset apache2] && ![variant_isset fastcgi] && ![variant_isset no_web]} {
    default_variants +apache2
}

variant pear description {Add PEAR} {
    configure.args-delete \
        --without-pear
    configure.args-append \
        --with-pear=${prefix}/lib/php
    destroot.target-append \
        install-pear
    destroot.args-append \
        PHP_PEAR_INSTALL_DIR=${prefix}/lib/php
    post-destroot {
        #nuke pear-stuff in ${destroot}
        system -W ${destroot} "rm -rf .channels .depdb .depdblock .filemap .lock"
        
        system "if \[ -f ${prefix}/lib/php/.depdblock \]; then rm -f ${destroot}${prefix}/lib/php/.depdblock; fi"
        system "if \[ -f ${prefix}/lib/php/.depdb \]; then rm -f ${destroot}${prefix}/lib/php/.depdb; fi"
        system "if \[ -f ${prefix}/lib/php/.filemap \]; then rm -f ${destroot}${prefix}/lib/php/.filemap; fi"
        system "if \[ -f ${prefix}/lib/php/.lock \]; then rm -f ${destroot}${prefix}/lib/php/.lock; fi"
        system "if \[ -d ${prefix}/lib/php/.channels \]; then rm -rf ${destroot}${prefix}/lib/php/.channels; fi"
    }
}

variant readline description {Add GNU readline functions} {
    depends_lib-append \
        port:readline
    configure.args-append \
        --with-readline=${prefix}
}

variant sockets description {Add socket communication functions} {
    configure.args-append \
        --enable-sockets
}

variant t1lib description {Add PostScript Type 1 font support with t1lib} {
    depends_lib-append \
        port:t1lib
    configure.args-append \
        --with-t1lib=${prefix}
}

variant suhosin description {Add Suhosin patch} {
    pre-fetch {
        if {"darwin" == ${os.platform} && ${os.major} < 9} {
            ui_error "The suhosin variant requires Mac OS X 10.5 or greater."
            return -code error "incompatible Mac OS X version"
        }
    }
    set suhosin_patch_version   5.2.16-0.9.7
    set suhosin_patch           suhosin-patch-${suhosin_patch_version}.patch.gz
    patch_sites-append          http://download.suhosin.org/
    patchfiles-append           ${suhosin_patch}
    checksums-append            ${suhosin_patch} \
                                sha1    fec10b2b81582d06bb0d0a96ea55c525afc8ab29 \
                                rmd160  b28b70faf136b3e04c5b483da0f4c2279378f43a \
}

destroot.args \
    INSTALL_ROOT=${destroot}

destroot.target \
    install-cli install-build install-headers install-programs

post-destroot {
    # Copy the Apache 1 module.
    if { [variant_isset apache] } {
        xinstall -m 755 -d ${destroot}${prefix}/libexec/apache \
            ${destroot}${prefix}/etc/apache/extra
        xinstall -m 755 ${worksrcpath}/libs/libphp5.so ${destroot}${prefix}/libexec/apache/
        xinstall -m 755 -c ${filespath}/mod_php.conf ${destroot}$prefix/etc/apache/extra
    }
    
    # Copy the Apache 2.2 module.
    if { [variant_isset apache2] } {
        xinstall -m 755 -d ${destroot}${prefix}/apache2/modules \
            ${destroot}${prefix}/apache2/conf/extra
        xinstall -m 755 ${worksrcpath}/libs/libphp5.so ${destroot}${prefix}/apache2/modules/
        xinstall -m 755 -c ${filespath}/mod_php.conf ${destroot}${prefix}/apache2/conf/extra
    }
    
    if { [variant_isset fastcgi] } {
        # If we've built an Apache module (any version) then the FastCGI binary
        # will not have been built, so we need to run through the whole process
        # again and build just the FastCGI binary. Keep the options here in sync
        # with the options specified in the apache and apache2 variants.
        if { [variant_isset apache] } {
            if { ![variant_isset macosx] && (![info exists os.subplatform] || ${os.subplatform} != "macosx") } {
                configure.args-delete \
                    --with-apxs=${prefix}/sbin/apxs
            } else {
                configure.args-delete \
                    --with-apxs=/usr/sbin/apxs
            }
        }
        if { [variant_isset apache2] } {
            configure.args-delete \
                --with-apxs2=${prefix}/apache2/bin/apxs
        }
        
        # Run the build again to get the FastCGI binary. Keep the options here
        # in sync with those in the fastcgi variant.
        if { [variant_isset apache] || [variant_isset apache2] } {
            configure.args-append \
                --enable-fastcgi \
                --enable-force-cgi-redirect
            ui_msg "$UI_PREFIX Configuring ${name} again for fastcgi"
            command_exec configure
            ui_msg "$UI_PREFIX Building ${name} again for fastcgi"
            command_exec build
            ui_msg "$UI_PREFIX Staging ${name} fastcgi into destroot"
        }
        
        # Copy the FastCGI binary to the bin dir under a new name so it doesn't
        # conflict with the cli version.
        xinstall -m 755 ${worksrcpath}/sapi/cgi/php-cgi ${destroot}${prefix}/bin
    }
    
    #file rename ${destroot}${prefix}/etc/pear.conf ${destroot}${prefix}/etc/pear.conf.sample
    
    # Copy the default php.ini files.
    xinstall -m 755 -d ${destroot}${phpinidir}
    xinstall -m 644 -W ${worksrcpath} \
        php.ini-dist \
        php.ini-recommended \
        ${destroot}${phpinidir}
}

pre-activate {
    set filepath ${extraphpinidir}/.turd_${name}
    if {[file exists ${filepath}]} {
        delete ${filepath}
    }
}

notes "
PHP 5.2 is obsolete. Please update to PHP 5.3 (the php5 port).\
For information on how to update your code, please see\
http://www.php.net/manual/en/migration53.php
"

post-activate {
    if {[file exists ${prefix}/etc/php.ini] && ![file exists ${phpinidir}/php.ini]} {
        move ${prefix}/etc/php.ini ${phpinidir}
        ui_msg "Your ${prefix}/etc/php.ini has been moved to ${phpinidir}/php.ini"
        ui_msg ""
    }
    if {![file exists ${phpinidir}/php.ini]} {
        ui_msg "To customize php, copy"
        ui_msg "${phpinidir}/php.ini-dist (if this is a development server) or"
        ui_msg "${phpinidir}/php.ini-recommended (if this is a production server) to"
        ui_msg "${phpinidir}/php.ini and then make changes."
    } else {
        ui_msg "You may need to update your php.ini for any changes that have been made"
        ui_msg "in this version of php. Compare ${phpinidir}/php.ini with"
        ui_msg "${phpinidir}/php.ini-dist (if this is a development server) or"
        ui_msg "${phpinidir}/php.ini-recommended (if this is a production server)."
    }
    
    if {![variant_isset no_web]} {
        ui_msg ""
        ui_msg "If this is your first install, you need to activate PHP in your web server."
        if {![variant_isset fastcgi]} {
            if {[variant_isset apache]} {
                set moduledir ${prefix}/libexec/modules
                set apxs ${prefix}/apache/bin/apxs
            }
            if {[variant_isset apache2]} {
                set moduledir ${prefix}/apache2/modules
                set apxs ${prefix}/apache2/bin/apxs
            }
            ui_msg ""
            ui_msg "To enable PHP in Apache, run"
            ui_msg "  cd ${moduledir}"
            ui_msg "  ${apxs} -a -e -n \"${my_name}\" lib${my_name}.so"
        }
    }
    
    #ui_msg "* copy  ${prefix}/etc/pear.conf.sample to  ${prefix}/etc/pear.conf"
}

variant debug description {Enable debug support (useful to analyze a PHP-related core dump)} {
    configure.args-append   --enable-debug
}

test.run                yes

livecheck.type          none
