# Maintainer: Ray Donnelly <mingwandroid@gmail.com>
# Upstream Maintainer: Balló György <ballogyor+arch at gmail dot com>
# Upstream Contributor: Sergej Pupykin <pupykin.s+arch@gmail.com>

_realname=frei0r-plugins
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.0
pkgrel=3
pkgdesc="Minimalistic plugin API for video effects"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://frei0r.dyne.org/"
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-gavl"
         "${MINGW_PACKAGE_PREFIX}-opencv")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("https://files.dyne.org/frei0r/$_realname-$pkgver.tar.gz"
        "https://files.dyne.org/frei0r/$_realname-$pkgver.tar.gz.asc"
        # https://github.com/dyne/frei0r/pull/101.diff
        "0000-Consolidated-opencv4-support.patch"
        "0001-Fix-autotools-build-for-opencv4.patch"
        "0002-fix-autoconf-fails-without-opencv.patch"
        "0003-Fix-out-of-source-tree-CXX-build-failures.patch"
        "0004-Support-MSYS2-mingw-w64.patch")
validpgpkeys=('6113D89CA825C5CEDD02C87273B35DA54ACB7D10') # Denis Roio (Jaromil)
sha256sums=('1b1ff8f0f9bc23eed724e94e9a7c1d8f0244bfe33424bb4fe68e6460c088523a'
            'SKIP'
            'c4a2ffe5e23e7755aee7b1db87bda029f99c4358c3715f04316fa29030e54f46'
            'da619d7f2fba7f775952092d747b9d129f3108c32ab8025decd2a2138ec29571'
            '474655eed3b2a5fe8179162f06c9ce4587854c4e3e15e91daf12b5d8ee0e0f17'
            '544823aba78bfb7f1830d8a69697a544d324bba06cb6431a554ceb266efb09b3'
            '9405b01f5a2564841fbce8b40cd47539b54f607c8c2e219a6e5fd3a6c5904b6c')


# =========================================== #
#   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}"

  plain "Consolidated opencv4 support (from upstream)"
  apply_patch_with_msg \
    0000-Consolidated-opencv4-support.patch

  plain "Autotools with OpenCV4 fix (upstreamed already)"
  apply_patch_with_msg \
    0001-Fix-autotools-build-for-opencv4.patch

  plain "Autotools OpenCV installation fix (from upstream)"
  apply_patch_with_msg \
    0002-fix-autoconf-fails-without-opencv.patch

  plain "General autotools fix (upstreaming .. hopefully)"
  apply_patch_with_msg \
    0003-Fix-out-of-source-tree-CXX-build-failures.patch

  plain "MSYS2/mingw-w64 autotools fixes (upstreaming .. hopefully)"
  apply_patch_with_msg \
    0004-Support-MSYS2-mingw-w64.patch

  # The 1.7.0 tarball seems to have incorrectly named files:
  cp AUTHORS.txt AUTHORS
  cp ChangeLog.txt ChangeLog
  cp COPYING.txt COPYING
  cp README.txt README.md
  cp INSTALL.txt INSTALL.md
  cp TODO.txt TODO

  ./autogen.sh
}

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure  \
    --prefix=${MINGW_PREFIX}

  plain "Start building..."
  make
}

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

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