# Contributor: Sui Libin <bygreencn@gmail.com>

_realname=vlfeat
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.9.21
pkgrel=4
pkgdesc="The VLFeat open source library implements popular computer vision algorithms specializing in image understanding and local features extraction and matching. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.vlfeat.org/"
msys2_repository_url="https://github.com/vlfeat/vlfeat"
msys2_references=(
  'aur: vlfeat'
)
license=('spdx:BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
            "groff")
source=("https://www.vlfeat.org/download/${_realname}-${pkgver}.tar.gz"
        "01-vlfeat-0.9.20-1-buildsys.mingw.patch"
        "02-Makefile-fix-arch-detection.patch"
        "03-fix-openmp-building-with-gcc.patch"
        "04-remove-fpic-rpath-for-clang.patch")
sha256sums=('8cbf686fd008e3d119c3a0986ffd06e4c3d9448b6f4721a9cc9bb4964c82ff55'
            'ff1b998d5af23bcd3e0e5f4084d1d29a67e08c0b0a8c2f36829ad435278ffd5f'
            'b377ccec0b9934720111b1bed0afc420dd244747df80cd358ed92afc2c5d0d61'
            '84151ec314d5b63fee45eb91ac7c9a1181fb4361d41f40fc1707763c1ce5e55a'
            'afe3ff1fb934d768cf3dc7c5f498b2acc05f80d56194ca5653c1ef1bb3789f0e')

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

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # Replace minizip with external
  apply_patch_with_msg \
    01-vlfeat-0.9.20-1-buildsys.mingw.patch \
    02-Makefile-fix-arch-detection.patch

  # https://github.com/vlfeat/vlfeat/pull/200.patch
  apply_patch_with_msg \
    03-fix-openmp-building-with-gcc.patch

  apply_patch_with_msg \
    04-remove-fpic-rpath-for-clang.patch
}

build() {
  cd "${srcdir}"/${_realname}-${pkgver}

  if [[ ${CARCH} == aarch64 ]]; then
    export DISABLE_SSE2=yes
    export DISABLE_AVX=yes
  fi 

  DISABLE_OPENMP=no \
  make all
}

package() {
  cd "${srcdir}"/${_realname}-${pkgver}
  make DESTDIR="${pkgdir}" prefix=${MINGW_PREFIX} install
}
