# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=installer-framework
pkgbase=mingw-w64-qt-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-qt-${_realname}"
pkgdesc="The Qt Installer Framework used for the Qt SDK installer (mingw-w64)"
pkgver=4.10.0
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://wiki.qt.io/Qt-Installer-Framework'
msys2_repository_url="https://code.qt.io/cgit/installer-framework/installer-framework.git/"
license=('spdx:GPL-3.0-or-later WITH Qt-GPL-exception-1.0 AND GFDL-1.3-or-later')
conflicts=("${MINGW_PACKAGE_PREFIX}-qt-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-qt-${_realname}-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qt6-static"
             "${MINGW_PACKAGE_PREFIX}-bzip2"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "${MINGW_PACKAGE_PREFIX}-zlib"
             "${MINGW_PACKAGE_PREFIX}-brotli"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-graphite2"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             "${MINGW_PACKAGE_PREFIX}-harfbuzz"
             "${MINGW_PACKAGE_PREFIX}-libb2"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-pcre2"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-libmng"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-libwebp")
source=(https://download.qt.io/official_releases/qt-${_realname}/${pkgver}/${_realname}-everywhere-src-${pkgver}.tar.xz
        "0002-add-installation-stuff-to-pro-files.patch"
        "0003-add-recursive-option-to-rmdir-operation.patch"
        "0004-add-dirExists-fuction.patch"
        "0005-fix-build-with-qt6.patch"
        "0006-Don-t-remove-files-that-live-outside-of-path.patch"
        "0010-fix-version-pass-to-windres.patch"
        "0011-fix-redefined-mode_t.patch"
        "0012-get_timezone-not-available-on-msvcrt.patch"
        "0013-fix-libarchive-linking.patch"
        "https://github.com/qtproject/installer-framework/commit/c3e9ef46a705d5561ae80f64e632036a2a4189ff.patch")
sha256sums=('c800433f230bbf01a5653644c103a0d4906a0eaa3f8d797f76f7b32025c8ac9b'
            '5f9f2a2ff87698dd72f9b9a42765e6b02034e3bfec8e8e348d55285048097a9b'
            '3de4920ee4f04cce6bc791b01e2ee9894958b26c47dcac7a4e2843eb429154b5'
            '2aaf51f346edb6db2367262d0d8e1bb77d0afee51fabd5b2766038a97ebf0ca6'
            '88ca7c3c31ce1dc32036658e1307e93ce933e1088205056def6b178eb52953b6'
            '00857e9a654ecaed93ec79898389329799fdc01968b7562ae1889498b12c943d'
            'b1c741190c172bc0f49ec17292132c7257e382ea29438c7177ec46e0eeaac5ca'
            'a2eaac4c11e0b6a41f3289657d9e95894a5ed6d625ea98cc41fd4356d2d22a8e'
            '51d50531fa3cc4f68473d51812f5feea128690dc8bc7de379bf9888dae9b9d46'
            'f064ed89be41deea9aa25c7d145ca566f9dfe03fadcba3850ac61c8542b09255'
            'fc10c9af91bb475bfd58e141feef0d0cfd516c8adc240248ee9ad4bc47bdf579')

# 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() {
  cd "${srcdir}"/${_realname}-everywhere-src-${pkgver%.*}

  apply_patch_with_msg \
    0002-add-installation-stuff-to-pro-files.patch \
    0003-add-recursive-option-to-rmdir-operation.patch \
    0004-add-dirExists-fuction.patch \
    0005-fix-build-with-qt6.patch \
    0010-fix-version-pass-to-windres.patch \
    0011-fix-redefined-mode_t.patch \
    0012-get_timezone-not-available-on-msvcrt.patch \
    0013-fix-libarchive-linking.patch \
    c3e9ef46a705d5561ae80f64e632036a2a4189ff.patch

  # This requires ../mingw-w64-qt5-static/0044-qt-5.4.1-Revert-Revert-fix-NTFS-mount-points.patch
  # to fix the problem for junction points. Together they fix:
  # https://github.com/msys2/msys2.github.io/issues/4
  apply_patch_with_msg \
    0006-Don-t-remove-files-that-live-outside-of-path.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  local _config
  if check_option "debug" "n"; then
    _config="release"
  else
    _config="debug"
  fi

  ${MINGW_PREFIX}/qt6-static/bin/qmake.exe \
    CONFIG+="${_config}" \
    CONFIG+=static \
    CONFIG+=libarchive \
    CONFIG+=no_testcase_installs \
    IFW_ZLIB_LIBRARY=${MINGW_PREFIX}/lib/libz.a \
    IFW_BZIP2_LIBRARY=${MINGW_PREFIX}/lib/libbz2.a \
    IFW_LZMA_LIBRARY=${MINGW_PREFIX}/lib/liblzma.a \
    ../${_realname}-everywhere-src-${pkgver%.*}/installerfw.pro

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make INSTALL_ROOT="${pkgdir}${MINGW_PREFIX}" install
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_realname}-everywhere-src-${pkgver%.*}/LICENSE* "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
}
