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

PortSystem          1.0
PortGroup           apache2 1.0

name                apache2
version             2.4.63
revision            0
checksums           rmd160  851f634398dbaa98e975db51269d008cc7a635f8 \
                    sha256  88fc236ab99b2864b248de7d49a008ec2afd7551e64dce8b95f58f32f94c46ab \
                    size    7517972

categories          www
maintainers         {ryandesign @ryandesign} {mps @Schamschula} 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            https://httpd.apache.org/
platforms           darwin freebsd openbsd

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

depends_lib         port:apr \
                    port:apr-util \
                    port:expat \
                    path:lib/libssl.dylib:openssl \
                    port:libxml2 \
                    port:pcre \
                    port:perl5 \
                    port:zlib

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

post-patch {
    reinplace "s,@MANDIR@,\${prefix}/share/${name},g" \
        ${worksrcpath}/config.layout
    reinplace "s,@APACHE_NAME@,${name},g" \
        ${worksrcpath}/config.layout
    reinplace -E "s/(User|Group) daemon/\\1 _www/" \
        ${worksrcpath}/docs/conf/httpd.conf.in
}

platform darwin {
    patchfiles-append   patch-support-apachectl.in.diff

    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-docs-conf-httpd.conf.in.diff
}

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

# https://trac.macports.org/wiki/WimplicitFunctionDeclaration#strchr
configure.checks.implicit_function_declaration.whitelist-append strchr

compiler.thread_local_storage yes

# Avoid inheriting CPP value from apr
configure.env       "CPP=${configure.cc} -E"

configure.pre_args --prefix=${prefix}
configure.args     --enable-authn-alias=shared \
                   --enable-cache \
                   --enable-deflate \
                   --enable-disk_cache \
                   --enable-file_cache \
                   --enable-layout=MacPorts \
                   --enable-mem_cache \
                   --enable-mods-shared=all \
                   --enable-proxy \
                   --enable-ssl \
                   --enable-proxy-connect \
                   --enable-proxy-ftp \
                   --enable-proxy-http \
                   --with-apr=${prefix}/bin/apr-1-config \
                   --with-apr-util=${prefix}/bin/apu-1-config \
                   --with-expat=${prefix} \
                   --with-libxml2=${prefix} \
                   --with-pcre=${prefix} \
                   --with-ssl=${prefix} \
                   --with-z=${prefix} \
                   ac_cv_path_RSYNC=/usr/bin/rsync

# explicitly disable modules that may build opportunistically
configure.args-append \
                   --disable-brotli \
                   --disable-lua

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
    foreach f [glob -tails -directory ${destroot}${apache2.sysconfdir}/extra/ *] {
        move ${destroot}${apache2.sysconfdir}/extra/${f} \
            ${destroot}${apache2.sysconfdir}/extra/${f}.orig
    }
    delete ${destroot}${apache2.sysconfdir}/original

    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}

    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" {}
#     }
}

post-activate {
    if {![file exists ${apache2.sysconfdir}/httpd.conf]} {
        file copy ${apache2.sysconfdir}/httpd.conf.orig ${apache2.sysconfdir}/httpd.conf
        reinplace "s|#LoadModule slotmem_shm_module|LoadModule slotmem_shm_module|g" \
        ${apache2.sysconfdir}/httpd.conf
    }

#
#   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 path:lib/libldap.dylib: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 brotli description {Build brotli module} {
    depends_lib-append      port:brotli
    configure.args-replace  --disable-brotli --enable-brotli
    configure.args-append   --with-brotli=${prefix}
}

variant http2 description {Enable HTTP/2 support} {
    depends_lib-append      port:nghttp2
    configure.args-append   --enable-http2
}

variant xml2enc description {Enable i18n support for markup filters} {
    configure.args-append   --enable-xml2enc --with-libxml2=${prefix}
    depends_lib-append      port:libxml2
}

variant mpms_shared_all description {Build all MPM's as DSO for dynamic loading} {
    configure.args-append   --enable-mpms-shared=all
}

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

notes "
For information about configuring Apache2, see
https://trac.macports.org/wiki/howto/Apache2
"

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