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

_realname=argon2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=20171227
pkgrel=3
pkgdesc="This is the reference implementation of Argon2, the password-hashing function that won the Password Hashing Competition (PHC) (mingw-w64)"
arch=('any')
license=('CC0 and Apache 2.0')
url='https://github.com/P-H-C/phc-winner-argon2'
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
source=(${_realname}-${pkgver}.tar.gz::"${url}/archive/${pkgver}.tar.gz"
        libargon2.pc
        001-mingw-fix-install.patch
        002-mingw-fix-src.patch)
sha256sums=('eaea0172c1f4ee4550d1b6c9ce01aab8d1ab66b4207776aa67991eb5872fdcd8'
            '17061b6f504640cec638d69404879a678e15332a4138673f4a8626ff4efc79a3'
            '3417ce1955960912ad943552fe458963a4da23edc2369ab75ffe87ba6b6225f5'
            'cf5c9e1d35f51df87cb2da1ac214a4955b4c9404eee072dc825b2fb2e912e5df')

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-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  cp -rf ${srcdir}/phc-winner-argon2-${pkgver} "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  make CC=${MINGW_PREFIX}/bin/gcc OPTTARGET=${_march}
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}" CC=${MINGW_PREFIX}/bin/gcc 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
}
