# 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.8.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")
sha256sums=('fc9a110ead81bc7e9bb70917383e181ce8a9b2d2cd36e41538848da50a679f11'
            '4100a93ac7e117a32430d22187a4ebe94e7830d94d2c23b85651bc1d95821409'
            '3de4920ee4f04cce6bc791b01e2ee9894958b26c47dcac7a4e2843eb429154b5'
            '2aaf51f346edb6db2367262d0d8e1bb77d0afee51fabd5b2766038a97ebf0ca6'
            'd4d52149e6cbc595222589c92b3a2f1ba48a36d19a27952dfb5dc063d7b417b3'
            '00857e9a654ecaed93ec79898389329799fdc01968b7562ae1889498b12c943d'
            '2129715e22dac176a669b7c3776b4d7f0bc66a3c01ad262b89dd23ca3825a324'
            'a2eaac4c11e0b6a41f3289657d9e95894a5ed6d625ea98cc41fd4356d2d22a8e'
            '51d50531fa3cc4f68473d51812f5feea128690dc8bc7de379bf9888dae9b9d46')

# 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

  # 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}
}
