# -*- 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$

PortSystem          1.0
PortGroup           active_variants 1.1

name                apache2
version             2.2.29
categories          www
maintainers         ryandesign openmaintainer
license             Apache-2
description         The extremely popular second version of the Apache http server 
long_description \
    Apache is an HTTP server designed as a plug-in replacement for \
    the NCSA server version 1.3 (or 1.4). It fixes numerous bugs in \
    the NCSA server and includes many frequently requested new \
    features, and has an API which allows it to be extended to meet \
    users' needs more easily.
homepage            http://httpd.apache.org/
platforms           darwin freebsd openbsd

master_sites        apache:httpd
distname            httpd-${version}
use_bzip2           yes
use_parallel_build  yes

checksums           rmd160  c9a823f038a6a1cbfd94cd9bdd067edd26cf7a3b \
                    sha256  574b4f994b99178dfd5160bcb14025402e2ce381be9889b83e4be0ffbf5839a4

depends_lib         port:apr \
                    port:apr-util \
                    port:expat \
                    port:openssl \
                    port:pcre \
                    port:perl5 \
                    port:zlib

# Both apache and apache2 want to install dbmmanage.1 and other manpages.
conflicts           apache thttpd

patchfiles          patch-docs-cgi-examples-printenv.diff \
                    libtool-tag.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" ${worksrcpath}/docs/cgi-examples/printenv
    reinplace -E "s/(User|Group) daemon/\\1 www/" ${worksrcpath}/docs/conf/httpd.conf.in
}

platform darwin {
    post-patch {
        # Customize userdir naming to match darwin
        reinplace "s|/home/|/Users/|g" ${worksrcpath}/docs/conf/extra/httpd-userdir.conf.in
        reinplace "s|public_html|Sites|g" ${worksrcpath}/docs/conf/extra/httpd-userdir.conf.in
    }

    # HFS can be case insensitive, so protect for this as in 
    # http://docs.info.apple.com/article.html?artnum=300422
    patchfiles-append   patch-httpd.conf.in-sensitive-files.diff
}

platform darwin 9 {
    post-extract {
        reinplace "s|-export-symbols-regex ssl_module||g" ${worksrcpath}/configure
    }
}

configure.pre_args --prefix=${prefix}/${name}
configure.args     --with-apr=${prefix}/bin/apr-1-config \
                   --with-apr-util=${prefix}/bin/apu-1-config \
                   --libdir=${prefix}/lib \
                   --with-expat=${prefix} \
                   --with-pcre=${prefix} \
                   --with-z=${prefix} \
                   --mandir=${prefix}/share/man \
                   --enable-mods-shared=all \
                   --enable-authn-alias=shared \
                   --enable-ssl \
                   --with-ssl=${prefix} \
                   --enable-cache \
                   --enable-disk_cache \
                   --enable-file_cache \
                   --enable-mem_cache \
                   --enable-deflate \
                   --enable-proxy \
                   --enable-proxy-connect \
                   --enable-proxy-http \
                   --enable-proxy-ftp

post-configure {
    # Setting DYLD_FALLBACK_LIBRARY_PATH instead of DYLD_LIBRARY_PATH
    # avoids issues with library collisions (bug #3013)
    reinplace "s|DYLD_LIBRARY_PATH|DYLD_FALLBACK_LIBRARY_PATH|g" \
        ${worksrcpath}/support/envvars-std
}

post-build {
    reinplace -E {s| -arch [a-z0-9_]+||g} \
        ${worksrcpath}/config.nice \
        ${worksrcpath}/build/config_vars.mk
    if {[tbool configure.ccache]} {
        reinplace -E {s|ccache ||g} \
            ${worksrcpath}/config.nice \
            ${worksrcpath}/build/config_vars.mk
    }
}

destroot.violate_mtree yes

post-destroot {
    set confDir ${destroot}${prefix}/${name}/conf
    eval delete ${destroot}${prefix}/${name}/bin/envvars \
                ${confDir}/httpd.conf \
                [glob -directory ${confDir}/extra *.conf]

    move ${destroot}${prefix}/${name}/htdocs/index.html ${destroot}${prefix}/${name}/htdocs/index.html.sample

    destroot.keepdirs ${destroot}${prefix}/${name}/logs \
                      ${confDir}/extra

    # fix libtool path
    reinplace "s|/${name}/build/libtool|/share/apr-1/build/libtool|g" \
        ${destroot}${prefix}/${name}/build/config_vars.mk

    set docdir ${prefix}/share/doc/apache-${version}
    xinstall -d ${destroot}${docdir}
    xinstall -m 0644 -W ${worksrcpath} ABOUT_APACHE CHANGES LICENSE NOTICE README \
        README.platforms ROADMAP VERSIONING ${destroot}${docdir}
}

post-activate {
    set confDir ${prefix}/${name}/conf
    # Copy into place sample or default versions of files user is likely to modify
    if {![file exists ${prefix}/${name}/htdocs/index.html]} {
        file copy ${prefix}/${name}/htdocs/index.html.sample ${prefix}/${name}/htdocs/index.html
    }
    if {![file exists ${prefix}/${name}/bin/envvars]} {
        xinstall -m 755 ${prefix}/${name}/bin/envvars-std ${prefix}/${name}/bin/envvars
    }
    if {![file exists ${confDir}/httpd.conf]} {
        file copy ${confDir}/original/httpd.conf ${confDir}/httpd.conf
    }
    foreach f [glob -tails -directory ${confDir}/original/extra *.conf] {
        if {![file exists ${confDir}/extra/${f}]} {
            file copy ${confDir}/original/extra/${f} ${confDir}/extra/${f}
        }
    }
}

platform openbsd {
    build.env-append "LD_LIBRARY_PATH=${prefix}/lib"
}

variant openldap description {Enable LDAP support through OpenLDAP} {
    configure.args-append --enable-ldap --enable-authnz-ldap
    depends_lib-append port:openldap
    require_active_variants apr-util openldap
}

variant preforkmpm conflicts workermpm eventmpm description {Use prefork MPM} {
    configure.args-append --with-mpm=prefork
}

variant workermpm conflicts preforkmpm eventmpm description {Use worker MPM} {
    configure.args-append --with-mpm=worker
}

variant eventmpm conflicts preforkmpm workermpm description {Use event MPM (experimental)} {
    configure.args-append --with-mpm=event
}

if {![variant_isset workermpm] && ![variant_isset eventmpm]} {
    default_variants +preforkmpm
}

startupitem.create    yes
startupitem.start    \
"\[ -x ${prefix}/${name}/bin/apachectl \] && ${prefix}/${name}/bin/apachectl start > /dev/null"
startupitem.stop    \
"\[ -r ${prefix}/${name}/logs/httpd.pid \] && ${prefix}/${name}/bin/apachectl stop > /dev/null"
startupitem.restart    \
"${prefix}/${name}/bin/apachectl restart > /dev/null"

livecheck.type  regex
livecheck.url   ${homepage}download.cgi
livecheck.regex httpd-(\[0-9.\]+)${extract.suffix}