# Maintainer: David Runge <dvzrv@archlinux.org>

_name=xapian-haystack
pkgname=python-xapian-haystack
pkgver=2.1.0.r7.g2247b23
_haystack_pkgver=2.8.1.r109.g7773b7f9
pkgrel=2
pkgdesc="Xapian backend for Django-Haystack"
arch=('any')
url="https://github.com/notanumber/xapian-haystack"
license=('GPL2')
depends=('python-django' 'python-django-haystack' 'python-six' 'python-xapian')
makedepends=('git' 'python-setuptools')
checkdepends=('python-pytest')
# tests not included in pypi sdist:
# https://github.com/notanumber/xapian-haystack/issues/191
# source=("https://files.pythonhosted.org/packages/source/${_name::1}/${_name}/${_name}-${pkgver}.tar.gz")
# release on pypi not on github:
# https://github.com/notanumber/xapian-haystack/issues/190
# source=("${_name}-${pkgver}.tar.gz::https://github.com/notanumber/xapian-haystack/archive/${pkgver}.tar.gz")
# using very recent version, as otherwise integration with django 3 is broken completely
# using the current python-django-haystack version to test against
source=("${_name}-${pkgver}::git+https://github.com/notanumber/xapian-haystack#commit=2247b23"
        "${pkgname}-2.1.0-xapian_settings.patch"
        "django-haystack-${_haystack_pkgver}::git+https://github.com/django-haystack/django-haystack#commit=7773b7f95599701d391983df6bacbe394033c0a6"
        "django-haystack-2.8.1-settings.patch")
sha512sums=('SKIP'
            '6866210a6b079ae2ecc905aa065f51c5b0e7647c644a20d1bde79779749b24cdb23b4105d847bd2779d92c5cfbe77f10bce88c4ae7ab657d194e99707085970e'
            'SKIP'
            '76016be102d21e3bb6734a7505e805c6cc28d749c94b948efc2456a045044057915b609d1ad254b91683e2670f0e7fa14802d9bea9a979cc6b411c2e2c5975f6')

pkgver() {
  cd "$pkgname-$pkgver"
  git describe --long --tags | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  mv -v "${_name}-${pkgver}" "$pkgname-$pkgver"
  (
    cd "$pkgname-$pkgver"
    # using python-six instead of relying on broken legacy way of importing from python-django
    sed -e 's/from django.utils import six/import six/' -i xapian_backend.py
    # fixing broken xapian_settings.py
    patch -Np1 -i "../${pkgname}-2.1.0-xapian_settings.patch"
  )
  (
    cd "django-haystack-${_haystack_pkgver}"
    # fixing broken settings.py
    patch -Np1 -i "../django-haystack-2.8.1-settings.patch"
  )
}

build() {
  cd "$pkgname-$pkgver"
  python setup.py build
}

check() {
  cd "$pkgname-$pkgver"
  # inserting xapian-haystack tests into django-haystack (because... pain)
  ln -sv "${PWD}/build/lib/xapian_backend.py" \
    ../django-haystack-${_haystack_pkgver}/haystack/backends/
  ln -sv "${PWD}/tests/"{xapian_tests,xapian_settings.py} \
    ../django-haystack-${_haystack_pkgver}/test_haystack/
  ln -svf "${PWD}/tests/xapian_tests/__init__.py" \
    ../django-haystack-${_haystack_pkgver}/test_haystack/
  cd "../django-haystack-${_haystack_pkgver}"
  PYTHONPATH="${PWD}:${PYTHONPATH}" django-admin.py test test_haystack.xapian_tests --settings=test_haystack.xapian_settings
}

package() {
  cd "$pkgname-$pkgver"
  python setup.py install --skip-build \
    --optimize=1 \
    --prefix=/usr \
    --root="${pkgdir}"
  install -vDm 644 {AUTHORS,README.rst} -t "${pkgdir}/usr/share/doc/${pkgname}"
}
