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

_realname=vigra
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.12.2
_pkgver=${pkgver//./-}
pkgrel=6
pkgdesc="vigra - Generic Programming for Computer Vision (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://ukoethe.github.io/vigra/'
msys2_repository_url="https://github.com/ukoethe/vigra"
msys2_references=(
  'anitya: 15957'
  'archlinux: vigra'
  'cpe: cpe:2.3:a:vigra_computer_vision_library_project:vigra_computer_vision_library'
)
license=('spdx:X11')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-imath"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-python-pytest"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
optdepends=("${MINGW_PACKAGE_PREFIX}-boost: For VigraNumpy"
            "${MINGW_PACKAGE_PREFIX}-python-numpy: For VigraNumpy")
source=(https://github.com/ukoethe/vigra/archive/Version-${_pkgver}/${_realname}-${pkgver}.tar.gz
        0001-vigra-numpy-detection.patch
        0002-fix-configuring-with-cmake.patch
        0014-python-module.ext.patch
        0015-cmake-files-location.patch
        0016-Fix-FindOpenEXR.patch)
sha256sums=('e927477a8a0bcdabcaab914e425782f7eba82711dbaca02538dfda79476f6b7e'
            '3eb264733fa81e3605000048e292a76b5bb4727d7c42fd1114dc66eacfa6b012'
            'fbc265cdc6e73e7ae566a11a2835660cc88509801e1351837499a6a02d58a696'
            'b981e9837a864badf6d892623d33565848dea2dbe4f669535c94a0d0282dfbed'
            '06e3f1ca5ad4dfa5a96194843445bbe6b6f9cf14865630ac0bae38f2beec9d41'
            '2572717e39f916c7c463b8f49306b683c5923906be2721f7da181c58b3a2b34b')

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

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

  apply_patch_with_msg \
    0001-vigra-numpy-detection.patch \
    0002-fix-configuring-with-cmake.patch \
    0014-python-module.ext.patch \
    0015-cmake-files-location.patch \
    0016-Fix-FindOpenEXR.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    ${extra_config[@]} \
    -DDOCINSTALL=share/doc \
    -DWITH_BOOST_GRAPH=ON \
    -DWITH_VIGRANUMPY=ON \
    -DWITH_OPENEXR=ON \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DBoost_USE_MULTITHREADED=ON \
    ../${_realname}-Version-${_pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/vigra/*.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}-Version-${_pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt
}
