# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Geoffroy Carrier <geoffroy.carrier@koon.fr>
# Contributor: michalzxc
# Contributor: nbags <neilbags@gmail.com>

pkgname=fail2ban
pkgver=0.10.4
pkgrel=1
pkgdesc='Bans IPs after too many failed authentication attempts'
url='https://www.fail2ban.org/'
license=('GPL')
arch=('any')
depends=('iptables' 'python-pyinotify' 'whois' 'sqlite' 'python-systemd')
makedepends=('python-setuptools')
checkdepends=('python-coverage')
backup=(etc/fail2ban/fail2ban.conf
        etc/fail2ban/jail.conf
        etc/logrotate.d/fail2ban)
source=("$pkgname-$pkgver.tar.gz::https://github.com/fail2ban/$pkgname/archive/$pkgver.tar.gz"
        "https://github.com/fail2ban/fail2ban/pull/2174/commits/12df4ec92c27a25a300482f7707f27f7522f592f.patch")
sha512sums=('3f4af84b7e3332b887240c927c1f706d2b3020217df2a68c64897619d54eb6dfa972992e3153f4ea150d025e2c8a2b537da47cf71a6dfee1df3c8d029a6d5f42'
            '081df2a7d76800a91d66b97a9a86f63b8f6e30c80eb5e192167b0e69debe5143035bf916eb6e105512ed323b797a0f20a1d80a43a68ddadd83d878ce094450d3')

prepare() {
  cd $pkgname-$pkgver
  # fix tmpfiles.d warning in systemd 239
  # https://github.com/fail2ban/fail2ban/pull/2174
  patch -p1 -i ../12df4ec92c27a25a300482f7707f27f7522f592f.patch
  sed -i 's|self.install_dir|"/usr/bin"|' setup.py
  sed -i 's/^before = paths-debian.conf/before = paths-arch.conf/' config/jail.conf
}

build() {
  cd $pkgname-$pkgver
  ./fail2ban-2to3
  python setup.py build
}

check() {
  # make temp copy to run coverage since it messes with file permissions
  cp -r $pkgname-$pkgver{,-coverage}
  cd $pkgname-$pkgver-coverage
  coverage run bin/fail2ban-testcases || warning "Tests failed"
}

package() {
  cd $pkgname-$pkgver
  python setup.py install --root "$pkgdir" --optimize=1

  install -Dm644 build/fail2ban.service \
    "$pkgdir"/usr/lib/systemd/system/$pkgname.service
  install -Dm644 files/fail2ban-tmpfiles.conf \
    "$pkgdir"/usr/lib/tmpfiles.d/$pkgname.conf
  install -Dm644 files/fail2ban-logrotate \
    "$pkgdir"/etc/logrotate.d/fail2ban
  install -Dm644 files/bash-completion \
    "$pkgdir"/usr/share/bash-completion/completions/fail2ban

  # fix sendmail location
  sed -i 's/sbin/bin/g' "$pkgdir"/etc/fail2ban/action.d/sendmail*.conf

  install -Dm644 man/fail2ban.1 "$pkgdir"/usr/share/man/man1/fail2ban.1
  install -Dm644 man/fail2ban-client.1 \
    "$pkgdir"/usr/share/man/man1/fail2ban-client.1
  install -Dm644 man/fail2ban-regex.1 \
    "$pkgdir"/usr/share/man/man1/fail2ban-regex.1
  install -Dm644 man/fail2ban-server.1 \
    "$pkgdir"/usr/share/man/man1/fail2ban-server.1
  install -Dm644 man/jail.conf.5 "$pkgdir"/usr/share/man/man5/jail.conf.5

  rm -r "$pkgdir/run"
}
