# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6-mqtt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=6.10.0
pkgrel=1
pkgdesc='Qt Module to implement MQTT protocol version 3.1 and 3.1.1 (mingw-w64)'
arch=(any)
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.qt.io'
msys2_repository_url="https://code.qt.io/cgit/qt/qtmqtt.git"
msys2_references=(
  'archlinux: qt6-mqtt'
  "cpe: cpe:/a:qt:qt"
)
license=('spdx:GPL-3.0-only WITH Qt-GPL-exception-1.0 AND BSD-3-Clause AND GFDL-1.3-no-invariants-only')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "rsync")
_pkgfn="${_realname/6-/}-${pkgver}"
source=("https://github.com/qt/qtmqtt/archive/v${pkgver}/${_pkgfn}.tar.gz")
sha256sums=('da442c5e3fab6314f70ca6a6c2b9491018b15ef125e4c621bb3a825868841a6a')

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    --log-level=STATUS \
    -GNinja \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_pkgfn}

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

package_qt6-mqtt() {
  depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
           "${MINGW_PACKAGE_PREFIX}-qt6-base")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  # Seperate debug-info files
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs "${pkgdir}"/.${MINGW_PREFIX} "${srcdir}"/${MSYSTEM}-debug/

  install -d "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 ${_pkgfn}/LICENSES/* -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
}

package_qt6-mqtt-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base-debug"
           "${MINGW_PACKAGE_PREFIX}-${_realname}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6-debug")
  options=('!strip')

  cp -rf "${srcdir}"/${MSYSTEM}-debug${MINGW_PREFIX} "${pkgdir}"/
}

# 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;
