# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Andrea Scarpino <andrea@archlinux.org>

pkgbase=qt6-base-24hms
pkgname=(qt6-base-24hms)

_pkgver=6.11.1
pkgver=${_pkgver/-/}
pkgrel=1.4
arch=(x86_64)
url='https://www.qt.io'
license=(GPL-3.0-only
         LGPL-3.0-only
         LicenseRef-Qt-Commercial
         Qt-GPL-exception-1.0)
pkgdesc='A cross-platform application and UI framework'
depends=(libicui18n.so brotli
         dbus
         double-conversion
         fontconfig
         freetype2
         glib2
         glibc
         harfbuzz
         icu
         krb5
         libb2
         libcups
         libdrm
         libgcc
         libgl
         libice
         libinput
         libjpeg-turbo
         libpng
         libproxy
         libsm
         libstdc++
         liburing
         libx11
         libxcb
         libxkbcommon
         libxkbcommon-x11
         md4c
         mesa
         mtdev
         openssl
         pcre2
         shared-mime-info
         sqlite
         systemd-libs
         tslib
         wayland
         xcb-util-cursor
         xcb-util-image
         xcb-util-keysyms
         xcb-util-renderutil
         xcb-util-wm
         xdg-utils
         zlib
         zstd)
makedepends=(python alsa-lib
             cmake
             cups
             freetds
             git
             gst-plugins-base-libs
             gtk3
             jemalloc
             libfbclient
             libpulse
             mariadb-libs
             ninja
             postgresql
             renderdoc
             unixodbc
             vulkan-headers
             xmlstarlet)
optdepends=('freetds: MS SQL driver'
            'gdk-pixbuf2: GTK platform plugin'
            'gtk3: GTK platform plugin'
            'libfbclient: Firebird/iBase driver'
            'mariadb-libs: MariaDB driver'
            'pango: GTK platform plugin'
            'perl: for syncqt'
            'postgresql-libs: PostgreSQL driver'
            'unixodbc: ODBC driver')

provides=("qt6-base=$pkgver")
conflicts=(qt6-base "qt6-3d>$pkgver" "qt6-5compat>$pkgver" "qt6-base>$pkgver" "qt6-charts>$pkgver" "qt6-connectivity>$pkgver" "qt6-datavis3d>$pkgver" "qt6-declarative>$pkgver" "qt6-doc>$pkgver" "qt6-examples>$pkgver" "qt6-imageformats>$pkgver" "qt6-lottie>$pkgver" "qt6-multimedia>$pkgver" "qt6-networkauth>$pkgver" "qt6-positioning>$pkgver" "qt6-quick3d>$pkgver" "qt6-quicktimeline>$pkgver" "qt6-remoteobjects>$pkgver" "qt6-scxml>$pkgver" "qt6-sensors>$pkgver" "qt6-serialbus>$pkgver" "qt6-serialport>$pkgver" "qt6-shadertools>$pkgver" "qt6-svg>$pkgver" "qt6-tools>$pkgver" "qt6-translations>$pkgver" "qt6-virtualkeyboard>$pkgver" "qt6-wayland>$pkgver" "qt6-webchannel>$pkgver" "qt6-webengine>$pkgver" "qt6-websockets>$pkgver" "qt6-webview>$pkgver" )
_pkgfn=qtbase
source=(
      oldherl-24hms.patch
      0001-text-input-v3-use-dashed-underline-instead-of-solid-.patch
      0002-text-input-v3-always-update-cursor-rectangle-after-e.patch
      'https://build.archlinuxcn.org/~oldherl/files/cldr/47/core.zip'
      'https://iso639-3.sil.org/sites/iso639-3/files/downloads/iso-639-3.tab'
      git+https://code.qt.io/qt/$_pkgfn#tag=v$_pkgver
        qt6-base-cflags.patch
        qt6-base-nostrip.patch)
sha256sums=(
      '0c845268bde9718fbd76ad759b8a51352a5fddd30dbd76aaa7d4dbf5ea010151'
      8e21c0a5d4c5b05b9a235dcde7b9ddb1217181183b37d42e472753999a7f65d7
      bae52d3f395be34c444d9f0e9368ad9f900269818c93e247f207c502a1d6b48e
      'd5ee2abac64158c04884a722f8ef4830ea22b6c74aac20185be2838db8eda788'
      '9660ebcab661e7a6bbb194a6c031fb89bea532af4f34fa5d99d653c20d9562cb'
      '2eafe504fae873d20f206b5661e2e10506879455cb2d370f42c5bb72ccf7a8a1'
            '5411edbe215c24b30448fac69bd0ba7c882f545e8cf05027b2b6e2227abc5e78'
            '4b93f6a79039e676a56f9d6990a324a64a36f143916065973ded89adc621e094')

prepare() {
  patch -d $_pkgfn -p1 < qt6-base-cflags.patch # Use system CFLAGS
  patch -d $_pkgfn -p1 < qt6-base-nostrip.patch # Don't strip binaries with qmake

  cd $_pkgfn
  patch -p1 -i ../oldherl-24hms.patch
  patch -p1 -i ../0001-text-input-v3-use-dashed-underline-instead-of-solid-.patch
  patch -p1 -i ../0002-text-input-v3-always-update-cursor-rectangle-after-e.patch
  cd util/locale_database
  echo "This is slow. It takes about 4 minutes on my desktop."
  ./cldr2qlocalexml.py ../../../ > ./24h.xml
  ./qlocalexml2cpp.py ./24h.xml ../../../iso-639-3.tab ../..
}

build() {
  # Set no_direct_extern_access based on architecture
  if [[ $CARCH == "aarch64" || $CARCH == "riscv64" ]]; then
    _no_direct_extern_access=OFF
  else
    _no_direct_extern_access=ON
  fi

  cmake -B build -S $_pkgfn -G Ninja \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DINSTALL_BINDIR=lib/qt6/bin \
    -DINSTALL_PUBLICBINDIR=bin \
    -DINSTALL_LIBEXECDIR=lib/qt6 \
    -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 \
    -DFEATURE_journald=ON \
    -DFEATURE_libproxy=ON \
    -DFEATURE_openssl_linked=ON \
    -DFEATURE_system_sqlite=ON \
    -DFEATURE_system_xcb_xinput=ON \
    -DFEATURE_no_direct_extern_access=$_no_direct_extern_access \
    -DFEATURE_mimetype_database=OFF \
    -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON \
    -DCMAKE_MESSAGE_LOG_LEVEL=STATUS
  cmake --build build
}

package_qt6-base-24hms() {
pkgdesc='A cross-platform application and UI framework. This package uses 24-hour HH:mm:ss notation in all locales.'
  depends+=(qt6-translations)
  DESTDIR="$pkgdir" cmake --install build

  install -Dm644 $_pkgfn/LICENSES/* -t "$pkgdir"/usr/share/licenses/$pkgbase
}

nopackage_qt6-xcb-private-headers() {
  pkgdesc='Private headers for Qt6 Xcb'

  depends=("qt6-base=$pkgver")
  optdepends=()
  groups=()

  cd $_pkgfn
  install -d -m755 "$pkgdir"/usr/include/qt6xcb-private/gl_integrations
  cp -r src/plugins/platforms/xcb/*.h "$pkgdir"/usr/include/qt6xcb-private/
  cp -r src/plugins/platforms/xcb/gl_integrations/*.h "$pkgdir"/usr/include/qt6xcb-private/gl_integrations/
}
