# -*- 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
# $Id: Portfile 92837 2012-05-09 00:51:48Z ryandesign@macports.org $

PortSystem              1.0

name                    exim
version                 4.71
revision                2
categories              mail
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. It is freely \
                        available under the terms of the GNU General \
                        Public Licence. In overall style it is similar \
                        to Smail 3, but its facilities are more extensive.
homepage                http://www.exim.org/
platforms               darwin
maintainers             nomaintainer
master_sites            http://mirrors.24-7-solutions.net/pub/exim/ftp/exim/exim4/ \
                        http://mirrors.24-7-solutions.net/pub/exim/ftp/exim/exim4/old/ \
                        ftp://ftp.exim.org/pub/exim/exim4/ \
                        ftp://ftp.exim.org/pub/exim/exim4/old/ \
                        ftp://ftp.easynet.be/exim/exim/exim4/ \
                        ftp://ftp.easynet.be/exim/exim/exim4/old/
use_bzip2               yes

checksums               md5     f9c5a2d94b5bb132d06e2fff85bef75e \
                        sha1    4b8f853843edcfa4f3bfbb4bef45d8dcff2fc990 \
                        rmd160  22e4ad1d43693d61fa1f2f2de40dda647599a087

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

depends_lib             port:db44 \
                        port: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

use_parallel_build      no

universal_variant       no

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 "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/db44|g" ${worksrcpath}/OS/Makefile-Darwin
    reinplace "s|DBMLIB =|DBMLIB=${prefix}/lib/db44/libdb-4.dylib|g" ${worksrcpath}/OS/Makefile-Darwin
    reinplace "s|/etc/aliases|${destroot}${prefix}/etc/aliases|g" ${worksrcpath}/scripts/exim_install
    adduser ${exim_user} gid=[existsgroup mail]
}

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

post-destroot {
    system "mv ${destroot}${prefix}/etc/exim/exim.conf ${destroot}${prefix}/etc/exim/exim.conf.sample"
    xinstall -o root -m 755 -d ${destroot}${prefix}/share/exim
    system "cp -R ${worksrcpath}/doc/ ${destroot}${prefix}/share/exim"
    xinstall -o ${exim_user} -g mail -m 750 -d ${destroot}${prefix}/var/spool/exim
    xinstall -o root -m 755 -d ${destroot}${prefix}/etc/rc.d
    xinstall -o root -m 755 -c \
        ${portpath}/files/exim.sh ${destroot}${prefix}/share/exim
    reinplace "s|__PREFIX|${prefix}|g" ${destroot}${prefix}/share/exim/exim.sh
}

variant mysql4 conflicts mysql5 description {build exim with support for mysql4 lookups} {
    depends_lib-append  port:mysql4
}

variant mysql5 conflicts mysql4 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  port: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|# SUPPORT_TLS=yes|SUPPORT_TLS=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 mysql4]
        ||  [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 mysql4]} {
            reinplace "s|^# LOOKUP_MYSQL=yes|LOOKUP_MYSQL=yes|g" ${worksrcpath}/Local/Makefile
            reinplace "s|^LOOKUP_LIBS=.*|& -L${prefix}/lib/mysql -lmysqlclient|g" ${worksrcpath}/Local/Makefile
            reinplace "s|^LOOKUP_INCLUDE=.*|& -I${prefix}/include/mysql -I ${prefix}/include|g" ${worksrcpath}/Local/Makefile
        }
        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}version.html
livecheck.regex         "The latest release is <b>(\\d+\\.\\d+)</b>"
