# 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=wasm-qt6-tools
_qtver=6.7.1
pkgver=${_qtver/-/}
pkgrel=1.1
arch=(any)
url='https://www.qt.io'
license=(GPL3 LGPL3 FDL custom)
pkgdesc='A cross-platform application and UI framework (tools, wasm)'
depends=('wasm-qt6-base')
makedepends=('wasm-qt6-declarative' 'qt6-declarative' 'qt6-tools' 'cmake' 'ninja')
options=('!strip' '!buildflags' 'staticlibs' '!emptydirs')
groups=(wasm-qt6)
_pkgfqn="qttools-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfqn}.tar.xz"
        '0001-Enable-only-SQL-plugins-which-are-known-to-work.patch')
sha256sums=('0953cddf6248f3959279a10904892e8a98eb3e463d729a174b6fc47febd99824'
            '99912e1e29e30040281c009c2d60a235c9902d209b185f61e694f5cb96133bef')

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() {
  export EMSDK=/usr/lib/emscripten
  /usr/lib/emscripten/emcmake cmake -G Ninja -B build -S $_pkgfqn \
      -DCMAKE_FIND_ROOT_PATH:PATH="/usr/lib/emscripten/system" \
      -DCMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES="/usr/lib/emscripten/system/include" \
      -DCMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES="/usr/lib/emscripten/system/include" \
      -DCMAKE_INSTALL_PREFIX:PATH="/usr/lib/emscripten/system" \
      -DBUILD_SHARED_LIBS=OFF \
      -DFEATURE_static_runtime=ON \
      -DFEATURE_thread=ON \
      -DQT_INCLUDE_DIRS_NO_SYSTEM=ON \
      -DQT_HOST_PATH=/usr
  VERBOSE=1 cmake --build build
}

package() {
  DESTDIR="$pkgdir" cmake --install build

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

  find "${pkgdir}"/usr/lib/emscripten/system -type f -name 'lib*.so' -exec /usr/lib/emscripten/emstrip -g --strip-unneeded {} \;
  find "${pkgdir}"/usr/lib/emscripten/system -type f -name 'lib*.a' -exec /usr/lib/emscripten/emstrip -g {} \;
  [[ -d "${pkgdir}"/usr/lib/emscripten/system/share/doc ]] && rm -r "${pkgdir}"/usr/lib/emscripten/system/share/doc || true

}
