# Maintainer: Jonathan White <support@dmapps.us>

_realname=argon2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=20190702
pkgrel=2
pkgdesc="This is the reference implementation of Argon2, the password-hashing function that won the Password Hashing Competition (PHC) (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/P-H-C/phc-winner-argon2'
license=('spdx:CC0-1.0 and Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=("${url}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        libargon2.pc
        001-mingw-fix-install.patch
        002-mingw-fix-src.patch)
sha256sums=('daf972a89577f8772602bf2eb38b6a3dd3d922bf5724d45e7f9589b5e830442c'
            '17061b6f504640cec638d69404879a678e15332a4138673f4a8626ff4efc79a3'
            '9b597bc9f83f1c8480c4dd129f5f1d39df6b290e3ede7f92083739ab8c57e749'
            '5f0566bb2a1d9a039f667cc35bc0afa803e65863c01765a37c574165f1d8eac1')

case ${MINGW_CHOST} in
  i686*)
    _march=i686
  ;;
  x86_64*)
    _march=x86-64
  ;;
esac

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd phc-winner-argon2-${pkgver}
  apply_patch_with_msg \
      001-mingw-fix-install.patch \
      002-mingw-fix-src.patch
}

build() {
  [[ -d "build-${MSYSTEM}" ]] && rm -rf "build-${MSYSTEM}"
  cp -rf "${srcdir}"/phc-winner-argon2-${pkgver} "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  make AR=${MINGW_PREFIX}/bin/ar OPTTARGET=${_march}
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make install DESTDIR="${pkgdir}" AR=${MINGW_PREFIX}/bin/ar PREFIX="${MINGW_PREFIX}" OPTTARGET=${_march}

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}"/libargon2.pc "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libargon2.pc"
  sed -e "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" \
      -e "s|@UPSTREAM_VER@|${pkgver}|g" \
      -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/libargon2.pc
}
