# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Andrea Scarpino <andrea@archlinux.org>

pkgname=qt5-tools
_basever=5.15.15
pkgver=5.15.15+kde+r3
pkgrel=2
_commit=0378d3e541f40bc9c5433eac70d3949ddff9cfc4
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
url='https://www.qt.io'
license=('GPL3' 'LGPL3' 'FDL' 'custom')
pkgdesc='A cross-platform application and UI framework (Development Tools, QtHelp)'
depends=('qt5-base' 'hicolor-icon-theme')
makedepends=('qt5-declarative' 'vulkan-headers' 'llvm' 'clang' 'git')
optdepends=('clang: for qdoc')
groups=('qt5')
conflicts=('qtchooser')
_pkgfqn=${pkgname/5-/}
source=(kde-$_pkgfqn::git+https://invent.kde.org/qt/qt/$_pkgfqn#commit=$_commit
        {assistant,designer,linguist,qdbusviewer}.desktop)
sha256sums=('0af01c5dee5c2534ac89cf082bd6049bb662ecf6bc07524793ae4a4a40aac1cb'
            'f1ae05a739879a79f5754d08047800217cf97351b328e9a6c5213ca06bbd4871'
            '692e9dcf4004ba9f273cf86f0faf83c325956d0037ec4348eb21d8ed7e15bb7c'
            'a1ce14940138609397a43cc4b0c06d76a0ff927051cdd38a9cdfde7df41d184b'
            'ee6cc8e70ea726332130b795931eaacec671fc8ed99496fe2cdd0a8d6ef4d72f')

pkgver() {
  cd kde-$_pkgfqn
  echo "$_basever+kde+r"`git rev-list --count v$_basever-lts-lgpl..$_commit` | sed -e 's|+kde+r0||'
}

prepare() {
  mkdir -p build
}

build() {
  cd build

  qmake ../kde-$_pkgfqn CONFIG+=fat-static-lto
  make
}

package() {
  cd build
  make INSTALL_ROOT="${pkgdir}" install

  cd ../kde-$_pkgfqn
  # install missing icons and desktop files
  for icon in src/linguist/linguist/images/icons/linguist-*-32.png ; do
    size=$(echo $(basename ${icon}) | cut -d- -f2)
    install -p -D -m644 ${icon} \
      "${pkgdir}/usr/share/icons/hicolor/${size}x${size}/apps/linguist.png"
  done

  install -D -m644 src/assistant/assistant/images/assistant.png \
    "${pkgdir}/usr/share/icons/hicolor/32x32/apps/assistant.png"
  install -D -m644 src/assistant/assistant/images/assistant-128.png \
    "${pkgdir}/usr/share/icons/hicolor/128x128/apps/assistant.png"
  install -D -m644 src/designer/src/designer/images/designer.png \
    "${pkgdir}/usr/share/icons/hicolor/128x128/apps/QtProject-designer.png"
  install -D -m644 src/qdbus/qdbusviewer/images/qdbusviewer.png \
    "${pkgdir}/usr/share/icons/hicolor/32x32/apps/qdbusviewer.png"
  install -D -m644 src/qdbus/qdbusviewer/images/qdbusviewer-128.png \
    "${pkgdir}/usr/share/icons/hicolor/128x128/apps/qdbusviewer.png"
  install -d "${pkgdir}/usr/share/applications"
  install -m644 "${srcdir}"/{linguist,designer,assistant,qdbusviewer}.desktop \
    "${pkgdir}/usr/share/applications/"

  # Symlinks for backwards compatibility
  for b in "${pkgdir}"/usr/bin/*; do
    ln -s $(basename $b) "${pkgdir}"/usr/bin/$(basename $b)-qt5
  done

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

  install -d "$pkgdir"/usr/share/licenses
  ln -s /usr/share/licenses/qt5-base "$pkgdir"/usr/share/licenses/${pkgname}
}
