# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=pyelftools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.27
pkgrel=1
pkgdesc='Python library for analyzing ELF files and DWARF debugging information (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/eliben/pyelftools"
license=('custom:Public Domain')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('!emptydirs')
source=("https://github.com/eliben/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('2958edd7c32909f1d541b8122797fee903db700d02d168b2789aa6b667fc4a30')

prepare() {  
  cd "$srcdir"
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  msg "Python build for ${CARCH}"  
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python test/run_all_unittests.py || warning "Tests failed"
  ${MINGW_PREFIX}/bin/python test/run_examples_test.py || warning "Tests failed"
  if [ "${CARCH}" == "x86_64" ]; then
    ${MINGW_PREFIX}/bin/python test/run_readelf_tests.py || warning "Tests failed"
  else
    echo "Skipping readelf tests (require x86_64)"
  fi
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -Dm644 README.rst CHANGES -t "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/"
  sed -e '1 { s/^#!.*$// }' -i "${pkgdir}${MINGW_PREFIX}/bin/readelf.py"
  cp -r examples "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/"
}
