# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opencv
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.5.2
pkgrel=1
pkgdesc="Open Source Computer Vision Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://opencv.org/"
license=("BSD")
# 4.5.0 doesn't support ceres 2.x, wait for >4.5.0
depends=(#"${MINGW_PACKAGE_PREFIX}-gst-plugins-base
         "${MINGW_PACKAGE_PREFIX}-ceres-solver"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-ogre3d"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-tesseract-ocr"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         #"${MINGW_PACKAGE_PREFIX}-qt5"
         #"${MINGW_PACKAGE_PREFIX}-gtkglext"
         #"${MINGW_PACKAGE_PREFIX}-gtk2"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-python-flake8"
             "${MINGW_PACKAGE_PREFIX}-python-pylint"
             "${MINGW_PACKAGE_PREFIX}-tiny-dnn"
             #"${MINGW_PACKAGE_PREFIX}-vtk"
             "${MINGW_PACKAGE_PREFIX}-cereal"
            )
optdepends=(#"${MINGW_PACKAGE_PREFIX}-ceres-solver: SFM module"
            #"${MINGW_PACKAGE_PREFIX}-gflags: SFM module"
            #"${MINGW_PACKAGE_PREFIX}-glog: SFM module"
            "${MINGW_PACKAGE_PREFIX}-ffmpeg: support to read and write video files"
            "${MINGW_PACKAGE_PREFIX}-python-numpy: Python 3.x interface"
            #"${MINGW_PACKAGE_PREFIX}-hdf5: opencv_hdf module"
            #"${MINGW_PACKAGE_PREFIX}-tesseract-ocr: opencv_text module"
            #"${MINGW_PACKAGE_PREFIX}-ogre3d: ovis module"
            #"${MINGW_PACKAGE_PREFIX}-vtk: opencv_viz module"
            )
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/opencv/opencv/archive/${pkgver}.tar.gz
        "${_realname}_contrib-${pkgver}.tar.gz"::https://github.com/opencv/opencv_contrib/archive/${pkgver}.tar.gz
        '0001-mingw-w64-cmake.patch'
        '0002-solve_deg3-underflow.patch'
        '0003-issue-4107.patch'
        '0004-generate-proper-pkg-config-file.patch'
        '0008-mingw-w64-cmake-lib-path.patch'
        '0010-find-libpng-header.patch'
        '0012-make-header-usable-with-C-compiler.patch'
        '0014-python-install-path.patch'
        '0015-windres-cant-handle-spaces-in-defines.patch'
        '0101-somehow-uint-not-detected.patch'
        '0102-mingw-w64-have-sincos.patch'
        '0103-sfm-module-linking.patch'
        '0104-rgbd-module-missing-include.patch'
        '0105-wechat-iconv-dependency.patch')
sha256sums=('ae258ed50aa039279c3d36afdea5c6ecf762515836b27871a8957c610d0424f8'
            '9f52fd3114ac464cb4c9a2a6a485c729a223afb57b9c24848484e55cef0b5c2a'
            'e50a477ca9fb613400ba276099bb3be6af8f1bfb277ea86459f0d635c24d218f'
            '400f17bde074677566660b1baab52842c1a3d7024129a2d75af6015f6b55ba4f'
            '480e45906b54c5b6079f437de50ebf2152a83860327613048b53d038526b8ad2'
            'fcb3a4a469b09475dc2dad0c49bcaade8b80aa155eeaf0645398a77cd8c940cc'
            '7398e66f80be37382bd427b5eb3a1201a23113c14e71435a44df8779ea1b8a34'
            'd6ad5a0865eefe662ca4c7aceb6aa7b1fd5fcd27e1e65ca839d442f054095e69'
            '9f918a974e9d5227fce3702b1f38716a7fb79586dda9256b5df44dcc0f858c3b'
            '0c310a580d6700601d4b4f824b849c0f0d64d3953e249f04c6a91f15aa8bee0a'
            '11522ffedb22980ba8d09ff715a25327fe14806e55588bffa761e39e1d035ea5'
            '7d2ff25f97c84b59793502786dd64e25c8ca991b0523ffea56b45ce031e80c3f'
            '2001804c5245af1894a308d6521f9cd044fb6dbd713b6e2e45106399a409c14d'
            'd9d4da3c3f0c84b80fd14b882378c79c91435db9e33b9a6199d8eb260ed6eb13'
            'c6c92cf39dfe45b8fb41d80ac0de3cd304e8b695420b307fd4320a105d8fe9f4'
            '3cf6a17b234ddf4f20e042acce329823e970aa06873d63652fa132c46ee56739')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f ${_fname} ]; then
      rm -rf ${_fname}
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
        0001-mingw-w64-cmake.patch \
        0002-solve_deg3-underflow.patch \
        0003-issue-4107.patch \
        0004-generate-proper-pkg-config-file.patch \
        0008-mingw-w64-cmake-lib-path.patch \
        0010-find-libpng-header.patch \
        0012-make-header-usable-with-C-compiler.patch \
        0014-python-install-path.patch \
        0015-windres-cant-handle-spaces-in-defines.patch

  cd "${srcdir}/${_realname}_contrib-${pkgver}"
  apply_patch_with_msg \
        0101-somehow-uint-not-detected.patch \
        0102-mingw-w64-have-sincos.patch \
        0103-sfm-module-linking.patch \
        0104-rgbd-module-missing-include.patch \
        0105-wechat-iconv-dependency.patch
}

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

  CFLAGS+=" -D_POSIX_SOURCE -Wno-attributes"
  CXXFLAGS+=" -D_POSIX_SOURCE -Wno-attributes"

  export OpenEXR_HOME=${MINGW_PREFIX}
  export OpenBLAS_HOME=${MINGW_PREFIX}
  export TINYDNN_ROOT=${MINGW_PREFIX}/include/tiny_dnn

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DPYTHON2_PACKAGES_PATH=;-DPYTHON3_PACKAGES_PATH=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DWITH_OPENCL=ON \
    -DWITH_OPENGL=ON \
    -DWITH_TBB=ON \
    -DWITH_XINE=OFF \
    -DBUILD_WITH_DEBUG_INFO=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_PROTOBUF=OFF \
    -DPROTOBUF_UPDATE_FILES=ON \
    -DBUILD_EXAMPLES=OFF \
    -DINSTALL_C_EXAMPLES=OFF \
    -DINSTALL_PYTHON_EXAMPLES=OFF \
    -DWITH_GTK=OFF \
    -DWITH_QT=OFF \
    -DWITH_VTK=OFF \
    -DWITH_GDAL=OFF \
    -DWITH_MSMF=OFF \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=OFF \
    -DWITH_OPENJPEG=ON \
    -DWITH_OPENCL_D3D11_NV=OFF \
    -DOPENCV_SKIP_PYTHON_LOADER=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    -DENABLE_PRECOMPILED_HEADERS=OFF \
    -DOPENCV_EXTRA_MODULES_PATH=../${_realname}_contrib-${pkgver}/modules \
    -DBUILD_opencv_python2=OFF \
    -DBUILD_opencv_python3=ON \
    -DPYTHON_DEFAULT_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DPYTHON3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DPYTHON3_PACKAGES_PATH=$(cygpath -u $(${MINGW_PREFIX}/bin/python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")) \
    -DOPENCV_PYTHON3_VERSION=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))") \
    -DBUILD_opencv_bioinspired=OFF \
    -DOPENCV_GENERATE_PKGCONFIG=ON \
    -DOPENCV_PYTHON_INSTALL_PATH=lib \
    -DOPENCV_SKIP_CMAKE_ROOT_CONFIG=ON \
    ../${_realname}-${pkgver}

  make
}

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

  # install missing headers https://github.com/opencv/opencv/issues/13201
  for _module in imgcodecs videoio photo; do
    cp -r "${srcdir}"/${_realname}-${pkgver}/modules/${_module}/include/opencv2/${_module}/legacy \
      "${pkgdir}${MINGW_PREFIX}"/include/opencv4/opencv2/${_module}
  done
}
