# -*- 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                    php4
version                 4.4.9
revision                12
set major               [lindex [split ${version} .] 0]
set my_name             php${major}
dist_subdir             ${my_name}
categories              lang php www
maintainers             jwa ryandesign
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 Web development \
    and can be embedded into HTML.

checksums \
    [suffix ${distname}] \
        md5     2e3b2a0e27f10cb84fd00e5ecd7a1880 \
        sha1    ab3d2205d756ddf93452b57bd6ce6a2014b1c374 \
        rmd160  0d77092efaccd8fe6efc16635be98c5d4ccfec66

depends_build \
    port:pkgconfig

depends_lib \
    port:libiconv \
    port:expat \
    port:gettext \
    port:zlib \
    port:openssl \
    port:tiff \
    port:libxml2 \
    port:libtool \
    port:mhash \
    port:libmcrypt \
    port:curl \
    port:jpeg \
    port:libpng \
    port:freetype

# Use -p1 to accommodate the Suhosin patch
patch.pre_args          -p1
patchfiles \
    patch-ext-openssl-openssl.c.diff \
    patch-ext-standard-string.c.diff \
    patch-scripts-php-config.in.diff

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 \
    --includedir=${prefix}/include/php4 \
    --libdir=${prefix}/lib/php4 \
    --sysconfdir=${prefix}/etc/php4 \
    --with-config-file-path=${phpinidir} \
    --with-config-file-scan-dir=${extraphpinidir} \
    --with-pear=${prefix}/lib/php4 \
    --program-suffix=4 \
    --enable-mbstring \
    --enable-dbx \
    --enable-safe-mode \
    --enable-dba \
    --enable-calendar \
    --enable-exif \
    --enable-ftp \
    --enable-wddx \
    --enable-filepro \
    --enable-bcmath \
    --enable-cli \
    --with-xml \
    --with-xmlrpc \
    --without-mysql \
    --with-iconv=${prefix} \
    --with-zlib=${prefix} \
    --with-gettext=${prefix} \
    --with-expat-dir=${prefix} \
    --with-dom=${prefix} \
    --with-openssl=${prefix} \
    --with-mhash=${prefix} \
    --with-mcrypt=${prefix} \
    --with-mime-magic \
    --with-curl=${prefix} \
    --with-gd \
    --with-jpeg-dir=${prefix} \
    --with-png-dir=${prefix} \
    --enable-gd-native-ttf \
    --with-freetype-dir=${prefix}

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 apache20 fastcgi description {Don't include any web server support} {}

variant apache conflicts apache2 apache20 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
    }
}

variant apache2 conflicts apache apache20 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
}

variant apache20 conflicts apache apache2 no_web description {Add Apache 2.0 web server module} {
    pre-configure {
        # Checking for mod_cgi.so is a convenient way to verify apache20 is using its
        # +preforkmpm variant. (+workermpm instead provides mod_cgid.so.)
        if {![file exists ${prefix}/apache20/modules/mod_cgi.so]} {
            ui_error "To use ${name} with the +apache20 variant, apache20 must be installed with the +preforkmpm variant."
            return -code error "incompatible apache20 installation"
        }
    }
    destroot.violate_mtree yes
    depends_lib-append \
        port:apache20
    configure.args-append \
        --with-apxs2=${prefix}/apache20/bin/apxs
}

variant fastcgi conflicts no_web description {Add FastCGI web server binary} {
    if { ![variant_isset apache] && ![variant_isset apache2] && ![variant_isset apache20] } {
        configure.args-append \
            --enable-fastcgi \
            --enable-force-cgi-redirect
    }
}

variant mysql4 conflicts mysql5 description {Add MySQL 4 support} {
    depends_lib-append \
        port:mysql4
    configure.args-append \
        --with-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
    post-extract {
        file mkdir "${workpath}/mysql5"
        file link -symbolic "${workpath}/mysql5/lib" "${prefix}/lib/mysql5"
        file link -symbolic "${workpath}/mysql5/include" "${prefix}/include/mysql5"
    }
    
    post-destroot {
        reinplace "s;${workpath}/mysql5/lib;${prefix}/lib/mysql5;" ${destroot}${prefix}/bin/php-config4
    }
}

variant postgresql8 requires postgresql82 description {Obsolete name for postgresql82 variant} {}

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
}

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
}

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
}

variant ldap description {Add LDAP protocol support} {
    depends_lib-append \
        port:openldap
    configure.args-append \
        --with-ldap=${prefix}
}

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_build-append \
        port:cclient
    configure.args-append \
        --with-imap=${prefix} \
        --with-imap-ssl=/usr
}

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

variant xslt description {Add XSLT support using Sablotron} {
    depends_lib-append \
        port:sablotron
    configure.args-append \
        --enable-xslt \
        --with-xslt-sablot=${prefix} \
        --with-iconv-dir=${prefix}
}

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

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

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

set pdflib_distname pdflib-2.1.8
set pdflib_distfile ${pdflib_distname}.tgz
variant pdflib description {Add PDFlib support} {
    master_sites-append         http://pecl.php.net/get/:pdflib
    extract.only                ${distfiles}
    distfiles-append            ${pdflib_distfile}:pdflib
    checksums-append            ${pdflib_distfile} \
                                sha1    5d0b01dc6fe81a39e7832a4c719983ebd06f6174 \
                                rmd160  a74d2bba8985bd85fbd61589450c591665663906
    post-extract {
        delete ${worksrcpath}/ext/pdf
        system -W ${workpath} "/usr/bin/gnutar --no-same-owner -xzf ${distpath}/${pdflib_distfile}"
        move ${workpath}/${pdflib_distname} ${worksrcpath}/ext/pdf
    }
    depends_lib-append          port:pdflib
    configure.args-append       --with-pdflib=${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   ${version}-0.9.6
    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} \
            md5     c4e88782b1572e0aee26e6b2124e6257 \
            sha1    b2b5121dc1f2bb5ffed968efb978a4a1ac9fcca6 \
            rmd160  39d2e74cc879187b348f2e0dd22755680fafb49d \
}

destroot.args \
    INSTALL_ROOT=${destroot} PHP_PEAR_INSTALL_DIR=${prefix}/lib/php

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

post-destroot {
    #copy module
    if { [variant_isset apache] } {
        xinstall -m 755 -d ${destroot}${prefix}/libexec/apache \
            ${destroot}${prefix}/etc/apache/extra
        xinstall -m 755 ${worksrcpath}/libs/libphp4.so ${destroot}${prefix}/libexec/apache/
        xinstall -m 755 -c ${filespath}/mod_php.conf ${destroot}$prefix/etc/apache/extra/mod_php.conf.sample
    }
    
    if { [variant_isset apache2] } {
        xinstall -m 755 -d ${destroot}${prefix}/apache2/modules \
            ${destroot}${prefix}/apache2/conf/extra
        xinstall -m 755 ${worksrcpath}/libs/libphp4.so ${destroot}${prefix}/apache2/modules/
        xinstall -m 755 -c ${filespath}/mod_php.conf ${destroot}${prefix}/apache2/conf/extra/mod_php.conf.sample
    }
    
    if { [variant_isset apache20] } {
        xinstall -m 755 -d ${destroot}${prefix}/apache20/modules \
            ${destroot}${prefix}/apache20/conf/extra
        xinstall -m 755 ${worksrcpath}/libs/libphp4.so ${destroot}${prefix}/apache20/modules/
        xinstall -m 755 -c ${filespath}/mod_php.conf ${destroot}${prefix}/apache20/conf/extra/mod_php.conf.sample
    }
    
    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, apache2 and apache20 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
        }
        if { [variant_isset apache20] } {
            configure.args-delete \
                --with-apxs2=${prefix}/apache20/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] || [variant_isset apache20] } {
            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 ${destroot}${prefix}/bin/php-cgi${major}
    }
    
    file rename ${destroot}${prefix}/etc/php4/pear.conf ${destroot}${prefix}/etc/php4/pear.conf.sample
    
    #copy php.ini
    xinstall -m 755 -d ${destroot}${phpinidir}
    xinstall -m 644 -W ${worksrcpath} \
        php.ini-dist \
        php.ini-recommended \
        ${destroot}${phpinidir}
    
    # rename files
    file rename ${destroot}${prefix}/bin/pear ${destroot}${prefix}/bin/pear4
    reinplace "s|${prefix}/bin/php|${prefix}/bin/php4|g" ${destroot}${prefix}/bin/pear4
    
    #nuke pear-stuff in ${destroot}
    system -W ${destroot} "rm -rf .channels .depdb .depdblock .filemap .lock"
    
    system "if \[ -f ${prefix}/lib/php4/.depdblock \]; then rm -f ${destroot}${prefix}/lib/php4/.depdblock; fi"
    system "if \[ -f ${prefix}/lib/php4/.depdb \]; then rm -f ${destroot}${prefix}/lib/php4/.depdb; fi"
    system "if \[ -f ${prefix}/lib/php4/.filemap \]; then rm -f ${destroot}${prefix}/lib/php4/.filemap; fi"
    system "if \[ -f ${prefix}/lib/php4/.lock \]; then rm -f ${destroot}${prefix}/lib/php4/.lock; fi"
    system "if \[ -d ${prefix}/lib/php4/.channels \]; then rm -rf ${destroot}${prefix}/lib/php4/.channels; fi"
}

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

notes "
PHP 4 is obsolete. Please update to PHP 5 (the php5 port).\
For information on how to update your code, please see\
http://www.php.net/manual/en/migration5.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
            }
            if {[variant_isset apache20]} {
                set moduledir ${prefix}/apache20/modules
                set apxs ${prefix}/apache20/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/php4/pear.conf.sample to  ${prefix}/etc/php4/pear.conf"
}

livecheck.type          none
