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

_realname=qbs
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.5.0
pkgrel=2
pkgdesc="Qt Build Suite (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://qbs.io'
license=('spdx:LGPL-3.0-only WITH Qt-GPL-exception-1.0 AND LGPL-2.1-or-later AND GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang-libs" # clang-libs for qdoc
             "${MINGW_PACKAGE_PREFIX}-python-beautifulsoup4"
             "${MINGW_PACKAGE_PREFIX}-python-lxml"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
_pkgfqn="${_realname}-src-${pkgver}"
source=("https://download.qt.io/official_releases/qbs/${pkgver}/${_pkgfqn}.tar.gz"
        "manifest"
        "0002-remove-lib-prefix-from-plugin-names.patch"
        "0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch")
sha256sums=('1801afd4f1fafc3015bd93a60da3896bf211ab4a328cddefde3cd16bafd5a6eb'
            '838098b25a8044176b3139b4003594570c62a8d64f5470fbbd769f3bf44e0855'
            'e62bfe3d1958ec0a83010d9fb27def32024839de9dbcd7088c21f08e1460c7e1'
            '6f8cc5892f0ba94fceb31484d8b656d85afb5f1cf6f7b54e679de9a4f7286bc6')

prepare() {
  cd ${_pkgfqn}

  patch -p1 -i "${srcdir}"/0002-remove-lib-prefix-from-plugin-names.patch
  patch -p1 -i "${srcdir}"/0004-Use-.dll.a-instead-of-.a-for-mingw-import-librar.1.4.patch
}

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wno-attributes" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DWITH_TESTS=OFF \
    -DQBS_LIB_INSTALL_DIR=lib \
    -DQBS_DLL_INSTALL_DIR=bin \
    -DQBS_INSTALL_HTML_DOCS=ON \
    -DQBS_INSTALL_QCH_DOCS=ON \
    -DQBS_INSTALL_MAN_PAGE=ON \
    ../${_pkgfqn}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -d "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_pkgfqn}/LICENSE.* \
    -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
  install -Dm644 "${srcdir}"/${_pkgfqn}/LGPL_EXCEPTION.txt \
    -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}

  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
}
