# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>

_realname=vigra
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.11.1
_pkgver=${pkgver//./-}
pkgrel=4
pkgdesc="vigra - Generic Programming for Computer Vision (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://ukoethe.github.io/vigra/'
license=('MIT X11')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-python-nose"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/ukoethe/vigra/releases/download/Version-${_pkgver}/${_realname}-${pkgver}-src.tar.gz
        0001-vigra-numpy-detection.patch
        0002-doc-destination.patch
        0003-vigra-cmath-std-namespace.patch
        0010-fix-incorrect-template-parameter.patch
        0011-fix-receive-from-PyUnicode_AsUTF8.patch
        0012-hdf5-fixes.patch
        0013-fix-build-hdf5-1.12.patch
        0014-python-module.ext.patch
        0015-cmake-files-location.patch)
sha256sums=('a5564e1083f6af6a885431c1ee718bad77d11f117198b277557f8558fa461aaf'
            '3395713d051f53b69979a4a610fd88a440b320ca5d0c0b6e944caf3fa3e2cbcd'
            'f214f448616aa9c4e05d5a83738bd14a48be40dfc4f6cfa233a9a81e76bfe23d'
            'b70f6f2d3873188febf406cb2bceeac45a5c5f47f14f2b481ff3a9cdb66f5d51'
            'f151f902483dfa2b1f3d431f54bb161300cf184158c9f416fa653d19ab363cc4'
            '754c0a67a2909512f696a56d4fb9c5253204f8db87389a9588de39a4fa03ffd2'
            '8f09d4b8e4044f20db1095c7232080bb9dff9743c2e2437ca43e5e88dc24a3a4'
            'bd3c224458a022c5255259e0a81bf26181de798b487fd257d1d21e05b5aa7d4c'
            '6f2a91b882ca2cab044655c97252dbbf1d49dc798b739ad62b169c5ef35b8e6b'
            '06e3f1ca5ad4dfa5a96194843445bbe6b6f9cf14865630ac0bae38f2beec9d41')

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

  patch -p1 -i "${srcdir}/0001-vigra-numpy-detection.patch"
  patch -p1 -i "${srcdir}/0002-doc-destination.patch"
  patch -p1 -i "${srcdir}/0003-vigra-cmath-std-namespace.patch"

  patch -p1 -i "${srcdir}/0010-fix-incorrect-template-parameter.patch"
  patch -p1 -i "${srcdir}/0011-fix-receive-from-PyUnicode_AsUTF8.patch"
  patch -p1 -i "${srcdir}/0012-hdf5-fixes.patch"
  patch -p1 -i "${srcdir}/0013-fix-build-hdf5-1.12.patch"
  patch -p1 -i "${srcdir}/0014-python-module.ext.patch"
  patch -p1 -i "${srcdir}/0015-cmake-files-location.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DWITH_BOOST_GRAPH=ON \
    -DWITH_VIGRANUMPY=ON \
    -DWITH_OPENEXR=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ../${_realname}-${pkgver}

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  #make check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" -j1 install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/{vigra,vigranumpy}/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
