# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

# Use the right mkspecs file
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
  _platform=win32-clang-g++
else
  _platform=win32-g++
fi

_realname=qt5-doc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.15.17
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="A cross-platform application and UI framework (Documentation) (mingw-w64)"
url='https://www.qt.io/'
msys2_references=(
  'archlinux: qt5-doc'
  "cpe: cpe:/a:qt:qt"
)
license=('spdx:GFDL-1.3-no-invariants-only')
depends=("${MINGW_PACKAGE_PREFIX}-qt5-base")
makedepends=("${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-fxc2"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-z3"
             "${MINGW_PACKAGE_PREFIX}-assimp"
             "${MINGW_PACKAGE_PREFIX}-jasper"
             "${MINGW_PACKAGE_PREFIX}-libmng"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-libwebp"
             "${MINGW_PACKAGE_PREFIX}-openal"
             "${MINGW_PACKAGE_PREFIX}-xpm-nox"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
            )
groups=("${MINGW_PACKAGE_PREFIX}-qt5")
_pkgfile="qt-everywhere-opensource-src-${pkgver}"
_pkgfqn="qt-everywhere-src-${pkgver}"
noextract=(${_pkgfile}.tar.xz)
source=(https://download.qt.io/official_releases/qt/${pkgver%.*}/${pkgver}/single/${_pkgfile}.tar.xz
        0000-adjust-qmake-conf-mingw.patch
        0001-qt-5.8.0-fix-sql-libraries-mingw.patch
        0002-qt-5.8.0-configure-gcc-before-clang.patch
        0003-fix-build-with-perl-cygwin.patch
        0004-fix-linking-again-different-static-libs.patch
        0006-qt-5.3.0-win_flex-replace.patch
        0007-qt-5.3.0-win32-g-Enable-static-builds.patch
        0008-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch
        0011-qt-5.8.0-mingw-dbus-and-pkg-config.patch
        0016-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch
        0017-qt-5.3.0-fix-examples-building.patch
        0025-qt-5.8.0-force-using-make-on-msys.patch
        0028-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch
        0029-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch
        0030-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch
        0035-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch
        0036-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch
        0048-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch
        0049-qt-5.8.0-win32-do-not-use-fontconfig.patch
        0051-qt-5.9.1-disable-qtlocation-mapboxgl-plugin.patch
        0052-qt-5.11-mingw-fix-link-qdoc-with-clang.patch
        0054-win32-dont-need-flatpack-theme.patch
        0056-qt-5.11-static_icu.patch
        0057-fix-and-enable-iconv-codec.patch
        0060-activeqt-handle-win64-in-dumpcpp.patch
        0062-fix-static-build-big-resources.patch
        0063-enable-mingw-schannel-alpn.patch
        0064-fix-mingw-win32-winnt-detection.patch
        0125-qt5-windeployqt-fixes.patch
        0300-qt-5.8.0-cast-errors.patch
        0304-qtdeclarative-disable-dx12.patch
        0310-fix-assimp-not-found.patch)
sha256sums=('85eb566333d6ba59be3a97c9445a6e52f2af1b52fc3c54b8a2e7f9ea040a7de4'
            '884b32f0e2e1bb110330a921b69443379a7be98c42f13754f9c5f56c040ba3b7'
            '617e6fa85a92353d0073425d37cd5d90d92cb7f906d66dd2d0df576122d091a4'
            '27ce2161b5dbea2fcce6b15948ab722a71036d056420854f6554969f683583bb'
            'affd50f9865b8350e3797eb53b6c2ae8afd59a1c558258be0f27f5537c92f807'
            'e04033364f5ad77906b4100c34572df57bd793a55bc33b7d8a753a2cc60af259'
            '18fd2fa42215ac47b3b314261ab98cbe65f8231429e4f29a152288a3ca93daf1'
            '4e154fbc9059a096c351d019da6b18c907b1d8b06e028f48c7365f62bcd0edc9'
            'e2ec5e67bdcfd162a82c49181a2d480f4b193acdfa6b0e22f4a8448286162630'
            '465d3897c4494695b66ac121944b3bd370db91bfea71dd2c9170dbf268cc99ea'
            'b31929c78f9906756a5e1bdf9a796541865c474aaa1b3eefea1a3f1d7c8d94ca'
            'd8a476be7e55e8bb9362868073dc9e7937431e68b9578c0a0ed103cf1ebc8c01'
            '97fcd6bd58ff8b8d735a6381b607bb3b4b351d1d07be1bbc18b97155dcbb501d'
            'f80ce415f25f0aa4ead2efe369b41efe35d9848d4d36d8dbf3831e48dbd6e506'
            '19566c38bd50581acaa6a9f46f0e25c41fdfc8b0b42269126e4d3a9b83e2f224'
            'ddaa067d144f5788ab7a3ed0dfc426d702c13de9c35f04eaaf38d642be4934d4'
            '8beeb610df3aff14bae43c56e5781cda3d49f1ad23c6e3fc2aa2405ce002de46'
            'f008164ef2a224b8c52736bab53f1aeec72d610928f03cce57f632824dfdd615'
            '42724bd154ed98c612d19a7daee2b5028270ef6dbfe7f35d5f97c8d0605f9fe6'
            '3dc4d7c4fed8fedd726211edbfe13f4d78247d3ab2f789f3ce2a8e43e59a7c5b'
            'c84cb5ba4243413232bc693fb0f54c0283bf0e496e6790a2698f226f80275a61'
            '69e1d6f8fc59ee2da0abe0a6965d0d7730bd015c5b1f1c5b4a69435328b21768'
            '00e32052ac1a9a8391275dfe69901d4ae146ac1441c00bf46f3ef1cfe459a78d'
            '8a74fab12c9d3faac81d75ea1c0662624b83059631db8d9f2570b414d62dc692'
            'b878680172290ad2dcb5508827ef5a9230cbfd8e97d2672d2e96b5addbd55833'
            '928a8eaca8840ff353cdff8fd11140724cb89094e6115fdd7b8cf534830adcca'
            'f07ff4f72626b91aa67ce7ffc78ce7c7c5765bf5431a65b7d867b5f66fa1506b'
            'f930338562119f542b550e3cdb802f1f7ebd476f54dd7297db424271167394b0'
            '6fc871c9a3697666c19d5c21a8f038b80c9c825b418435b0723bf35a9e532d91'
            '28ad823dc8b6665de7b552e82411a8b7b25bf716c42597d8dc8644bded63199c'
            '9ea7aeb486023f3aebf1af44603ebd9266009aaeaf91e52c37c8c5154ea3900c'
            '052c7035ad9a4fc0321975ba7658fb755bbc0841c7e1d9e88bb0a15e6a90b770'
            '157b97b3e3031d93d93e45368a1c54ebfbf9578116f75f5126e977e5dd518191')


# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  [[ -d "${srcdir}"/${_pkgfqn} ]] && rm -rf "${srcdir}"/${_pkgfqn}
  tar -xJf "${srcdir}"/${_pkgfile}.tar.xz -C "${srcdir}" --exclude=${_pkgfqn}/{qtandroidextras,qtmacextras,qtwayland,qtwebengine,qtx11extras} || true

  cd "${srcdir}"/${_pkgfqn}

  # MSYS2 gperf cannot handle \r\n.
  find . -name "*.gperf" -exec dos2unix "{}" \;

  apply_patch_with_msg \
    0000-adjust-qmake-conf-mingw.patch \
    0001-qt-5.8.0-fix-sql-libraries-mingw.patch \
    0002-qt-5.8.0-configure-gcc-before-clang.patch \
    0003-fix-build-with-perl-cygwin.patch \
    0004-fix-linking-again-different-static-libs.patch \
    0006-qt-5.3.0-win_flex-replace.patch \
    0007-qt-5.3.0-win32-g-Enable-static-builds.patch \
    0008-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch \
    0011-qt-5.8.0-mingw-dbus-and-pkg-config.patch \
    0016-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch \
    0017-qt-5.3.0-fix-examples-building.patch

  apply_patch_with_msg \
    0025-qt-5.8.0-force-using-make-on-msys.patch

  # Ossi says we should fix this in qwebkit with a $$(shell_path), but the ' quotes get in the way
  # due to qtPrepareTool adding them with $$shell_quote
  apply_patch_with_msg \
    0028-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch \
    0029-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch \
    0030-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch \
    0035-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch

  # Patches so that qt5-static can be used with cmake.
  apply_patch_with_msg \
    0036-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch

  apply_patch_with_msg \
    0048-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch \
    0049-qt-5.8.0-win32-do-not-use-fontconfig.patch \
    0051-qt-5.9.1-disable-qtlocation-mapboxgl-plugin.patch \
    0052-qt-5.11-mingw-fix-link-qdoc-with-clang.patch \
    0054-win32-dont-need-flatpack-theme.patch \
    0056-qt-5.11-static_icu.patch \
    0057-fix-and-enable-iconv-codec.patch \
    0060-activeqt-handle-win64-in-dumpcpp.patch \
    0062-fix-static-build-big-resources.patch \
    0063-enable-mingw-schannel-alpn.patch \
    0064-fix-mingw-win32-winnt-detection.patch \
    0125-qt5-windeployqt-fixes.patch \
    0300-qt-5.8.0-cast-errors.patch \
    0304-qtdeclarative-disable-dx12.patch \
    0310-fix-assimp-not-found.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -i "s| -mthreads||g" qtbase/mkspecs/common/g++-win32.conf
  fi

  local _ARCH_TUNE=
  if [[ ${CARCH} == x86_64 ]]; then
    _ARCH_TUNE="-march=nocona -msahf -mtune=generic"
  fi

  BIGOBJ_FLAGS="-Wa,-mbig-obj"

  # Append these ones ..
  sed -i "s|^QMAKE_CFLAGS .*= \(.*\)$|QMAKE_CFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" qtbase/mkspecs/${_platform}/qmake.conf
  sed -i "s|^QMAKE_CXXFLAGS .*= \(.*\)$|QMAKE_CXXFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS}|g" qtbase/mkspecs/${_platform}/qmake.conf

  # To keep the build folder name quite small (PATH_MAX limit)
  # the source folder (long) is renamed to $MSYSTEM
  cd "${srcdir}"
  [[ -d ${MSYSTEM} ]] && rm -rf ${MSYSTEM}
  sleep 5
  mv ${_pkgfqn} ${MSYSTEM}

  cd ${MSYSTEM}
  ln -s ${MINGW_PREFIX}/bin qttools/
  ln -s ${MINGW_PREFIX}/bin/{rcc.exe,uic.exe,moc.exe} qtbase/bin/
}

build() {
  cd ${MSYSTEM}

  export PATH="${srcdir}/${MSYSTEM}/qtbase/bin:${PATH}"
  export LLVM_INSTALL_DIR=${MINGW_PREFIX}
  export VULKAN_SDK=${MINGW_PREFIX}
  export QT_PLUGIN_PATH=${MINGW_PREFIX}/share/qt5/plugins

  ./configure.bat \
    -prefix ${MINGW_PREFIX} \
    -datadir ${MINGW_PREFIX}/share/qt5 \
    -archdatadir ${MINGW_PREFIX}/share/qt5 \
    -examplesdir ${MINGW_PREFIX}/share/qt5/examples \
    -testsdir ${MINGW_PREFIX}/share/qt5/tests \
    -opensource \
    -confirm-license \
    -platform ${_platform} \
    -nomake examples \
    -nomake tests \
    -shared \
    -make-tool make \
    -I${MINGW_PREFIX}/include/mariadb \
    -opengl desktop \
    -jasper \
    -dbus \
    -icu \
    -fontconfig \
    -system-freetype \
    -system-harfbuzz \
    -system-libjpeg \
    -system-libpng \
    -system-tiff \
    -system-webp \
    -system-zlib \
    -system-pcre \
    -system-assimp \
    -system-doubleconversion \
    -no-iconv \
    -no-gstreamer \
    -no-wmf \
    -no-mng \
    -plugin-sql-psql \
    -plugin-sql-mysql \
    -plugin-sql-odbc

  make docs
}

package() {
  cd ${MSYSTEM}

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${srcdir}/${MSYSTEM}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find "${srcdir}/${MSYSTEM}" -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make INSTALL_ROOT="${pkgdir}" install_docs

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