# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=texstudio
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.8.9
pkgrel=1
pkgdesc="Integrated writing environment for creating LaTeX documents (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://texstudio.sourceforge.net/"
msys2_repository_url='https://github.com/texstudio-org/texstudio'
msys2_changelog_url='https://texstudio-org.github.io/CHANGELOG.html'
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-hunspell"
         "${MINGW_PACKAGE_PREFIX}-poppler-qt6"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
         "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-quazip-qt6"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
optdepends=("${MINGW_PACKAGE_PREFIX}-evince: pdf reader")
source=("${msys2_repository_url}/archive/${pkgver}/${_realname}-$pkgver.tar.gz"
        '0001-install-extra-files-on-mingw.patch'
        '0002-texstudio-4.2.2-fallbackpaths-win32-as-unix.patch')
sha256sums=('4d7e04fe076ae01da5773b5ae6e9386345a1cc2bf95dee8648e7bb2faa4adeea'
            'b89f798f910e54dc6618edc09ab2834537897b5b18e0590fcb75c3530d4796fb'
            'a9f1dfadf40aa434b687e6a5738f7a61a1dabe258abc9b8f34f181987b25cf81')

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

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

    apply_patch_with_msg \
      0001-install-extra-files-on-mingw.patch \
      0002-texstudio-4.2.2-fallbackpaths-win32-as-unix.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

    if [[ ${CARCH} == aarch64 ]]; then
      _extra_config+=("-DTEXSTUDIO_ENABLE_CRASH_HANDLER=OFF")
    fi

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DTEXSTUDIO_ENABLE_MEDIAPLAYER=ON \
        "${_extra_config[@]}" \
        ../${_realname}-${pkgver}

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

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

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

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