# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=portfile:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           legacysupport 1.1
PortGroup           lua 1.0
PortGroup           openssl 1.0

# clock_gettime
legacysupport.newest_darwin_requires_legacy 15

name                prosody
version             0.12.3
revision            3
maintainers         nomaintainer

categories          net chat
description         Prosody is a modern flexible communications server for \
                    Jabber/XMPP written in Lua.
long_description    {*}${description} It aims to be easy to set up and configure, \
                    and light on resources. For developers it aims to be easy \
                    to extend and give a flexible system on which to rapidly \
                    develop added functionality, or prototype new protocols.

homepage            https://prosody.im
license             MIT

master_sites        ${homepage}/downloads/source/

depends_lib         port:libidn \
                    path:lib/pkgconfig/icu-uc.pc:icu
lua.modules         luasocket \
                    luasec \
                    luaexpat \
                    luafilesystem

checksums           rmd160  fdcf162b73c5b57c49638b487f2896e36503c345 \
                    sha256  35da0d031ff46040a2d638e004d4255e249b6323fe6212db9ddd76b401db2101 \
                    size    615302

set logdir          ${prefix}/var/log/${name}
set rundir          ${prefix}/var/run/${name}

patchfiles          patch-config.diff

post-patch {
    reinplace       "s|@@PREFIX@@|${prefix}|g" \
                    ${worksrcpath}/prosody.cfg.lua.dist

    reinplace       "s|^#!/usr/bin/env lua\$|#!${lua.bin}|g" \
                    ${worksrcpath}/prosody \
                    ${worksrcpath}/prosodyctl
}

# It needs C99, otherwise build fails with old Apple complilers.
configure.cflags-append  -std=c99 -fPIC
configure.ldflags-append -bundle -undefined dynamic_lookup

# using `--ostype=macosx` appends `-mmacosx-version-min=10.3` to CFLAGS
configure.args-append \
                    --with-lua=${lua.dir} \
                    --with-lua-include=${lua.dir}/include \
                    --with-lua-lib=${lua.dir}/lib \
                    --c-compiler=\"\$CC\" \
                    --linker=\"\$CC\" \
                    --cflags=\"\$CFLAGS\" \
                    --ldflags=\"\$LDFLAGS\"

startupitem.create  yes
startupitem.start   "${prefix}/bin/${name}ctl start"
startupitem.stop    "${prefix}/bin/${name}ctl stop"
startupitem.restart "${prefix}/bin/${name}ctl restart"
startupitem.pidfile auto ${rundir}/prosody.pid

add_users           ${name} group=${name} realname=Prosody\ Server

destroot.keepdirs   ${destroot}${logdir} ${destroot}${rundir}

post-destroot {
    xinstall -o ${name} -g ${name} -d ${destroot}${logdir} ${destroot}${rundir}
}

livecheck.type      regex
livecheck.url       ${master_sites}
livecheck.regex     "${name}-(\\d+\\.\\d+\\.\\d+)"
