# -*- coding: utf-8; mode: tcl; tab-width: 4; truncate-lines: t; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4
# $Id: Portfile 65768 2010-03-31 16:53:23Z wsiegrist@apple.com $

PortSystem       1.0

name             apache2
version          2.2.15
revision         1
categories       www
maintainers      imajes
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           md5     016cec97337eccead2aad6a7c27f2e14 \
                    sha1    5f0e973839ed2e38a4d03adba109ef5ce3381bc2 \
                    rmd160  e5c5da1fdf86a6b0501f6c8e97ccb1982e81cfdf

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

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

post-patch {
    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 7 {
	pre-configure {
		system "cd ${worksrcpath} && glibtoolize --force"
	}
}

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} \
                   --mandir=${prefix}/share/man \
                   --enable-mods-shared=all \
                   --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

configure.ccache no

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
}

destroot.violate_mtree yes

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

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

	# fix libtool path
    reinplace "s|/apache2/build/libtool|/share/apr-1/build/libtool|g" \
		${destroot}${prefix}/apache2/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}/apache2/conf
    # Make sure initial conf files are present and set up correctly
    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
	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
}

variant no_startupitem description {Do not create a startup item} {
	startupitem.create	no
}

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

livecheck.type  regex
livecheck.url   ${homepage}
livecheck.regex {Apache HTTP Server (\d+(?:\.\d+)*) Released}

