# Maintainer: Martchus <martchus@gmx.net>
<%== content_for 'additional_contributors' %>\

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

<%== content_for 'comment_header' %>\
pkgname=mingw-w64-qt6-<%== $qt_module %><%== $static_suffix %>
<%== content %>\
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs')
groups=(mingw-w64-qt6)
_pkgfqn="qt<%== $qt_module %>-everywhere-src-${_qtver}"
<%== include 'fragments/qt6-source' %>\
<%== include 'fragments/source_file_list', relevant_files => $patch_files %>)
sha256sums=('<%== stash('git_commit') ? 'SKIP' : $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
<%== content_for 'before_build_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_pkg_config=ON<%== content_for 'additional_cmake_args' %>
    cmake --build build-$_arch
  done
}

package() {
  for _arch in ${_architectures}; do
<%== content_for 'before_package_install' %>\
    DESTDIR="$pkgdir" cmake --install build-$_arch<%== content_for 'after_package_install' %>

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

% }
% unless (stash('no_libs')) {
    # 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

  install -d "$pkgdir"/usr/share/licenses
  ln -s /usr/share/licenses/mingw-w64-qt6-base<%== $static_variant ? '-static' : '' %> "$pkgdir"/usr/share/licenses/$pkgname

}
