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

go.setup            github.com/foxcpp/maddy 0.7.1 v
revision            0

homepage            https://maddy.email

description         Composable all-in-one mail server

long_description    \
    Maddy Mail Server implements all functionality required to run a e-mail \
    server. It can send messages via SMTP (works as MTA), accept messages via \
    SMTP (works as MX) and store messages while providing access to them via \
    IMAP. In addition to that it implements auxiliary protocols that are \
    mandatory to keep email reasonably secure (DKIM, SPF, DMARC, DANE, \
    MTA-STS). It replaces Postfix, Dovecot, OpenDKIM, OpenSPF, OpenDMARC and \
    more with one daemon with uniform configuration and minimal maintenance \
    cost.

categories          mail
installs_libs       no
license             GPL-3
maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

checksums           rmd160  859f1a140575c19ae3f25c67e482a0234e4ddbb9 \
                    sha256  541288477afb040a04a5cf17a1c3e3b7a81b287dc3a81c844b2d6ad66688c29e \
                    size    410871

depends_build-append \
                    port:scdoc

patchfiles          patch-build.sh.diff

post-patch {
    reinplace "s|/etc/maddy|${prefix}/etc/maddy|g" ${worksrcpath}/maddy.conf
}

set maddy_logfile   ${prefix}/var/log/${name}.log

# Allow Go to fetch dependencies at build time
go.offline_build no

build.cmd           ./build.sh
build.pre_args      --prefix ${prefix} \
                    --destdir ${destroot}
build.args          build

destroot.keepdirs   ${destroot}${prefix}/var/lib/maddy \
                    ${destroot}${prefix}/var/run/maddy \
                    ${destroot}${prefix}/libexec/maddy

if {"darwin" eq ${os.platform} && ${os.major} > 8} {
    set svcuser _${name}
} else {
    set svcuser ${name}
}

add_users ${svcuser} group=${svcuser} realname=Maddy\ Email\ Server

startupitem.create  yes
startupitem.executable \
                    maddy run
startupitem.logfile ${maddy_logfile}
startupitem.user    ${svcuser}

destroot {
    system -W ${worksrcpath} \
        "${build.env} ./build.sh --prefix ${prefix} --destdir ${destroot} install"

    xinstall -d -o ${svcuser} -m 0700 ${destroot}${prefix}/var/lib/maddy
    xinstall -d -o ${svcuser} -m 0700 ${destroot}${prefix}/var/run/maddy
    xinstall -d ${destroot}${prefix}/libexec/maddy

    touch ${destroot}${maddy_logfile}

    file attributes \
        ${destroot}${maddy_logfile} -owner ${svcuser} -group ${svcuser}
}

notes-append "
    Before loading the maddy service, adjust configuration in ${prefix}/etc/maddy,
    as well as generating certificates and other necessary prerequisites.
"