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

name                cyrus5-imapd
version             2.5.4
revision            3
categories          mail
platforms           darwin
license             BSD-old
maintainers         nomaintainer

description         The Cyrus IMAP Server
long_description    Popular, scalable, open standars based IMAP & POP3 mail server \
                    developed by The Carnegie Mellon University.

homepage            http://cyrusimap.org/
master_sites        ftp://ftp.cyrusimap.org/cyrus-imapd/ \
                    https://www.cyrusimap.org/releases/old
distname            cyrus-imapd-${version}

checksums           rmd160  8263f17933e40959c34e51ea8ece3e1020519f0e \
                    sha256  98839d2865c0ff5c5dc0f59aba54ad111135697c3383919e037651d90f00aaaa

depends_build       port:makedepend

depends_lib         port:perl5 \
                    port:cyrus-sasl2 \
                    path:lib/libssl.dylib:openssl

patchfiles          patch-util_h.diff \
                    patch-Makefile_in.diff

# Check for any db* installation
if {[glob -nocomplain ${prefix}/include/db*] != ""} {
    set db_list [lsort [glob ${prefix}/include/db\[0-9\]*]]
    set current_db [lindex ${db_list} end]
    set db_ver [string range [lindex [split ${current_db} "/"] end] end-1 end]
} else {
    set db_ver 60
    depends_lib-append   port:db60
}

pre-fetch {
    ui_msg "Selected db${db_ver}"
}

post-patch {
    foreach dir {contrib contrib/cyrus-graphtools.1.0/cgi-bin contrib/cyrus-graphtools.1.0/script perl/imap/examples perl/sieve/scripts tools} {
        reinplace "s|^#!.*perl|#!${prefix}/bin/perl|" {*}[glob -d ${worksrcpath}/${dir} *.pl]
    }
}

# wrong number of arguments at (eval 1) line 41, <DATA> line 256.
configure.ccache    no

configure.perl      ${prefix}/bin/perl5

configure.args      --mandir=${prefix}/share/man \
                    --sysconfdir=${prefix}/etc/cyrus \
                    --with-cyrus-prefix=${prefix} \
                    --with-pidfile=${prefix}/var/run/cyrus-master.pid \
                    --with-bdb-incdir=${prefix}/include/db${db_ver} \
                    --with-bdb-libdir=${prefix}/lib/db${db_ver} \
                    --with-perl=${prefix}/bin/perl5 \
                    --with-sasl=${prefix} \
                    --with-openssl=${prefix} \
                    --with-cyrus-user=_cyrus \
                    --with-cyrus-group=mail \
                    --enable-cmulocal \
                    --enable-netscapehack \
                    --with-snmp=no \
                    --disable-sieve \
                    --disable-gssapi \
                    --with-com_err

startupitem.create  yes
startupitem.name    cyrus
startupitem.requires Disks Network "System Log"
startupitem.start   "${prefix}/bin/master -d"
startupitem.stop    "pidfile=${prefix}/var/run/cyrus-master.pid
            if \[ -f \${pidfile} \]; then
               kill -TERM \$(cat \${pidfile})
            fi"

post-destroot   {
        #add_users cyrus group=cyrus home=${prefix}/var/imap shell=/usr/bin/false passwd="\*"
        xinstall -m 755 -v ${worksrcpath}/tools/mkimap ${destroot}${prefix}/bin
        reinplace "s|/etc/imapd.conf|${prefix}/etc/cyrus/imapd.conf|" ${destroot}${prefix}/bin/mkimap
        xinstall -m 755 -v ${worksrcpath}/tools/mknewsgroups ${destroot}${prefix}/bin
        xinstall -m 755 -v ${worksrcpath}/tools/dohash ${destroot}${prefix}/bin
        xinstall -m 755 -v ${worksrcpath}/tools/rehash ${destroot}${prefix}/bin
        xinstall -m 755 -v ${worksrcpath}/tools/upgradesieve ${destroot}${prefix}/bin
        xinstall -d -m 755 ${destroot}${prefix}/share/doc/
        file copy ${worksrcpath}/doc ${destroot}${prefix}/share/doc/${name}
        foreach d [glob -directory ${destroot}${prefix}/share/doc/${name} -type d *] { file delete -force ${destroot}${prefix}/share/doc/${name}/html ${destroot}${prefix}/share/doc/${name}/CVS $d/CVS }
        xinstall -d -m 755 ${destroot}${prefix}/etc/cyrus/samples
        foreach f [glob -directory ${worksrcpath}/master/conf -type f *.conf] {
            xinstall -m 644 -v $f ${destroot}${prefix}/etc/cyrus/samples/[file tail $f].sample
        }
        xinstall -d -m 750 -o _cyrus -g mail ${destroot}${prefix}/var/imap
        xinstall -d -m 750 -o _cyrus -g mail ${destroot}${prefix}/var/spool/imap
        destroot.keepdirs ${destroot}${prefix}/var/run ${destroot}${prefix}/var/imap \
        ${destroot}${prefix}/var/spool/imap ${destroot}${prefix}/var/log
        delete {*}[glob ${destroot}${prefix}/lib/perl5/*/*/perllocal.pod]
}

platform powerpc {
        configure.args-append --build=powerpc
}

variant murder description {Enable IMAP Murder support} {
        configure.args-append --enable-murder
}

variant sieve description {Use sieve} {
        configure.args-delete --disable-sieve
        configure.args-append --enable-sieve
}

variant idled description {Build idled} {
        configure.args-append --enable-idled
}

platform i386 {
        configure.args-append --build=i386
}

variant snmp description {Use Net SNMP} {
        depends_lib-append      port:net-snmp
        configure.args-delete   --with-snmp=no
        configure.args-append   --with-snmp=yes
}

livecheck.type      regex
livecheck.url       ${homepage}
livecheck.regex     "/${name}/${name}-(\\d+(?:\\.\\d+)*)\\${extract.suffix}"

##
# Miscellaneous notes:
# -) Port is itself bare bones, support for many other things could be added (maybe as variants), so suggest away!
##
