# Maintainer: Martchus <martchus@gmx.net>
<%== content_for 'additional_contributors' %>\
# Based on: AUR packages qt6-base-git and mingw-w64-qt6-base-git, official qt5-base package

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

# This file is created from PKGBUILD.sh.ep contained by the mentioned repository.
# Do not edit it manually! See README.md in the repository's root directory
# for more information.
% if ($patch_files->size) {

# All patches are managed at https://github.com/Martchus/qt<%== $qt_module %>
% }

pkgname=<%= $package_name %>
_qtver=6.1.1
pkgver=${_qtver/-/}
pkgrel=1
arch=(any)
url='https://www.qt.io'
license=(GPL3 LGPL3 FDL custom)
pkgdesc='A cross-platform application and UI framework (mingw-w64)'
depends=('mingw-w64-crt' 'mingw-w64-zlib' 'mingw-w64-libjpeg-turbo' 'mingw-w64-sqlite'
         'mingw-w64-libpng' 'mingw-w64-openssl' 'mingw-w64-dbus' 'mingw-w64-harfbuzz'
         'mingw-w64-brotli' 'mingw-w64-pcre2' 'mingw-w64-zstd')
makedepends=('mingw-w64-cmake<%= $static_suffix %>' 'mingw-w64-postgresql' 'mingw-w64-mariadb-connector-c'
             'mingw-w64-vulkan-headers' 'mingw-w64-vulkan-icd-loader' 'mingw-w64-pkg-config'
             'qt6-base' 'ninja')
optdepends=('mingw-w64-postgresql: PostgreSQL driver'
            'mingw-w64-mariadb-connector-c: MariaDB driver'
            'qt6-base: development tools')
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs')
groups=(mingw-w64-qt6)
_pkgfqn="qt<%== $qt_module %>-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfqn}.tar.xz"\
<%== include 'fragments/source_file_list', relevant_files => $patch_files %>)
sha256sums=('<%== $qt_module_sha256 %>'\
<%== include 'fragments/sha256_list', relevant_files => $patch_files %>)

_architectures='i686-w64-mingw32 x86_64-w64-mingw32'

% if ($patch_files->size || content_for('prepare')->size) {
prepare () {
  cd $_pkgfqn

% if ($patch_files->size) {
  # 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
% }
<%== content_for 'prepare' %>\
}

% }
build() {
  for _arch in ${_architectures}; do
    export PKG_CONFIG=/usr/bin/$_arch-pkg-config
    $_arch-cmake<%== $static_suffix %> -G Ninja -B build-$_arch -S $_pkgfqn \\
% if ($static_variant) {
      -DCMAKE_INSTALL_PREFIX:PATH="/usr/$_arch/static" \\
      -DFEATURE_static_runtime=ON \\
      -DFEATURE_openssl_linked=ON \\
% }
      -DFEATURE_pkg_config=ON \\
      -DFEATURE_system_pcre2=ON \\
      -DFEATURE_system_freetype=ON \\
      -DFEATURE_system_harfbuzz=ON \\
      -DFEATURE_system_sqlite=ON \\
      -DINSTALL_BINDIR=lib/qt6/bin \\
      -DINSTALL_DOCDIR=share/doc/qt6 \\
      -DINSTALL_ARCHDATADIR=lib/qt6 \\
      -DINSTALL_DATADIR=share/qt6 \\
      -DINSTALL_INCLUDEDIR=include/qt6 \\
      -DINSTALL_MKSPECSDIR=lib/qt6/mkspecs \\
      -DINSTALL_EXAMPLESDIR=share/doc/qt6/examples \\
      -DINPUT_openssl=<%== $static_variant ? 'linked' : 'runtime' %>
    VERBOSE=1 cmake --build build-$_arch
  done
}

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

    install -Dm644 $_pkgfqn/LICENSE* -t "$pkgdir"/usr/share/licenses/$pkgname

% unless ($static_variant) {
    # 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

    # Symlinks for backwards compatibility
    for qmake; do
        ln -rs "$pkgdir"/usr/$_arch/lib/qt6/bin/$_b "$pkgdir"/usr/bin/$_arch-$_b-qt6
    done

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

    find "$pkgdir/usr/$_arch" -iname '*.exe' -exec $_arch-strip --strip-all {} \;
    find "$pkgdir/usr/$_arch" -iname '*.dll' -exec $_arch-strip --strip-unneeded {} \;
    find "$pkgdir/usr/$_arch" -iname '*.a'   -exec $_arch-strip -g {} \;
    [[ -d "$pkgdir/usr/$_arch/<%== $static_variant ? 'static/' : '' %>share/doc" ]] && rm -r "$pkgdir/usr/$_arch/<%== $static_variant ? 'static/' : '' %>share/doc"
  done
}
