# -*- 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                   legacysupport 1.0
PortGroup                   lua 1.0

name                        lighttpd
version                     1.4.81
revision                    0
checksums                   rmd160  d0903f07d63c8036762024eeec4d6fba340db703 \
                            sha256  d7d42c3fd2fd94b63c915aa7d18f4da3cac5937ddba33e909f81cf50842a5840 \
                            size    882560

set branch                  [join [lrange [split ${version} .] 0 1] .]
categories                  www
maintainers                 {ryandesign @ryandesign} openmaintainer
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                    https://www.lighttpd.net/
master_sites                https://download.lighttpd.net/lighttpd/releases-${branch}.x/
use_xz                      yes

# Upstream no longer provides a configure script in the tarball.
use_autoreconf              yes
autoreconf.cmd              ./autogen.sh
autoreconf.args

depends_build-append        port:autoconf \
                            port:automake \
                            port:libtool \
                            path:bin/pkg-config:pkgconfig

depends_lib                 port:brotli \
                            port:pcre2 \
                            port:spawn-fcgi \
                            port:xxhashlib \
                            port:zlib \
                            port:zstd

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/rrdtool.conf \
        ${worksrcpath}/doc/config/lighttpd.annotated.conf
}

configure.args-append       CC_FOR_BUILD="${configure.cc}" \
                            CFLAGS_FOR_BUILD="${configure.cflags}" \
                            --with-brotli \
                            --with-lua=lua \
                            --with-pcre2 \
                            --with-xxhash \
                            --with-zlib \
                            --with-zstd \
                            ac_cv_prog_AWK=/usr/bin/awk

platform darwin {
    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.annotated.conf
        reinplace "s|@GROUP@|${group}|g" ${worksrcpath}/doc/config/lighttpd.annotated.conf
    }
}

test.run                    yes
test.target                 check
test.env                    SHELL=/bin/sh

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 0644 ${f} ${destroot}${lighttpd_config_dir}/[file tail ${f}].default
    }
    foreach f [glob ${worksrcpath}/doc/config/conf.d/*.conf] {
        xinstall -m 0644 ${f} ${destroot}${lighttpd_config_dir}/conf.d/[file tail ${f}].default
    }

   set docdir ${destroot}${prefix}/share/doc/${name}
   xinstall -d ${docdir}
   xinstall -m 0644 -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 mysql57 description {Enable MySQL 5.7 support} {
    depends_lib-append      port:mysql57
    configure.args-append   --with-mysql=${prefix}/lib/mysql57/bin/mysql_config
    #configure.cppflags-append -I${prefix}/include/mysql57/mysql
}

variant ssl description {Enable serving secure web sites with SSL} {
    depends_lib-append      path:lib/libssl.dylib:openssl
    configure.args-append   --with-openssl
}

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.
"
