# $Id: Portfile 92837 2012-05-09 00:51:48Z ryandesign@macports.org $

PortSystem  1.0

name            postfix
version         2.9.2
revision        1
categories      mail
license         IBMPL-1.0
maintainers     jmr openmaintainer
description     Fast and robust mail transfer agent
long_description    Postfix attempts to be fast, easy to administer, and \
            secure, while at the same time being \
            sendmail-compatible enough to not upset existing \
            users. It also offers QMQP and VERP support to let \
            Postfix act as delivery daemon for ezmlm-idx.
homepage        http://www.postfix.org/
platforms       darwin
checksums       rmd160 512fb6fcb42d628b14a7153f3a5f2e03b7b6a8c6 \
                sha256 ed66f5406056267ce21dbf48c6f4e3b53bf8fad46d799d314445353f42b5c902

master_sites    http://archive.mgm51.com/mirrors/postfix-source/official/ \
                http://de.postfix.org/ftpmirror/official/ \
                ftp://ftp.its.cz/MIRRORS/ftp.porcupine.org/mirrors/postfix-release/official/ \
                ftp://ftp.cuhk.edu.hk/pub/packages/mail-server/postfix/official/ \
                http://mirror.postfix.jp/postfix-release/official/ \
                http://postfix.bbnx.net/source/official/

patchfiles      patch-sys_defs.h patch-mail_params.h patch-postfix-install \
                patch-makedefs

post-patch {
     eval reinplace "s|/etc/|${prefix}/etc/|g" \
          [glob ${worksrcpath}/man/man1/*] \
          [glob ${worksrcpath}/man/man5/*] \
          [glob ${worksrcpath}/man/man8/*]

     reinplace "s|__PREFIX|${prefix}|g" ${worksrcpath}/src/util/sys_defs.h \
                                        ${worksrcpath}/src/global/mail_params.h

     reinplace "s|ls |/bin/ls |g" \
          ${worksrcpath}/conf/postfix-script

# Set main.cf default values
     reinplace "s|#default_privs|default_privs|g" \
          ${worksrcpath}/conf/main.cf
     reinplace "s|/etc/postfix/network_table|${prefix}/etc/postfix/network_table|g" \
          ${worksrcpath}/conf/main.cf
     reinplace "s|/etc/postfix/relay_recipients|${prefix}/etc/postfix_relay_recipients|g" \
          ${worksrcpath}/conf/main.cf
     reinplace "s|/etc/postfix/header_checks|${prefix}/etc/postfix/header_checks|g" \
          ${worksrcpath}/conf/main.cf
     reinplace "s|PATH=/bin:/usr/bin|PATH=${prefix}/bin:/bin:/usr/bin|g" \
          ${worksrcpath}/conf/main.cf
}

set CCARGS  [list -DNO_PCRE]
set AUXLIBS [list "-L${prefix}/lib" "-R${prefix}/lib" "-L${prefix}/lib"]
if {[variant_isset universal]} {
    set CCARGS [concat ${CCARGS} ${configure.universal_cflags}]
    set AUXLIBS [concat ${AUXLIBS} ${configure.universal_ldflags}]
} else {
    set CCARGS [concat ${CCARGS} ${configure.cc_archflags}]
    set AUXLIBS [concat ${AUXLIBS} ${configure.ld_archflags}]
}
set pf_mail_owner "_postfix"
set pf_setgid_group "_postdrop"
set config_files [list main.cf master.cf access aliases canonical generic \
                  header_checks relocated transport virtual]

# Beginning with Mac OS X 10.5, the user 'postfix' changed to '_postfix'
platform darwin 8 {
    set pf_mail_owner "postfix"
    set pf_setgid_group "postdrop"
}

if {${os.major} >= 10 && ${os.platform} == "darwin"} {
    lappend AUXLIBS -lresolv
}

configure {
    system "cd ${worksrcpath} && ${build.cmd} makefiles CC='${configure.cc}' \
        OPT='${configure.optflags}' CCARGS='${CCARGS}' AUXLIBS='${AUXLIBS}'"
}

build.target

startupitem.create      yes
startupitem.start       "${prefix}/sbin/${name} start"
startupitem.stop        "${prefix}/sbin/${name} stop"

destroot {
    destroot.keepdirs ${destroot}${prefix}/var/spool/postfix/public \
        ${destroot}${prefix}/var/spool/postfix/maildrop \
        ${destroot}${prefix}/var/lib/postfix
    system "cd ${worksrcpath} && /bin/sh postfix-install -non-interactive \
        install_root=${destroot} \
        config_directory=${prefix}/etc/postfix \
        queue_directory=${prefix}/var/spool/postfix \
        command_directory=${prefix}/sbin \
        daemon_directory=${prefix}/libexec/postfix \
        data_directory=${prefix}/var/lib/postfix \
        sendmail_path=${prefix}/sbin/sendmail \
        newaliases_path=${prefix}/bin/newaliases \
        mailq_path=${prefix}/bin/mailq \
        etc_directory=${prefix}/etc/postfix \
        share_directory=${prefix}/share/postfix \
        manpage_directory=${prefix}/share/man \
        sample_directory=${prefix}/share/postfix/sample \
        readme_directory=${prefix}/share/postfix/readme \
        mail_owner=${pf_mail_owner} \
        setgid_group=${pf_setgid_group}"

        # This makes sure we don't overwrite user cf files.
        foreach f $config_files {
            move ${destroot}${prefix}/etc/postfix/${f} \
                 ${destroot}${prefix}/etc/postfix/${f}.sample
        }

        if {[geteuid] == 0} {
            system "chown -R ${pf_mail_owner} \
                '${destroot}${prefix}/var/spool/postfix/public' \
                '${destroot}${prefix}/var/lib/postfix'"
        }
}

pre-install {
        if {[geteuid] != 0} {
            ui_msg "Insufficient privileges to chown files; you'll need to do this manually:"
            ui_msg "sudo chown -R ${pf_mail_owner} ${prefix}/var/spool/postfix/public ${prefix}/var/lib/postfix"
        }
}

post-activate {
    foreach f $config_files {
        if {![file exists ${prefix}/etc/postfix/${f}]} {
            copy ${prefix}/etc/postfix/${f}.sample \
                 ${prefix}/etc/postfix/${f}
        }
    }
}

# Tell the user about renaming the sample configuration files.
notes "
To get postfix working, you need to edit the configuration files\
(${prefix}/etc/postfix/*). You may also want to turn off Apple's built-in MTA.\
For example:

sudo launchctl unload -w /System/Library/LaunchDaemons/org.postfix.master.plist
"

variant pcre description "add pcre support" {
    set CCARGS      [ldelete ${CCARGS} -DNO_PCRE]
    lappend CCARGS  -DHAS_PCRE "-I${prefix}/include"
    lappend AUXLIBS "-L${prefix}/lib" -lpcre
    depends_lib-append  port:pcre
}

variant tls description "add tls support via openssl" {
    depends_lib-append  port:openssl
    lappend CCARGS  -DUSE_TLS -DHAS_SSL "-I${prefix}/include"
    lappend AUXLIBS -lssl -lcrypto
}

variant sasl description "add sasl support via cyrus-sasl2" {
    depends_lib-append  port:cyrus-sasl2
    lappend CCARGS  -DUSE_CYRUS_SASL -DUSE_SASL_AUTH "-I${prefix}/include/sasl"
    lappend AUXLIBS -lsasl2
}

variant ldap description "add ldap support via openldap" {
    depends_lib-append  port:openldap
    lappend CCARGS  -DHAS_LDAP
    lappend AUXLIBS -lldap -llber
}

# Keep the mysql5 variant until the mysql5 port is replaced_by mysql51 port.
variant mysql5  conflicts mysql51 mysql55 mariadb percona description "add MySQL support via mysql5" {
    depends_lib-append path:bin/mysql_config5:mysql5
    lappend CCARGS  -DHAS_MYSQL "-I${prefix}/include/mysql5/mysql"
    lappend AUXLIBS "-L${prefix}/lib/mysql5/mysql" -lmysqlclient -lz -lm
}

variant mysql51 conflicts mysql5 mysql55 mariadb percona description "add MySQL support via mysql51" {
    depends_lib-append port:mysql51
    lappend CCARGS  -DHAS_MYSQL "-I${prefix}/include/mysql51/mysql"
    lappend AUXLIBS "-L${prefix}/lib/mysql51/mysql" -lmysqlclient -lz -lm
}

variant mysql55 conflicts mysql5 mysql51 mariadb percona description "add MySQL support via mysql55" {
    depends_lib-append port:mysql55
    lappend CCARGS  -DHAS_MYSQL "-I${prefix}/include/mysql55/mysql"
    lappend AUXLIBS "-L${prefix}/lib/mysql55/mysql" -lmysqlclient -lz -lm
}

variant mariadb conflicts mysql5 mysql51 mysql55 percona description "add MySQL support via mariadb" {
    depends_lib-append port:mariadb
    lappend CCARGS  -DHAS_MYSQL "-I${prefix}/include/mariadb/mysql"
    lappend AUXLIBS "-L${prefix}/lib/mariadb/mysql" -lmysqlclient -lz -lm
}

variant percona conflicts mysql5 mysql51 mysql55 mariadb description "add MySQL support via percona" {
    depends_lib-append port:percona
    lappend CCARGS  -DHAS_MYSQL "-I${prefix}/include/percona/mysql"
    lappend AUXLIBS "-L${prefix}/lib/percona/mysql" -lmysqlclient -lz -lm
}

variant postgresql83 conflicts postgresql84 postgresql90 postgresql91 description "add postgresql support via postgresql83" {
    depends_lib-append port:postgresql83
    lappend CCARGS  -DHAS_PGSQL "-I${prefix}/include/postgresql83"
    lappend AUXLIBS "-L${prefix}/lib/postgresql83" -lpq
}

variant postgresql84 conflicts postgresql83 postgresql90 postgresql91 description "add postgresql support via postgresql84" {
    depends_lib-append port:postgresql84
    lappend CCARGS  -DHAS_PGSQL "-I${prefix}/include/postgresql84"
    lappend AUXLIBS "-L${prefix}/lib/postgresql84" -lpq
}

variant postgresql90 conflicts postgresql83 postgresql84 postgresql91 description "add postgresql support via postgresql90" {
    depends_lib-append port:postgresql90
    lappend CCARGS  -DHAS_PGSQL "-I${prefix}/include/postgresql90"
    lappend AUXLIBS "-L${prefix}/lib/postgresql90" -lpq
}

variant postgresql91 conflicts postgresql83 postgresql84 postgresql90 description "add postgresql support via postgresql91" {
    depends_lib-append port:postgresql91
    lappend CCARGS  -DHAS_PGSQL "-I${prefix}/include/postgresql91"
    lappend AUXLIBS "-L${prefix}/lib/postgresql91" -lpq
}

variant dovecot_sasl description "add Dovecot SASL support" {
    lappend CCARGS  -DUSE_SASL_AUTH -DDEF_SERVER_SASL_TYPE="dovecot"
}

livecheck.type  regex
livecheck.url   ftp://ftp.porcupine.org/mirrors/postfix-release/official/
livecheck.regex ${name}-(\[0-9.\]+)${extract.suffix}
