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

name                        lighttpd
version                     1.4.30
set branch                  [join [lrange [split ${version} .] 0 1] .]
categories                  www
platforms                   darwin
maintainers                 ryandesign
license                     BSD

description                 A secure, fast, compliant and flexible web-server

long_description            lighttpd is a secure, fast, compliant and very \
                            flexible web server which has been optimized for \
                            high-performance environments. It has a very low \
                            memory footprint compared to other web servers \
                            and takes care of CPU load.

homepage                    http://www.lighttpd.net/
master_sites                http://download.lighttpd.net/lighttpd/releases-${branch}.x/
use_bzip2                   yes

checksums                   rmd160  3d54d76c8530c556e13bf189ba85ecf6a0e8e38d \
                            sha256  0d795597e4666dbf6ffe44b4a42f388ddb44736ddfab0b1ac091e5bb35212c2d

use_autoreconf              yes

depends_build-append        port:pkgconfig

depends_lib                 port:bzip2 \
                            port:pcre \
                            port:spawn-fcgi \
                            port:zlib

patchfiles                  patch-conf.diff

post-patch {
    reinplace "s|@PREFIX@|${prefix}|g" \
        ${worksrcpath}/doc/config/conf.d/cgi.conf \
        ${worksrcpath}/doc/config/conf.d/fastcgi.conf \
        ${worksrcpath}/doc/config/conf.d/mysql_vhost.conf \
        ${worksrcpath}/doc/config/conf.d/rrdtool.conf \
        ${worksrcpath}/doc/config/lighttpd.conf
}

platform darwin {
    depends_lib-append      port:libev
    patchfiles-append       patch-conf-darwin.diff
    post-patch {
        if {${os.major} <= 8} {
            set user "www"
            set group "www"
        } else {
            set user "_www"
            set group "_www"
        }
        reinplace "s|@USER@|${user}|g" ${worksrcpath}/doc/config/lighttpd.conf
        reinplace "s|@GROUP@|${group}|g" ${worksrcpath}/doc/config/lighttpd.conf
    }
    configure.args-append   --with-libev ac_cv_func_kqueue=no
}

set lighttpd_config_dir     ${prefix}/etc/${name}

startupitem.create          yes
startupitem.executable      ${prefix}/sbin/lighttpd -D -f ${lighttpd_config_dir}/lighttpd.conf

destroot.keepdirs           ${destroot}${prefix}/var/log/lighttpd \
                            ${destroot}${prefix}/www/htdocs \
                            ${destroot}${prefix}/var/run/lighttpd \
                            ${destroot}${prefix}/var/lib/lighttpd/sockets \
                            ${destroot}${prefix}/var/cache/lighttpd/compress

post-destroot {
    xinstall -d ${destroot}${lighttpd_config_dir}/conf.d
    foreach f [glob ${worksrcpath}/doc/config/*.conf] {
        xinstall -m 644 ${f} ${destroot}${lighttpd_config_dir}/[file tail ${f}].default
    }
    foreach f [glob ${worksrcpath}/doc/config/conf.d/*.conf] {
        xinstall -m 644 ${f} ${destroot}${lighttpd_config_dir}/conf.d/[file tail ${f}].default
    }

   set docdir ${destroot}${prefix}/share/doc/${name}
   xinstall -d ${docdir}
   xinstall -m 644 -W ${worksrcpath} \
       AUTHORS \
       COPYING \
       NEWS \
       README \
       ${docdir}
}

install.asroot              yes

post-activate {
    # Copy default conf files if not present
    foreach f [glob ${lighttpd_config_dir}/*.conf.default] {
        set f [file rootname ${f}]
        if {![file exists ${f}]} {
            copy ${f}.default ${f}
        }
    }
    foreach f [glob ${lighttpd_config_dir}/conf.d/*.conf.default] {
        set f [file rootname ${f}]
        if {![file exists ${f}]} {
            copy ${f}.default ${f}
        }
    }
}

variant mysql4 conflicts mysql5 description {Enable MySQL 4 support} {
    depends_lib-append      port:mysql4
    configure.args-append   --with-mysql=${prefix}/bin/mysql_config
    #configure.cppflags-append -I${prefix}/include/mysql
}

variant mysql5 conflicts mysql4 description {Enable MySQL 5 support} {
    depends_lib-append      path:bin/mysql_config5:mysql5
    configure.args-append   --with-mysql=${prefix}/bin/mysql_config5
    #configure.cppflags-append -I${prefix}/include/mysql5/mysql
}

variant ssl description {Enable serving secure web sites with SSL} {
    depends_lib-append      port:openssl
    configure.args-append   --with-openssl
}

variant cml description {Enable Cache Meta-Language (CML)} {
    depends_lib-append      port:lua \
                            port:libmemcache \
                            port:memcached
    configure.args-append   --with-lua \
                            --with-memcache
}

variant davprops description {Enable mod_webdav} {
    depends_lib-append      port:libxml2 \
                            port:sqlite3
    configure.args-append   --with-webdav-props
}

default_variants            +ssl

notes "
Before starting lighttpd it must be configured.\
Basic server configuration is in the file ${lighttpd_config_dir}/lighttpd.conf.\
Select which modules you want enabled in ${lighttpd_config_dir}/modules.conf.\
Individual modules' settings are in ${lighttpd_config_dir}/conf.d.

Sample config files have the .conf.default extension.\
When updating lighttpd, you should investigate whether you need to update\
your .conf files with changes from the corresponding .conf.default files.
"