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

_realname=argon2_cffi
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=18.3.0
_argom2_relname=argon2
_argon2_pkgver=20171227
pkgrel=1
pkgdesc="CFFI-based Argon2 Bindings for Python (mingw-w64)"
arch=('any')
url='https://github.com/hynek/argon2_cffi'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-cffi"
             "${MINGW_PACKAGE_PREFIX}-python2-enum34"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-six"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-cffi"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-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}/${_argon2_pkgver}-${argon2_pkgver}.tar.gz"
        mingw-fixes.patch
        002-mingw-fix-src.patch)
sha256sums=('14bd291406ae6f178ba7c56f0e1fdc87812c00dfa40fbbfa35cbf54413d56b78'
            'eaea0172c1f4ee4550d1b6c9ce01aab8d1ab66b4207776aa67991eb5872fdcd8'
            '5e06a2af6e3da607419b1e1897847f154794d6319683ce190d34d7af7f1be480'
            'cf5c9e1d35f51df87cb2da1ac214a4955b4c9404eee072dc825b2fb2e912e5df')

# 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/${_realname}-${pkgver}"
    cp -r $srcdir/phc-winner-${_argom2_relname}-${_argon2_pkgver}/* extras/libargon2/  
    apply_patch_with_msg mingw-fixes.patch
  popd
  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 pytest
  done
}

package_python3-argon2_cffi() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-cffi"
           "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
           "${MINGW_PACKAGE_PREFIX}-python3-six")

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

package_python2-argon2_cffi() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-cffi"
           "${MINGW_PACKAGE_PREFIX}-python2-enum"
           "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
           "${MINGW_PACKAGE_PREFIX}-python2-six")

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

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

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

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

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