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

pkgname=mingw-w64-clang-aarch64-qt6-svg
_qtver=6.11.0
pkgver=${_qtver/-/}
pkgrel=2.1
arch=(any)
url='https://www.qt.io'
license=(GPL-3.0-only
         LGPL-3.0-only
         LicenseRef-Qt-Commercial
         Qt-GPL-exception-1.0)
pkgdesc='Classes for displaying the contents of SVG files (mingw-w64-clang-aarch64)'
depends=('mingw-w64-clang-aarch64-qt6-base')
makedepends=('mingw-w64-clang-aarch64-cmake' 'qt6-base' 'ninja')
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs')
groups=(mingw-w64-clang-aarch64-qt6)
_pkgfqn="qtsvg-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfqn}.tar.xz"
        '0001-Fix-zero-width-strokes-on-paths.patch')
sha256sums=('dfa8d653be07087d9407ed4a4ebae847f8953e0b7abd829f089803ab652a30e6'
            '145ba50fdee6cf5deabc31f5cfff968effa6c97b31b6c1daf20957d352b457b6')

_architectures=${MINGW_W64_QT6_ARCHS:-aarch64-w64-mingw32}

prepare () {
  cd $_pkgfqn

  # apply patches; further descriptions can be found in patch files itself
  for patch in "$srcdir/"*.patch; do
    msg2 "Applying patch $patch"
    patch -p1 -i "$patch"
  done
}

build() {
  for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
    export PKG_CONFIG=/usr/bin/$_arch-pkg-config

    # workaround https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120495
    [[ $pkgname =~ .*-clang-.* ]] || export CXXFLAGS+=' -Wno-template-body -fcoroutines'

    $_arch-cmake -G Ninja -B build-$_arch -S $_pkgfqn \
      -DQT_NO_PACKAGE_VERSION_CHECK:BOOL=TRUE \
      -DFEATURE_pkg_config=ON
    cmake --build build-$_arch
  done
}

package() {
  for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
    DESTDIR="$pkgdir" cmake --install build-$_arch

    # Add symlinks of DLLs in usual bin directory
    mkdir -p "$pkgdir/usr/bin" "$pkgdir/usr/$_arch/bin"
    for dll in "$pkgdir"/usr/$_arch/lib/qt6/bin/*.dll; do
        ln -rs "$dll" "$pkgdir/usr/$_arch/bin/${dll##*/}"
    done

    # Drop QMAKE_PRL_BUILD_DIR because reference the build dir
    find "$pkgdir/usr/$_arch/lib" -type f -name '*.prl' \
      -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;

    find "$pkgdir/usr/$_arch" -iname '*.exe' -exec $STRIP --strip-all {} \;
    find "$pkgdir/usr/$_arch" -iname '*.dll' -exec $STRIP --strip-unneeded {} \;
    find "$pkgdir/usr/$_arch" -iname '*.a'   -exec $STRIP -g {} \;
    [[ -d "$pkgdir/usr/$_arch/share/doc" ]] && rm -r "$pkgdir/usr/$_arch/share/doc"
  done

  install -d "$pkgdir"/usr/share/licenses
  ln -s /usr/share/licenses/mingw-w64-clang-aarch64-qt6-base "$pkgdir"/usr/share/licenses/$pkgname

}
