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

_realname=argon2_cffi
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=19.2.0
_argom2_relname=argon2
_argon2_pkgver=20190702
pkgrel=2
pkgdesc="CFFI-based Argon2 Bindings for Python (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://github.com/hynek/argon2_cffi'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-cffi"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python-six")
options=('staticlibs' 'strip' '!debug')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/hynek/argon2_cffi/archive/${pkgver}.tar.gz"
        "${_argom2_relname}-${_argon2_pkgver}.tar.gz"::"https://github.com/P-H-C/phc-winner-argon2/archive/${_argon2_pkgver}.tar.gz"
        mingw-fixes.patch
        002-mingw-fix-src.patch)
sha256sums=('b7ad06a32aba8e795830c0248f8e6c464b7f3037442322937552a5e8b6013b7a'
            'daf972a89577f8772602bf2eb38b6a3dd3d922bf5724d45e7f9589b5e830442c'
            '5e06a2af6e3da607419b1e1897847f154794d6319683ce190d34d7af7f1be480'
            '5f0566bb2a1d9a039f667cc35bc0afa803e65863c01765a37c574165f1d8eac1')

# 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}"
  # include a patch from mingw-w64-argon2 for good measure.
  pushd "$srcdir/phc-winner-${_argom2_relname}-${_argon2_pkgver}"
    apply_patch_with_msg 002-mingw-fix-src.patch
  popd
  pushd "${srcdir}/argon2-cffi-${pkgver}"
    cp -r ${srcdir}/phc-winner-${_argom2_relname}-${_argon2_pkgver}/* extras/libargon2/
    apply_patch_with_msg mingw-fixes.patch
  popd

  rm -rf python-build-${CARCH} | true
  cp -r "argon2-cffi-${pkgver}" "python-build-${CARCH}"

  # Set version for setuptools_scm
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}
}

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

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

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"
}
