# -*- 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 92398 2012-04-27 19:02:38Z ryandesign@macports.org $

PortSystem              1.0
PortGroup               github 1.0
PortGroup               python27 1.0

github.setup            fail2ban fail2ban 0.8.6
categories              security python
license                 GPL-2+
maintainers             free.fr:fclaire openmaintainer
platforms               darwin
homepage                http://www.fail2ban.org
description             Fail2ban protects computer against brute-force attacks

long_description        Fail2ban scans log files (e.g. /var/log/apache/error_log) and \
                        bans IPs that show the malicious signs -- too many password failures, \
                        seeking for exploits, etc. Generally Fail2Ban then used to update \
                        firewall rules to reject the IP addresses for a specified amount \
                        of time, although any arbitrary other action (e.g. sending an email, \
                        or ejecting CD-ROM tray) could also be configured. Out of the box \
                        Fail2Ban comes with filters for various services (apache, curier, ssh, etc).

checksums               sha256  24d02422c55f1b7d92bea30a7dde6f72f280eadf09d478fcca8b1dd40fb4e914 \
                        rmd160  70678ca46849a462367603fb3d0e2c6c2f3b75cf

set f2bconfdir          ${prefix}/etc/${name}
set f2bbindir           ${prefix}/bin

python.link_binaries_suffix

patchfiles              patch-setup.py.diff \
                        patch-setup.cfg.diff \
                        patch-fail2ban-client.diff \
                        patch-fail2ban-regex.diff \
                        patch-fail2ban-server.diff \
                        patch-config-fail2ban.conf.diff \
                        patch-client-csocket.py.diff \
                        patch-server-server.py.diff

post-patch   {
    reinplace "s|@@PREFIX@@|${prefix}|g"    ${worksrcpath}/setup.py \
                                            ${worksrcpath}/setup.cfg \
                                            ${worksrcpath}/fail2ban-client \
                                            ${worksrcpath}/fail2ban-regex \
                                            ${worksrcpath}/fail2ban-server \
                                            ${worksrcpath}/client/csocket.py \
                                            ${worksrcpath}/config/fail2ban.conf \
                                            ${worksrcpath}/server/server.py
}

startupitem.create      yes
startupitem.start       "${f2bbindir}/${name}-client start"
startupitem.stop        "${f2bbindir}/${name}-client stop"

destroot.keepdirs       ${destroot}${prefix}/var/run/fail2ban

post-destroot {
    # Adding a suffix to config files
    set cfgfiles [concat [glob ${destroot}${f2bconfdir}/*.conf] [glob ${destroot}${f2bconfdir}/action.d/*.conf] [glob ${destroot}${f2bconfdir}/filter.d/*.conf]]
    foreach cfgfile ${cfgfiles} {
        move ${cfgfile} ${cfgfile}.${version}
    }
}

post-activate {
    # If not already present we put default config files
    set cfgfiles [concat [glob ${f2bconfdir}/*.conf.${version}] [glob ${f2bconfdir}/action.d/*.conf.${version}] [glob ${f2bconfdir}/filter.d/*.conf.${version}]]
    foreach cfgfile ${cfgfiles} {
        regsub "\.${version}" ${cfgfile} {} origcfgfile
        if {![file exists ${origcfgfile}]} {
            file copy ${cfgfile} ${origcfgfile}
        }
    }
}
