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

# clock_gettime(), openat()
PortGroup               legacysupport 1.1

legacysupport.newest_darwin_requires_legacy 15

name                    exim
version                 4.94.2
revision                3
checksums               rmd160  4de1b7cca08ccbcaf3987332d15cd1fbc6135c9b \
                        sha256  051861fc89f06205162f12129fb7ebfe473383bb6194bf8642952bfd50329274 \
                        size    1838076

categories              mail
license                 GPL-2

description             complete replacement for sendmail

long_description        Exim is a mail transfer agent (MTA) developed at the \
                        University of Cambridge for use on Unix systems \
                        connected to the Internet. In overall style it is \
                        similar to Smail 3, but its facilities are more \
                        extensive.

homepage                https://www.exim.org/
master_sites            https://ftp.exim.org/pub/exim/exim4/ \
                        https://ftp.exim.org/pub/exim/exim4/old/ \
                        http://www.mirrorservice.org/sites/ftp.exim.org/pub/exim/exim4/ \
                        http://www.mirrorservice.org/sites/ftp.exim.org/pub/exim/exim4/old/

maintainers             nomaintainer
use_xz                  yes

depends_build           path:bin/perl:perl5 \
                        port:pcre

depends_lib             port:db48 \
                        path:lib/libssl.dylib:openssl \
                        port:sqlite3

startupitem.create      yes
startupitem.start       "${prefix}/share/exim/exim.sh start"
startupitem.stop        "${prefix}/share/exim/exim.sh stop"

set exim_user           exim
add_users ${exim_user} group=mail

patchfiles              openssl3.patch

use_parallel_build      no

universal_variant       no

# macro_predef.c:84: error: ‘for’ loop initial declaration used outside C99 mode
configure.cflags-append -std=c99
compiler.c_standard     1999

configure {
    copy ${worksrcpath}/src/EDITME ${worksrcpath}/Local/Makefile
    reinplace "s|/usr/exim/bin|${prefix}/sbin|g" ${worksrcpath}/Local/Makefile
    reinplace "s|/usr/exim/configure|${prefix}/etc/exim/exim.conf|g" ${worksrcpath}/Local/Makefile
    reinplace "s|EXIM_USER=|EXIM_USER=${exim_user}|g" ${worksrcpath}/Local/Makefile
    reinplace "s|/var/spool/exim|${prefix}/var/spool/exim|g" ${worksrcpath}/Local/Makefile
    reinplace "s|EXIM_MONITOR=eximon.bin|#EXIM_MONITOR=eximon.bin|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# WITH_CONTENT_SCAN=yes|WITH_CONTENT_SCAN=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# LOOKUP_PASSWD=yes|LOOKUP_PASSWD=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# LOOKUP_CDB=yes|LOOKUP_CDB=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# LOOKUP_DSEARCH=yes|LOOKUP_DSEARCH=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# SUPPORT_MAILDIR=yes|SUPPORT_MAILDIR=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# TLS_LIBS=-L/usr/local/openssl/lib|TLS_LIBS=-L${prefix}/lib|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# TLS_INCLUDE=-I/usr/local/openssl/include|TLS_INCLUDE=-I${prefix}/include/openssl|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# INCLUDE=.*|INCLUDE=-I${prefix}/include|g" ${worksrcpath}/Local/Makefile
    reinplace "s|PCRE_LIBS=-lpcre|PCRE_LIBS=-L${prefix}/lib -lpcre|g" ${worksrcpath}/Local/Makefile
    reinplace "1i\\\nEXTRALIBS=\$(MACPORTS_LEGACY_SUPPORT_LDFLAGS)\n" ${worksrcpath}/OS/Makefile-Darwin
    reinplace "s|CC=cc|CC=${configure.cc}|g" ${worksrcpath}/OS/Makefile-Darwin
    reinplace "s|X11=/usr/X11R6|X11=${prefix}|g" ${worksrcpath}/OS/Makefile-Darwin
    reinplace "s|# Exim: OS-specific make file for Darwin (Mac OS X).|INCLUDE=-I${prefix}/include/db48|g" ${worksrcpath}/OS/Makefile-Darwin
    reinplace "s|DBMLIB =|DBMLIB=${prefix}/lib/db48/libdb-4.dylib|g" ${worksrcpath}/OS/Makefile-Darwin
    reinplace "s|/etc/aliases|${destroot}${prefix}/etc/aliases|g" ${worksrcpath}/scripts/exim_install
}

build.args              CFLAGS="${configure.cflags} [get_canonical_archflags cc]" \
                        FULLECHO= \
                        LFLAGS="${configure.ldflags} [get_canonical_archflags ld]"

destroot.destdir        ROOT=${destroot}
destroot.keepdirs       ${destroot}${prefix}/var/spool/exim

post-destroot {
    move ${destroot}${prefix}/etc/exim/exim.conf ${destroot}${prefix}/etc/exim/exim.conf.sample

    xinstall -o root -m 755 -d ${destroot}${prefix}/share/doc
    copy ${worksrcpath}/doc ${destroot}${prefix}/share/doc/exim

    xinstall -o ${exim_user} -g mail -m 750 -d ${destroot}${prefix}/var/spool/exim

    xinstall -o root -m 755 -d ${destroot}${prefix}/share/exim
    xinstall -o root -m 755 -c ${filespath}/exim.sh ${destroot}${prefix}/share/exim
    reinplace "s|__PREFIX|${prefix}|g" ${destroot}${prefix}/share/exim/exim.sh
}

variant mysql5 description {build exim with support for mysql5 lookups} {
    depends_lib-append  path:bin/mysql_config5:mysql5
}

variant ldap conflicts macports_ldap description {build exim with support for ldap lookups using the shipped ldap} {
    # do nothing .. until post-configure
}

variant macports_ldap conflicts ldap description {install exim with support for ldap lookups using macports ldap} {
    depends_lib-append  path:lib/libldap.dylib:openldap
}

post-configure {
    reinplace "s|# AUTH_CRAM_MD5=yes|AUTH_CRAM_MD5=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# AUTH_PLAINTEXT=yes|AUTH_PLAINTEXT=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# AUTH_SPA=yes|AUTH_SPA=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# USE_OPENSSL=yes|USE_OPENSSL=yes|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# TLS_LIBS=-lssl -lcrypto|TLS_LIBS=-lssl -lcrypto|g" ${worksrcpath}/Local/Makefile
    reinplace "s|# LDAP_LIB_TYPE=OPENLDAP2|LDAP_LIB_TYPE=OPENLDAP2|g" ${worksrcpath}/Local/Makefile
    
    # settings common to any lookup
    if {[variant_isset mysql5]
        ||  [variant_isset ldap]
        ||  [variant_isset macports_ldap]
    } then {
        reinplace "s|^# LOOKUP_INCLUDE=.*|LOOKUP_INCLUDE=|g" ${worksrcpath}/Local/Makefile
        reinplace "s|^# LOOKUP_LIBS=.*|LOOKUP_LIBS=-L${prefix}/lib|g" ${worksrcpath}/Local/Makefile
        # lookup specific settings
        if {[variant_isset mysql5]} {
            reinplace "s|^# LOOKUP_MYSQL=yes|LOOKUP_MYSQL=yes|g" ${worksrcpath}/Local/Makefile
            reinplace "s|^LOOKUP_LIBS=.*|& -L${prefix}/lib/mysql5/mysql -lmysqlclient |g" ${worksrcpath}/Local/Makefile
            reinplace "s|^LOOKUP_INCLUDE=.*|& -I${prefix}/include/mysql5/mysql |g"  ${worksrcpath}/Local/Makefile
        }
        if {[variant_isset ldap]} {
            reinplace "s|^# LOOKUP_LDAP=yes|LOOKUP_LDAP=yes|g"  ${worksrcpath}/Local/Makefile
            reinplace "s|^LOOKUP_LIBS=.*|& -L/usr/lib -lldap -llber |g" ${worksrcpath}/Local/Makefile
            reinplace "s|^LOOKUP_INCLUDE=.*|& -I/usr/include |g"  ${worksrcpath}/Local/Makefile
        }
        if {[variant_isset macports_ldap]} {
            reinplace "s|^# LOOKUP_LDAP=yes|LOOKUP_LDAP=yes|g"  ${worksrcpath}/Local/Makefile
            reinplace "s|^LOOKUP_LIBS=.*|& -L${prefix}/lib -lldap -llber |g" ${worksrcpath}/Local/Makefile
            reinplace "s|^LOOKUP_INCLUDE=.*|& -I${prefix}/include |g"  ${worksrcpath}/Local/Makefile
        }
    }
}

livecheck.type          regex
livecheck.url           ${homepage}
livecheck.regex         {Latest Version: ([0-9.]+)}
