# Maintainer: AlexWMF <alexxwmf@gmail.com>

_realname=qca
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6")
pkgver=2.3.10
pkgrel=1
pkgdesc="Qt Cryptographic Architecture (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://userbase.kde.org/QCA"
msys2_repository_url="https://invent.kde.org/libraries/qca"
msys2_references=(
  'archlinux: qca'
)
license=('spdx:LGPL-2.1')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-5compat")
depends=("${MINGW_PACKAGE_PREFIX}-ca-certificates"
         "${MINGW_PACKAGE_PREFIX}-cyrus-sasl"
         "${MINGW_PACKAGE_PREFIX}-gnupg"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-openssl")
source=("https://download.kde.org/stable/${_realname}/${pkgver}/${_realname}-${pkgver}.tar.xz"{,.sig}
        "cyrus_sasl_linking.patch"
        "qt5-custom-dirs.patch"
        "qca-mingw-pkg-config.patch"
        "qca-mingw-custom-certificates.patch"
        "qca-mingw-link-to-release.patch")
sha256sums=('1c5b722da93d559365719226bb121c726ec3c0dc4c67dea34f1e50e4e0d14a02'
            'SKIP'
            'eadfa58299b2dd22ad7ea4b6841466effb191e24ed153485c0f4c82ff4ce00f7'
            'faada79c026ccb50b122b8a45d5c0f138ac2dbfd1cf036d7a9e070f524eeae89'
            '9107a43a7fa620d32dc78c2a7a4f58e17896fe1fc6dd695e6e49a7c65351bbfd'
            '879b6e306a1a4a1887a824cde5fecef3dc43170b9f7578d968fe705d6da9f613'
            '037b615f8b6f609112383b69e6050e7c939b12b0c7c0fa210a58e075c0d1c589')
validpgpkeys=('CB9387521E1EE0127DA804843FDBB55084CC5D84') # Harald Sitter <sitter@kde.org>

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -p1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    cyrus_sasl_linking.patch \
    qt5-custom-dirs.patch \
    qca-mingw-pkg-config.patch \
    qca-mingw-custom-certificates.patch \
    qca-mingw-link-to-release.patch
}

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

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

  #QC_CERTSTORE_PATH=${MINGW_PREFIX}/ssl/certs/ca-bundle.crt
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;DQC_CERTSTORE_PATH=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DWITH_botan_PLUGIN=OFF \
    -DBUILD_TESTS=OFF \
    -DQCA_SYSTEMSTORE_PATH=../etc/ssl/certs/ca-bundle.crt \
    -DQCA_INSTALL_IN_QT_PREFIX=ON \
    -DBUILD_WITH_QT6=OFF \
    ../${_realname}-${pkgver}

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

  if [[ ${CARCH} != i686 ]]; then

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;DQC_CERTSTORE_PATH=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DWITH_botan_PLUGIN=OFF \
    -DBUILD_TESTS=OFF \
    -DQCA_SYSTEMSTORE_PATH=../etc/ssl/certs/ca-bundle.crt \
    -DQCA_INSTALL_IN_QT_PREFIX=ON \
    -DBUILD_WITH_QT6=ON \
    ../${_realname}-${pkgver}

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

  fi
}

package_qca-qt5() {
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5-git")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5-git")
  depends+=("${MINGW_PACKAGE_PREFIX}-qt5-base")
  install=${_realname}-qt5-${MSYSTEM}.install
  cd "${srcdir}"/build-${MSYSTEM}-qt5
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt5/COPYING"
}

package_qca-qt6() {
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5-git")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5-git")
  depends+=("${MINGW_PACKAGE_PREFIX}-qt6-base"
            "${MINGW_PACKAGE_PREFIX}-qt6-5compat")
  install=${_realname}-qt6-${MSYSTEM}.install
  cd "${srcdir}"/build-${MSYSTEM}-qt6
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt6/COPYING"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
