# Maintainer: Martchus <martchus@gmx.net>

# All my PKGBUILDs are managed at https://github.com/Martchus/PKGBUILDs where
# you also find the URL of a binary repository.

_reponame=qtutilities
pkgname=mingw-w64-qtutilities-qt6
_name=${pkgname#mingw-w64-}
_name=${_name%-qt6}
pkgver=6.16.0
pkgrel=1.1
arch=('any')
pkgdesc='Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models (mingw-w64)'
license=(GPL-2.0-or-later)
depends=('mingw-w64-crt' 'mingw-w64-qt6-base' 'mingw-w64-c++utilities')
optdepends=("$_name-doc: API documentation")
checkdepends=('mingw-w64-wine')
makedepends=('mingw-w64-gcc' 'mingw-w64-cmake' 'mingw-w64-qt6-tools' 'qt6-base' 'qt6-tools' 'qt6-declarative' 'clang' 'ninja')
url="https://github.com/Martchus/${_reponame}"
source=("${_name}-${pkgver}.tar.gz::https://github.com/Martchus/${_reponame}/archive/v${pkgver}.tar.gz")
sha256sums=('625d722e5ce23e8ba9aa884d4a9e9c1b5a267f934c5f8d9f3c1208b1493ed9f5')
options=(!buildflags staticlibs !strip !emptydirs)

_architectures=(${MINGW_W64_QT6_ARCHS:-x86_64-w64-mingw32})
_configurations=()
[[ $NO_SHARED_LIBS ]] || _configurations+=('shared')
[[ $NO_STATIC_LIBS ]] || _configurations+=('static') makedepends+=('mingw-w64-cmake-static' 'mingw-w64-qt6-base-static')

build() {
  declare -A _config_flags=(
    [shared]='
        -DBUILD_SHARED_LIBS:BOOL=ON
    '
    [static]='
        -DBUILD_SHARED_LIBS:BOOL=OFF
        -DCMAKE_FIND_LIBRARY_SUFFIXES:STRING=.a;.lib
        -DSTATIC_LINKAGE:BOOL=ON
        -DSTATIC_LIBRARY_LINKAGE:BOOL=ON
        -DBUILTIN_TRANSLATIONS:BOOL=ON
        -DSVG_SUPPORT:BOOL=OFF
        -DSVG_ICON_SUPPORT:BOOL=OFF
    '
  )
  declare -A _cfg_suffix=(
    [static]='-static'
  )
  for _arch in "${_architectures[@]}"; do
    for _cfg in "${_configurations[@]}"; do
      msg2 "${_arch}-${_cfg}"
      ${_arch}-cmake${_cfg_suffix[$_cfg]} \
        -G Ninja \
        -S "$srcdir/${PROJECT_DIR_NAME:-$_reponame-$pkgver}" \
        -B "build-${_arch}-${_cfg}" \
        -DCMAKE_BUILD_TYPE:STRING='Release' \
        -DCMAKE_INSTALL_PREFIX="/usr/${_arch}" \
        -DCONFIGURATION_NAME:STRING="${_cfg}-qt6" \
        -DCONFIGURATION_DISPLAY_NAME="" \
        -DCONFIGURATION_PACKAGE_SUFFIX:STRING="-${_cfg}" \
        -DCONFIGURATION_TARGET_SUFFIX:STRING="qt6" \
        -DQT_PACKAGE_PREFIX:STRING=Qt6 \
        -DKF_PACKAGE_PREFIX:STRING=KF6 \
        -DENABLE_TARGETS_FOR_MINGW64_CROSS_PACKAGING:BOOL=ON \
        -DSETUP_TOOLS:BOOL=ON \
        ${_config_flags[$_cfg]}
      VERBOSE=1 cmake --build "build-${_arch}-${_cfg}"
    done
  done
}

check() {
  for _arch in "${_architectures[@]}"; do
    for _cfg in "${_configurations[@]}"; do
      msg2 "${_arch}-${_cfg}"
      export WINEPATH="/usr/${_arch}/bin" WINEDEBUG=-all
      export QT_QPA_PLATFORM=offscreen
      [[ $_cfg == shared ]] && cmake --build "build-${_arch}-${_cfg}" --target check || true
    done
  done
}

package() {
  for _arch in "${_architectures[@]}"; do
    for _cfg in "${_configurations[@]}"; do
      msg2 "${_arch}-${_cfg}"
      DESTDIR="${pkgdir}" cmake --build "build-${_arch}-${_cfg}" --target install-mingw-w64-strip
    done
  done
}
