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

_realname=gammaray
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.1
pkgrel=1
pkgdesc="A tool for examining the internals of a Qt application and to some extent also manipulate it (mingw-64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://www.kdab.com/gammaray/"
msys2_repository_url="https://github.com/KDAB/GammaRay"
msys2_references=(
  'archlinux: gammaray'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-dlfcn"
         "${MINGW_PACKAGE_PREFIX}-syntax-highlighting")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-extra-cmake-modules"
             "${MINGW_PACKAGE_PREFIX}-qt6-3d"
             "${MINGW_PACKAGE_PREFIX}-qt6-connectivity"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-location"
             "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
             "${MINGW_PACKAGE_PREFIX}-qt6-scxml"
             "${MINGW_PACKAGE_PREFIX}-qt6-shadertools"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-glslang"
             "${MINGW_PACKAGE_PREFIX}-kcoreaddons")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-3d: 3D plugins"
            "${MINGW_PACKAGE_PREFIX}-qt6-declarative: Quick/Qml and positioning plugins"
            "${MINGW_PACKAGE_PREFIX}-qt6-scxml: state machine viewer plugin"
            "${MINGW_PACKAGE_PREFIX}-qt6-connectivity: bluetooth plugin"
            "${MINGW_PACKAGE_PREFIX}-qt5-positioning: positioning plugin"
            "${MINGW_PACKAGE_PREFIX}-qt6-svg: Widget Export actions plugin"
            "${MINGW_PACKAGE_PREFIX}-qt6-tools: Widget Export actions plugin"
            "${MINGW_PACKAGE_PREFIX}-kcoreaddons")
source=(${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        003-win32-not-elf.patch
        004-files-layout.patch
        005-fix-dll-naming.patch)
sha256sums=('270b35239f2473f9c9cded13498ddb8af82d589bf6518db776d837a73c9441d8'
            'b13e2caa213d0b83a37c72e57bb5dc6e1d7e3d06d3f3031ad8931df3059cd9c0'
            '5c7e44e3fa6e914e369c2149fe9ca4b9e133f6753c7c6539cef66c9aeac5da21'
            '1d4ebacca418f8f1c9df0c4c438742e63d5d372d37d1605ecf9b478b098155eb')
validpgpkeys=(E86C000370B1B9E2A9191AD53DBFB6882C9358FB) # KDAB Products <info@kdab.com>

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    003-win32-not-elf.patch \
    004-files-layout.patch \
    005-fix-dll-naming.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _extra_config+=("-DSTACK_DETAILS_BFD=ON")
  else
    _extra_config+=("-DSTACK_DETAILS_BFD=OFF")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DECM_MKSPECS_INSTALL_DIR=;-DPLUGIN_INSTALL_DIR=;" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_OLDINCLUDEDIR=${MINGW_PREFIX}/include/qt6 \
    -DTRANSLATION_INSTALL_DIR=${MINGW_PREFIX}/share/qt6/translations \
    -DPLUGIN_INSTALL_DIR=${MINGW_PREFIX}/share/qt6/plugins/gammaray \
    -DECM_MKSPECS_INSTALL_DIR=${MINGW_PREFIX}/share/qt6/mkspecs/modules \
    -DGAMMARAY_INSTALL_QT_LAYOUT=ON \
    -DGAMMARAY_MULTI_BUILD=FALSE \
    -DGAMMARAY_WITH_KDSME=OFF \
    -DSTACK_DETAILS_AUTO_DETECT=OFF \
    -DMINGW_MSVCR_LIBRARY="" \
    -DQT_VERSION_MAJOR=6 \
    "${_extra_config[@]}" \
    -S ${_realname}-${pkgver} \
    -B build-${MSYSTEM}

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

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
