# -*- 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 118105 2014-03-22 18:03:40Z ryandesign@macports.org $

PortSystem          1.0
# Untested
PortGroup           apache2 1.0

name                apache24-devel
apache2.name        apache2
version             2.4.9
categories          www
maintainers         ryandesign mathiesen.info:macintosh pixilla 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  2d8095d7ce9298d813aed849cc1cb3272a4acb5a \
                    sha256  f78cc90dfa47caf3d83ad18fd6b4e85f237777c1733fc9088594b70ce2847603

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

patch.pre_args      -p1
patchfiles          patch-build-rules.mk.in.diff \
                    patch-config.layout.diff \
                    patch-support-apxs.in.diff

post-patch {
    reinplace "s,@MANDIR@,\${prefix}/share/${apache2.name},g" \
        ${worksrcpath}/config.layout
    reinplace "s,@APACHE_NAME@,${apache2.name},g" \
        ${worksrcpath}/config.layout
}

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

configure.pre_args --prefix=${prefix}
configure.args     --with-apr=${prefix}/bin/apr-1-config \
                   --with-apr-util=${prefix}/bin/apu-1-config \
                   --enable-layout=MacPorts \
                   --with-expat=${prefix} \
                   --with-pcre=${prefix} \
                   --with-z=${prefix} \
                   --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
    }
}

post-destroot {

    move ${destroot}${apache2.sysconfdir}/httpd.conf \
        ${destroot}${apache2.sysconfdir}/httpd.conf.orig

    xinstall -m 755 -d  ${destroot}${apache2.datadir}
    xinstall -m 755 -d  ${destroot}${apache2.runtimedir}
    xinstall -m 755 -d  ${destroot}${apache2.logfiledir}
    destroot.keepdirs   ${destroot}${apache2.datadir} \
                        ${destroot}${apache2.runtimedir} \
                        ${destroot}${apache2.logfiledir}

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

    xinstall -d ${destroot}${apache2.docdir}
    xinstall -m 0644 -W ${worksrcpath} ABOUT_APACHE CHANGES LICENSE NOTICE README \
        README.platforms ROADMAP VERSIONING ${destroot}${apache2.docdir}

#     foreach {module args} [array get apache_default_modules] {
#         apxsInstall ${module} "yes" {}
#     }
}

pre-activate {
    # apache24-devel 2.4.7_0 and earlier installed these items directly to ${prefix} (bypassing ${destroot}); remove them if found
    foreach d "${apache2.datadir} ${apache2.runtimedir} ${apache2.logfiledir}" {
        set f ${d}/.turd_${name}
        if {[file exists ${f}] && [registry_file_registered ${f}] == "0"} {
            if {[catch {delete ${f}}]} {
                ui_warn "Cannot delete ${f}; please remove it manually"
            }
        }
    }
}

# post-activate {
# 
#     if {![file exists ${apache2.sbindir}/envvars]} {
#         xinstall -m 755 ${apache2.sbindir}/envvars-std ${apache2.sbindir}/envvars
#     }
# 
#     fileInstall "install" "${prefix}" "0644"
# 
#     set listenFile [open "${apache2.sysconfdir}/ports.conf" w 0644]
#     foreach listen_port ${apache2.listen_ports} {
#         puts ${listenFile} "Listen ${listen_port}"
#     }
#     close ${listenFile}
# 
#     foreach action {enmod dismod} {
#         foreach module ${apache2.default_ ${action}} {
#             exec "${apache2.sbindir}/a2${action}" ${module}
#         }
#     }
# }

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
    pre-configure {
        if {-1 == [string first -lldap [exec ${prefix}/bin/apu-1-config --libs]]} {
            ui_error "To install ${name} with the +openldap variant, apr-util must be installed"
            ui_error "with the +openldap variant. Rebuild apr-util using:"
            ui_error
            ui_error "    sudo port upgrade --enforce-variants apr-util +openldap"
            ui_error
            return -code error "incompatible apr-util installation"
        }
    }
}

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}/sbin/apachectl \] && ${prefix}/sbin/apachectl start > /dev/null"
startupitem.stop    \
"\[ -r ${prefix}/var/run/${apache2.name}/httpd.pid \] && ${prefix}/sbin/apachectl stop > /dev/null"
startupitem.restart \
"${prefix}/sbin/apachectl graceful > /dev/null"

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