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

# The patches for this package are developed inside the -git package.
# This is for three reasons:
# 1. Upstreaming is easier.
# 2. The Qt devs often forget to release Qbs at the same time as Qt Creator.
# 3. Commit messages are retained (though efforts are made for that via git init)

# TODO :: Patch this for clang support:
#         qbs setup-toolchains --type clang /mingw64/bin/clang.exe clang
#         .. will add a profile to %AppData%/QtProject/qbs.ini that contains:
#         qt-project\qbs\profiles\clang\qbs\toolchain=clang, llvm, gcc
#         .. but is missing:
#         qt-project\qbs\profiles\clang\qbs\targetOS=windows
#         then in share/qbs/modules/cpp/windows-mingw.qbs we need to change
#         condition: qbs.targetOS.contains("windows") && qbs.toolchain.contains("mingw")
#         .. to:
#         condition: qbs.targetOS.contains("windows") \
#               && (qbs.toolchain.contains("mingw") || qbs.toolchain.contains("gcc"))

_realname=qbs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.18.0
pkgrel=2
_pkgfqn="${_realname}-src-${pkgver}"
pkgdesc="Qt Build Suite (mingw-w64)"
url='https://www.qt.io/'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('LGPL')
depends=("${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-clang" "git") # clang for qdoc
options=('docs' 'staticlibs') # '!strip' 'debug')
source=("https://download.qt.io/official_releases/qbs/${pkgver}/${_pkgfqn}.tar.gz"
        #${_pkgfqn}::git://code.qt.io/qbs/qbs.git#tag="v${pkgver}"
        "manifest"
        "0003-add-generator-header-installs.1.4.patch"
        "0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch"
        "0005-setup-toolchains-Pretend-msys-is-mingw.1.4.patch"
        "0006-Add-some-missing-files.1.4.patch"
        "0008-add-many-files-to-installed-headers.1.4.patch")
sha256sums=('3d0211e021bea3e56c4d5a65c789d11543cc0b6e88f1bfe23c2f8ebf0f89f8d4'
            '838098b25a8044176b3139b4003594570c62a8d64f5470fbbd769f3bf44e0855'
            '540ff56b72e56d6ede1420e66f94e1ea0348ce172bf71f153b7381632a19a305'
            'b6d51c58e96ac4fd957ccedcff7812de6f84ccfc9f996e30cf2e75fb5b4e5f02'
            '320c57d7989038203998957962840e9a66698e7419238017c8c73c6e0dfd3da9'
            '6c8285f0e274ccede48d7e2f9bead02d1ef34225753a627974b32dce68b6d28d'
            '95306d788a375935bcd5227eefbd73850b9c2554edd1b1757090f6a59c608e88')

prepare() {
  cd ${_pkgfqn}
  #[[ -d src/plugins/generator ]] && rm -rf src/plugins/generator
  #rm -rf src/lib/corelib/api/generator.*
  rm -rf src/lib/corelib/tools/generat*

  patch -p1 -i "${srcdir}"/0003-add-generator-header-installs.1.4.patch
  patch -p1 -i "${srcdir}"/0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch
  patch -p1 -i "${srcdir}"/0005-setup-toolchains-Pretend-msys-is-mingw.1.4.patch
  patch -p1 -i "${srcdir}"/0006-Add-some-missing-files.1.4.patch
  patch -p1 -i "${srcdir}"/0008-add-many-files-to-installed-headers.1.4.patch
}

build() {
  local _config
  if check_option "debug" "n"; then
    _config="release"
  else
    _config="debug"
  fi
  cd ${srcdir}
  
  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH}
  cd build-${CARCH}
  
  export MSYS2_ARG_CONV_EXCL="QBS_INSTALL_PREFIX="
  ${MINGW_PREFIX}/bin/qmake.exe \
    QBS_INSTALL_PREFIX="${MINGW_PREFIX}" \
    CONFIG+=${_config} \
    CONFIG+=qbs_enable_project_file_updates \
    ../${_pkgfqn}/qbs.pro
  
  # Because xcodeprojectgenerator has a build order problem.
  make
  make docs
}

package() {
  cd ${srcdir}/build-${CARCH}
  
  export MSYS2_ARG_CONV_EXCL="QBS_INSTALL_PREFIX="
  make INSTALL_ROOT=${pkgdir} install
  make INSTALL_ROOT=${pkgdir} install_docs

  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.dll

  for ff in qbs-setup-toolchains qbs-setup-qt; do
    cp ${srcdir}/manifest ${pkgdir}${MINGW_PREFIX}/bin/${ff}.exe.manifest
  done
  cp ${srcdir}/${_pkgfqn}/VERSION ${pkgdir}${MINGW_PREFIX}/include/qbs
}
