# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6-base
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
_qtver=6.1.2
pkgver=${_qtver/-/}
pkgrel=2
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.qt.io'
license=(GPL3 LGPL3 FDL custom)
pkgdesc="A cross-platform application and UI framework (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-dbus"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-libmariadbclient"
            $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-firebird2" )
            $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-postgresql" ))
conflicts=("${MINGW_PACKAGE_PREFIX}-qt5"
           "${MINGW_PACKAGE_PREFIX}-qt5-debug"
           "${MINGW_PACKAGE_PREFIX}-qt5-static")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-xmlstarlet"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-firebird2" )
             $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-postgresql" ))
groups=(${MINGW_PACKAGE_PREFIX}-qt6)
_pkgfn="${_realname/6-/}-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/$_qtver/submodules/$_pkgfn.tar.xz")
sha256sums=('b9c4061c1c7999c42c315fc5b0f4f654067b4186066dd729bbcf1bdce8d781c8')

build() {
  cd ${srcdir}
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM}
  cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DINSTALL_INCLUDEDIR=include/qt6 \
    -DINSTALL_ARCHDATADIR=share/qt6 \
    -DINSTALL_DATADIR=share/qt6 \
    -DINSTALL_MKSPECSDIR=share/qt6/mkspecs \
    -DINSTALL_DESCRIPTIONSDIR=share/qt6/modules \
    -DQT_FEATURE_system_doubleconversion=ON \
    -DQT_FEATURE_system_freetype=ON \
    -DQT_FEATURE_system_jpeg=ON \
    -DQT_FEATURE_system_pcre2=ON \
    -DQT_FEATURE_system_png=ON \
    -DQT_FEATURE_system_sqlite=ON \
    -DQT_FEATURE_system_zlib=ON \
    -DQT_FEATURE_openssl=ON \
    -DQT_FEATURE_opengl=ON \
    -DQT_FEATURE_opengl_desktop=ON \
    -DQT_FEATURE_dbus=ON \
    -DQT_FEATURE_icu=ON \
    -DQT_FEATURE_fontconfig=ON \
    -DQT_FEATURE_pkg_config=ON \
    -DQT_FEATURE_sql_ibase=OFF \
    $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] && echo \
      "-DQT_FEATURE_sql_psql=OFF" || true ) \
    ../${_pkgfn}

  export PATH=$PWD/bin:$PATH

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

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

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $_pkgfn/LICENSE* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
}
