# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=qownnotes
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=25.9.6
pkgrel=1
pkgdesc="Plain-text file markdown note taking with Nextcloud/ownCloud integration"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.qownnotes.org/'
msys2_repository_url='https://github.com/pbek/QOwnNotes/'
msys2_references=(
  'aur: qownnotes'
)
license=('spdx:GPL-2.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-qt6-base"
  "${MINGW_PACKAGE_PREFIX}-qt6-svg"
  "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-qt6-websockets"
  "${MINGW_PACKAGE_PREFIX}-aspell"
  "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
  "${MINGW_PACKAGE_PREFIX}-libgit2"
  "${MINGW_PACKAGE_PREFIX}-libbotan"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-qt6-tools"
)
source=("https://github.com/pbek/QOwnNotes/releases/download/v${pkgver}/qownnotes-${pkgver}.tar.xz"
        "0001-allow-system-botan.patch")
sha256sums=('2b71646f9a3bd0cc7463a84c36d3768dee4af51c9a49f746740f463253e1e7c5'
            'e332f9ad90713434224f3b1c4b344a9539dfe83acd62cd033490f9d4acf58633')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-allow-system-botan.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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DQON_QT6_BUILD=ON \
      -DBUILD_WITH_SYSTEM_BOTAN=ON \
      -DBUILD_WITH_LIBGIT2=ON \
      -DBUILD_WITH_ASPELL=ON \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
