# Maintainer: Smo <smo@xpra.org>

_realname=pyu2f
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.1.4
pkgrel=1
pkgdesc="pyu2f is a python based U2F host library for Linux, Windows, and MacOS. It provides functionality for interacting with a U2F device over USB (mingw-w64)"
arch=('any')
url='https://github.com/google/pyu2f/'
license=('Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
options=('staticlibs' 'strip' '!debug')

source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/pyu2f/archive/${pkgver}.tar.gz")
sha512sums=('16f948b812edd5f0e4d97350872809b7c1b52f38eea49170e4aa21e0f695935dc0d71d4fa077ee1c1a3ef2f0711d62f0cc49983a89c841aa31aadf7b76340035')

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}"
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=$pkgver
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py check
  done
}

package_python3-pyu2f() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"
}

package_python2-pyu2f() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"
}

package_mingw-w64-i686-python2-pyu2f() {
  package_python2-pyu2f
}

package_mingw-w64-i686-python3-pyu2f() {
  package_python3-pyu2f
}

package_mingw-w64-x86_64-python2-pyu2f() {
  package_python2-pyu2f
}

package_mingw-w64-x86_64-python3-pyu2f() {
  package_python3-pyu2f
}
