# Maintainer: Václav Šmilauer <eu@doxos.eu>

_realname=aravis
pkgbase=mingw-w64-${_realname}
pkgname=(
  "${MINGW_PACKAGE_PREFIX}-${_realname}"
  "${MINGW_PACKAGE_PREFIX}-${_realname}-gst"
)
pkgver=0.8.33
pkgrel=4
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://wiki.gnome.org/Projects/Aravis"
msys2_repository_url="https://github.com/AravisProject/aravis/"
msys2_references=(
  'aur: aravis'
)
license=('spdx:LGPL-2.1-or-later')
makedepends=(
   "${MINGW_PACKAGE_PREFIX}-cc"
   "${MINGW_PACKAGE_PREFIX}-meson"
   "${MINGW_PACKAGE_PREFIX}-pkgconf"
   "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
   "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
   "${MINGW_PACKAGE_PREFIX}-gstreamer"
   "${MINGW_PACKAGE_PREFIX}-gtk3"
   "${MINGW_PACKAGE_PREFIX}-libnotify"
   "${MINGW_PACKAGE_PREFIX}-libxml2"
   "${MINGW_PACKAGE_PREFIX}-libusb"
   "${MINGW_PACKAGE_PREFIX}-zlib"
   "rsync"
)
source=("https://github.com/AravisProject/aravis/releases/download/${pkgver}/${_realname}-${pkgver}.tar.xz")
sha256sums=('3c4409a12ea70bba4de25e5b08c777112de854bc801896594f2cb6f8c2bd6fbc')

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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix=${MINGW_PREFIX} \
    --buildtype=plain \
    -Ddocumentation=disabled \
    -Dgst-plugin=enabled \
    -Dintrospection=enabled \
    -Dusb=enabled \
    -Dviewer=enabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson compile
}

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

  # Add current build dirs to PATH to find DLLs
  PATH=$PWD/gst:$PWD/src:$PATH \
    ${MINGW_PREFIX}/bin/meson test || warning "Tests failed"
}

_move_aravis-gst_to_dst(){
    # move everything from $1 which is not the base lib to $2
    rsync -armR -v \
        --remove-source-files \
        --include='bin/arv-viewer*' \
        --exclude='bin/*'  \
        --exclude='include/' \
        --exclude='pkgconfig/' \
        --exclude='libaravis*' \
        --exclude='licenses/' \
        --exclude='gir-*/' \
        --exclude='girepository*/' \
        "${1}/./" "${2}"
    # delete empty directories in $1
    find "${1}" -type d -empty -delete
}

package_aravis() {
  pkgdesc="A vision library for genicam based cameras (library, headers, tools) (mingw-w64)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-libnotify"
    "${MINGW_PACKAGE_PREFIX}-libxml2"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-libusb"
    "${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime"
  )

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

  _TMP=`realpath _tmp`
  mkdir ${_TMP}
  _move_aravis-gst_to_dst "${pkgdir}${MINGW_PREFIX}" "${_TMP}"
  rm -rf ${_TMP}
}

package_aravis-gst() {
  pkgdesc="A vision library for genicam based cameras (Gtk3 viewer, GStreamer plugin) (mingw-w64)"
  depends=(
    "${MINGW_PACKAGE_PREFIX}-aravis"
    "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
    "${MINGW_PACKAGE_PREFIX}-gtk3"
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-good"
    "${MINGW_PACKAGE_PREFIX}-gstreamer"
  )

  # DESTDIR="$_TMP" ${MINGW_PREFIX}/bin/meson install
  cd "${srcdir}/build-${MSYSTEM}"
  _TMP=`realpath _tmp`
  DESTDIR="${_TMP}" ${MINGW_PREFIX}/bin/meson install
  _move_aravis-gst_to_dst "${_TMP}" "${pkgdir}"
  rm -rf ${_TMP}
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
