NAME="qt5"
DESC="Cross-platform application framework that is widely used for developing application software with a graphical user interface"
VERSION="5.15.12"

FILES=(
  "https://download.qt.io/archive/qt/${VERSION%.*}/${VERSION}/single/qt-everywhere-opensource-src-${VERSION}.tar.xz"
  "qt-everywhere-opensource-src-5.15.12-security_fix-1.patch"
  "qt-everywhere-opensource-src-5.15.12-kf5-1.patch"
  "assistant-qt5.desktop"
  "designer-qt5.desktop"
  "linguist-qt5.desktop"
  "qdbusviewer-qt5.desktop"
)
HASHES=(
  "3fb1cd4f763f5d50d491508b7b99fb77"
  "82257f13e8c3b85955bf1d0750049b945dedadc3f2f76960f3d922347372b1da"
  "50db2eb15bada7e87ddbe43a9a6a8f2844513356dd9c9adc9f4fedfc12b0db6d"
  "162c1b77fa48db234f483148337c57f3850d9749cfdb8660054a8c81a52c1f4b"
  "945d209667d4bdb01ddb9d5ee4968dee55e4386f0927246995de8bda93b0c274"
  "27b1237daf07ac00b9035f607744c42f5824455f20f94e3f1ac9eed64508542c"
  "5ce3d8b7550bdd1423c786b6ef543564aacaec61a62fdee1601bea249ff794cb"
)

DEPENDS=(
  "xorg" "fontconfig" "libinput" "double-conversion"
  "libjpeg-turbo" "libxi" "libxkbcommon"
  "libxrender" "mesa" "shared-mime-info"
  "sqlite" "xcb-util-image" "xcb-util-keysyms"
  "xcb-util-renderutil" "xcb-util-wm" "harfbuzz"
)

PACKAGE() {
  tar xf "qt-everywhere-opensource-src-${VERSION}.tar.xz"
  cd "qt-everywhere-src-${VERSION}"

  export CFLAGS="${CFLAGS} -std=gnu89"
  export CXXFLAGS="${CFLAGS} -std=gnu89"
  export CPPFLAGS="${CFLAGS} -std=gnu89"

  patch -Np1 -i "../qt-everywhere-opensource-src-${VERSION}-kf5-1.patch"
  mkdir -pv qtbase/.git
  sed -e "/pragma once/a#include <cstdint>"                                    \
    -i qtlocation/src/3rdparty/mapbox-gl-native/include/mbgl/util/geometry.hpp \
       qtlocation/src/3rdparty/mapbox-gl-native/include/mbgl/util/string.hpp   \
       qtlocation/src/3rdparty/mapbox-gl-native/src/mbgl/gl/stencil_mode.hpp
  patch -Np1 -i "../qt-everywhere-opensource-src-${VERSION}-security_fix-1.patch"

  find . -type f -name document.h \
   -exec sed -i -e '/GenericStringRef& operator=(const GenericStringRef& rhs) { s = rhs.s; length = rhs.length; }/d' {} \;

  ./configure -prefix /usr                              \
            -sysconfdir /etc/xdg                        \
            -confirm-license                            \
            -opensource                                 \
            -dbus-linked                                \
            -openssl-linked                             \
            -system-harfbuzz                            \
            -system-sqlite                              \
            -nomake examples                            \
            -no-rpath                                   \
            -journald                                   \
            -skip qtwebengine                           \
            -archdatadir    /usr/lib/qt5                \
            -bindir         /usr/bin                    \
            -plugindir      /usr/lib/qt5/plugins        \
            -importdir      /usr/lib/qt5/imports        \
            -headerdir      /usr/include/qt5            \
            -datadir        /usr/share/qt5              \
            -docdir         /usr/share/doc/qt5          \
            -translationdir /usr/share/qt5/translations \
            -examplesdir    /usr/share/doc/qt5/examples

  make
  make INSTALL_ROOT="${ROOTDIR}" install

  find "${ROOTDIR}/usr" -name \*.prl \
   -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d' {} \;

  install -v -dm755 "${ROOTDIR}/usr/share/pixmaps/"
  install -v -Dm644 qttools/src/assistant/assistant/images/assistant-128.png \
                    "${ROOTDIR}/usr/share/pixmaps/assistant-qt5.png"
  install -v -Dm644 qttools/src/designer/src/designer/images/designer.png \
                    "${ROOTDIR}/usr/share/pixmaps/designer-qt5.png"
  install -v -Dm644 qttools/src/linguist/linguist/images/icons/linguist-128-32.png \
                    "${ROOTDIR}/usr/share/pixmaps/linguist-qt5.png"
  install -v -Dm644 qttools/src/qdbus/qdbusviewer/images/qdbusviewer-128.png \
                    "${ROOTDIR}/usr/share/pixmaps/qdbusviewer-qt5.png"

  install -Ddm755 "${ROOTDIR}/usr/share/applications"
  cp "${ROOTDIR}/assistant-qt5.desktop"   "${ROOTDIR}/usr/share/applications/"
  cp "${ROOTDIR}/designer-qt5.desktop"    "${ROOTDIR}/usr/share/applications/"
  cp "${ROOTDIR}/linguist-qt5.desktop"    "${ROOTDIR}/usr/share/applications/"
  cp "${ROOTDIR}/qdbusviewer-qt5.desktop" "${ROOTDIR}/usr/share/applications/"

  for file in moc uic rcc qmake lconvert lrelease lupdate; do
    ln -sfrvn "${ROOTDIR}/usr/bin/$file" "${ROOTDIR}/usr/bin/${file}-qt5"
  done

  cd .. && rm -r "qt-everywhere-src-${VERSION}"
}
