# -*- 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           cmake 1.1
PortGroup           github 1.0

github.setup        lpereira lwan 0.3 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0

homepage            https://lwan.ws

description         Lwan is a high-performance & scalable web server.

long_description    {*}${description} With its low disk and memory \
                    footprints, it's suitable to be used from embedded \
                    devices to robust servers. Both static and dynamic \
                    content can be served, as it can also be used as a \
                    library. Dynamic content can be generated by code written \
                    in either C or Lua.  Connections are handled individually \
                    by coroutines, which are transparently and efficiently \
                    juggled by a per-CPU cooperative scheduler, giving the \
                    illusion of blocking I/O to handlers. Lwan isn't just a \
                    simple static file server: it can be used as a library to \
                    build web services on top of it. In fact, the static file \
                    server isn't a special case: it just uses the same APIs \
                    that are available when Lwan is used as a library.

categories          www
license             GPL-2

maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

checksums           rmd160  0f12c549c8b37bd5e4d3ce622814be40ee1d2e66 \
                    sha256  14d382745ec68616ae5deede3327b5e2c787ba67d077c0ac40b748f8ae6c97a2 \
                    size    376148

cmake.build_type    Release

depends_build-append \
                    port:pkgconfig

depends_lib-append  port:brotli \
                    port:lua51  \
                    port:zlib   \
                    port:zstd

set lwan_user       ${name}

add_users           ${lwan_user} group=${lwan_user} \
                    realname=LWAN\ Web\ Server

set lwan_conf_path  ${prefix}/etc/${name}
set lwan_conf_file  ${lwan_conf_path}/${name}.conf
set lwan_log_path   ${prefix}/var/log/${name}
set lwan_log_file   ${lwan_log_path}/${name}.log
set lwan_share_path ${prefix}/share/${name}
set lwan_wwwroot    ${prefix}/var/www-${name}
set lwan_conf_tpl   ${lwan_share_path}/${name}.conf.default

set lwan_plist_src  ${workpath}/org.macports.lwan.plist

post-extract {
    # Set default configuration to listen on port 80
    reinplace "s|:8080|:80|g"                       ${worksrcpath}/lwan.conf

    # Set default configuration to use the MacPorts-specific www-root
    reinplace "s|./wwwroot|${lwan_wwwroot}|g"       ${worksrcpath}/lwan.conf

    # Prepare launchd plist
    copy ${filespath}/org.macports.lwan.plist ${workpath}/
    reinplace "s|@NAME@|${name}|g"                  ${lwan_plist_src}
    reinplace "s|@USER@|${lwan_user}|g"             ${lwan_plist_src}
    reinplace "s|@GROUP@|${lwan_user}|g"            ${lwan_plist_src}
    reinplace "s|@PREFIX@|${prefix}|g"              ${lwan_plist_src}
    reinplace "s|@CONF_FILE@|${lwan_conf_file}|g"   ${lwan_plist_src}
    reinplace "s|@LOGFILE@|${lwan_log_file}|g"      ${lwan_plist_src}
}

post-destroot {
    xinstall -d -m 755 ${destroot}${lwan_conf_path}
    xinstall -d -m 755 ${destroot}${lwan_share_path}
    xinstall -d -m 755 ${destroot}${lwan_wwwroot}
    xinstall -d -m 755 -o ${lwan_user} -g ${lwan_user} \
        ${destroot}${lwan_log_path}

    copy {*}[glob ${worksrcpath}/wwwroot/*] ${destroot}${lwan_wwwroot}/
    copy ${worksrcpath}/lwan.conf ${destroot}${lwan_conf_tpl}

    xinstall -d -m 755 \
        ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}

    xinstall -m 0644 -o root -W ${workpath} org.macports.${name}.plist \
        ${destroot}${prefix}/etc/LaunchDaemons/org.macports.${name}

    xinstall -d -m 755 ${destroot}/Library/LaunchDaemons

    ln -s ${prefix}/etc/LaunchDaemons/org.macports.${name}/org.macports.${name}.plist \
        ${destroot}/Library/LaunchDaemons/org.macports.${name}.plist
}

destroot.keepdirs   ${destroot}${lwan_conf_path} \
                    ${destroot}${lwan_log_path}

post-activate {
    if {![file exists ${lwan_conf_file}]} {
        copy ${lwan_conf_tpl} ${lwan_conf_file}
    }
}

notes "
    lwan's configuration file is located at: ${lwan_conf_file}

    To start the lwan service, use `port load`:
    $ sudo port load lwan

    To stop and disable the service, use `port unload`:
    $ sudo port unload lwan
"
