# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=unittest2
pkgbase=mingw-w64-python-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-python2-${_realname}"
pkgver=1.1.0
pkgrel=1
pkgdesc="New features in unittest for Python 2 (mingw-w64)"
url="http://www.voidspace.org.uk/python/articles/unittest2.shtml"
arch=('any')
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python2-six"
         "${MINGW_PACKAGE_PREFIX}-python2-traceback2")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
source=(${_realname}-${pkgver}.tar.gz::https://hg.python.org/unittest2/archive/${pkgver}.tar.gz
        LICENSE
       9badd4cde9ab.patch
       d091f0086b03.patch)
sha512sums=('ccd701194994623422d5c0bdd88d89b279cbbc67cd96b3cfee7f48e1c32f87c154a3378771e2909afa89d9c13692173ccbd692312b1ad7b9d05cf8c115fcc14b'
            '85242ed21eb191fa0bab16f3e390957f1c8b7bc3b79aefbea2d925feaa8248f33d6e22afaa7d1c07c5fcb5e800910aa2a1cb52d892c104337bbdb57a380373a2'
            'ed9aea429d2821b1c7448e84af8ed07127c91918d042b4b59dc80bfd23858e6698749ca97a00d4985c9d6a14056ac9bfa649135bd163668cfb0a2b63427b9e2f'
            'c045a4f1f464083a2eb1917f1d7712181c862cb5a6e6d88ca836ae7259f557a53b0d6939fc59b7965701fb83a791afce7867d01b3215c2b327e83153ed09f41e')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"
  pushd  "${_realname}-${pkgver}"
    apply_patch_with_msg 9badd4cde9ab.patch \
      d091f0086b03.patch
  popd
 # sed -i "s/'argparse', //" ${_realname}-${pkgver}/setup.py
  rm -rf python2-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python2-build-${CARCH}"
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
  export PBR_VERSION=$pkgver
}

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

package() {
  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --skip-build -O1 --root="${pkgdir}"
  install -Dm 644 "${srcdir}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${pkgname}/LICENSE"
  install -Dm 644 README.txt "${pkgdir}${MINGW_PREFIX}/share/doc/${pkgname}/README"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}/bin/|/usr/bin/env |g" -i ${_f}
  done
}

# vim: ts=2 sw=2 et:
