# Maintainer: Luke T. Shumaker <lukeshu@parabola.nu>
# Maintainer (aur:python-spf-engine): Morgenstern <charles [at] charlesbwise [dot] com> 
# Maintainer (aur:python-postfix-policyd-spf): Hao Zhang theivorytower [at] gmail [dot] com>
# Maintainer (aur:python2-postfix-policyd-spf): Filip S. Adamsen <fsa [at] fsadev [dot] com>
# Contributor (aur:python2-postfix-policyd-spf): Samed Beyribey <ras0ir@eventualis.org>
#
# Rationale: A key part of Parabola infrastructure

pkgname=spf-engine
pkgver=3.1.0
pkgrel=4
pkgdesc="SPF (Sender Policy Framework) back-end for Postfix policy and Sendmail milter"
arch=('any')
url="https://launchpad.net/${pkgname}/"
license=('Apache-2.0' 'GPL-2.0-only')

replaces=(pypolicyd-spf)
provides=(
	python-spf-engine # /usr/lib/python*/site-packages/{}/
	policyd-spf       # /usr/bin/{}
	pyspf-milter      # /usr/bin/{}
	pypolicyd-spf     # old package name
)
conflicts=("${provides[@]}")

depends=('python-authres' 'python-pyspf')
optdepends=('python-pymilter: For pyspf-milter (Sendmail) support')
makedepends=('python-build' 'python-installer' 'python-wheel' 'python-flit' "${optdepends[@]%%:*}")

backup=(etc/python-policyd-spf/policyd-spf.conf
        etc/pyspf-milter/pyspf-milter.conf)

# The published (sdist) tarball excludes the tests from Git
# (literally: `exclude = ["test_suite"`), but that's OK because the
# tests (1) require functioning network, and (2) seem to depend on
# real DNS entries that have since been changed.

source=("${url}/${pkgver%.*}/${pkgver}/+download/${pkgname}-${pkgver}.tar.gz"{,.asc})
md5sums=('1704f1869df135a754315b09612eeebc'
         'SKIP')
sha256sums=('1d4b8cc58142a88b4b1603129eb9307e6256aa0146c88d515a09a58dbfa39169'
            'SKIP')
validpgpkeys=('E7729BFFBE85400FEEEE23B178D7DEFB9AD59AF1') # Donald Scott Kitterman

prepare() {
  cd "$pkgname-$pkgver"

  # Fix pyspf version check.
  sed -i 's/if int(micro) < 9:/if int(maj) < 2 or (int(maj) == 2 and int(minor) == 0 and int(micro) < 9):/' spf_engine/__init__.py
}

build() {
  cd "$pkgname-$pkgver"
  python -m build --wheel --no-isolation
}

package() {
  _python_version=$(pacman -S --print-format='%v' python|cut -d- -f1|cut -d. -f1,2)
  depends+=("python>=${_python_version}" "python<${_python_version%.*}.$(( ${_python_version##*.} + 1 ))")

  cd "$pkgname-$pkgver"
  python -m installer --destdir="$pkgdir" dist/*.whl

  # Fix directory structure and file locations.
  mv -T -- "${pkgdir}/usr/etc" "${pkgdir}/etc"
  sed -i \
    -e 's|\/usr\/local\/bin|\/usr\/bin|' \
    -e 's|\/usr\/local\/etc|\/etc|' \
    "${pkgdir}/usr/lib/systemd/system/pyspf-milter.service"

  # Have the default pyspf-milter.conf listen on a filesystem socket,
  # not a TCP socket.
  sed -i \
    -e 's|\#Socket|Socket|' \
    -e '/inet/d' \
    "${pkgdir}/etc/pyspf-milter/pyspf-milter.conf"

  # Don't ship init scripts; they're crusty and suitable for neither
  # systemd nor openrc.
  rm -rf -- "${pkgdir}/etc/init.d"
}
