# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=pyalpm
pkgbase="python-${_realname}"
pkgname=("python2-${_realname}" "python3-${_realname}")
pkgver=0.8
pkgrel=3
pkgdesc="Libalpm bindings for Python"
arch=('i686' 'x86_64')
license=('GPL')
url="https://projects.archlinux.org/users/remy/pyalpm.git/"
depends=("libarchive-devel")
makedepends=('python2'
             'python3'
             'gettext-devel'
             'heimdal-devel'
             'libarchive-devel'
             'libcurl-devel'
             'libgpgme-devel'
             'pacman')
source=("https://sources.archlinux.org/other/${_realname}/${_realname}-${pkgver}.tar.gz"
        "0001-Python2-compat-Module-initialization.patch"
        "0002-Python2-compat-Use-Py_BuildValue-s-file-name.patch"
        "0003-Python2-compat-pycman-from-__future__-import-print_f.patch"
        "0004-Python2-compat-python3-python-coding-utf-8.patch"
        "0005-Use-pkg-config-add-LIBALPM_STATIC-env.-var.patch")
sha256sums=('b83b50a8a03b38954c84a280ea627e7c5a750cd6451530e0418c097ab0476e22'
            '489a3556bea9e22e809d7be31beb2dcaa74a3f776acd304d4a31cc1cfc2c547a'
            '37f510ad8b66317b68125e7b2c4d7fee8396a0e795c145c443bfb3770187bed4'
            '4b64a72f86cb6df75f37acf96de50148314ae1f44ae5ebd011b4631a7b5abcb8'
            '0faf882eda91d360a346f24591cf51d94f5f8b9ed0f8c4291e8610bb5932db98'
            '7588f18f1c8546957dc1acba1a08d1a0bf14f7a5c3a6efcb8cc8a4278928ce06')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p0 -i "${srcdir}"/0001-Python2-compat-Module-initialization.patch
  patch -p0 -i "${srcdir}"/0002-Python2-compat-Use-Py_BuildValue-s-file-name.patch
  patch -p0 -i "${srcdir}"/0003-Python2-compat-pycman-from-__future__-import-print_f.patch
  patch -p0 -i "${srcdir}"/0004-Python2-compat-python3-python-coding-utf-8.patch
  patch -p0 -i "${srcdir}"/0005-Use-pkg-config-add-LIBALPM_STATIC-env.-var.patch
  
  # The git repo uses symlinks in the scripts folder to
  # the actual source files in the pycman folder. Patch
  # files created by git will therefore not contain any
  # changes to the files in scripts so copy the patched
  # ones across now.
  pushd "${srcdir}"/${_realname}-${pkgver}
    for _file in deptest query remove sync upgrade version; do
      cp -f pycman/action_${_file}.py scripts/pycman-${_file}
    done
  popd
}

build() {
  cd "$srcdir"
  rm -rf python{2,3}-build
  for builddir in python{2,3}-build; do
    cp -r ${_realname}-${pkgver} ${builddir}
    pushd ${builddir} > /dev/null 2>&1
    LIBALPM_STATIC=1 \
      /usr/bin/${builddir%-build} setup.py build
    popd > /dev/null 2>&1
  done
}

package_python3-pyalpm() {
  depends=("python3")

  cd "$srcdir/python3-build"
  /usr/bin/python3 setup.py install --prefix=/usr --root="${pkgdir}" -O1
}

package_python2-pyalpm() {
  depends=("python2")

  cd "$srcdir/python2-build"
  /usr/bin/python2 setup.py install --prefix=/usr --root="${pkgdir}" -O1

  # Avoid conflicts.
  mv "${pkgdir}"/usr/bin/lsoptdepends "${pkgdir}"/usr/bin/lsoptdepends2
  pushd "${pkgdir}"/usr/bin > /dev/null 2>&1
    for _file in database deptest query remove sync upgrade version; do
      mv pycman-${_file} pycman2-${_file}
    done
  popd > /dev/null 2>&1
}
