# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=abiword
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.0.8
pkgrel=1
pkgdesc="A word processor. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.gnome.org/World/AbiWord'
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-enchant"
         "${MINGW_PACKAGE_PREFIX}-fribidi"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-libgda"
         "${MINGW_PACKAGE_PREFIX}-libgsf"
         "${MINGW_PACKAGE_PREFIX}-libical"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-librevenge"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         "${MINGW_PACKAGE_PREFIX}-libwpg"
         "${MINGW_PACKAGE_PREFIX}-libwps"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-redland"
         "${MINGW_PACKAGE_PREFIX}-wv"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-asio"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "autoconf-archive")
source=("https://gitlab.gnome.org/World/AbiWord/-/archive/release-${pkgver}/AbiWord-release-${pkgver}.tar.bz2"
        001-support-python3.patch::https://gitlab.gnome.org/World/AbiWord/-/commit/51787d61.patch
        002-support-enchant2.patch::https://gitlab.gnome.org/World/AbiWord/-/commit/ae05e92d.patch
        003-do-not-redefine-snprintf.patch
        004-fix-build-rc-file.patch
        005-do-not-redefine-winnt-version.patch
        006-fix-build-with-clang.patch
        007-fix-segfault.patch
        https://gitlab.gnome.org/World/AbiWord/-/commit/119c7a86.patch)
sha256sums=('ef2fdc2cb66a54a58185e7008bd349ce858e59648e18571913d5a3e2b43abf37'
            '195c7dc042bb60794aced6040838b05cf1d42d4faf9443cf13fdd9d58d681f93'
            'b1681d56056b216ae250d442bd6f3eadd161b7f5b7705ec53c0d8221449bd457'
            '5a9034f934343774493b29a1a5f2c1a529d747e13d80ad2c318217a8710a3ac2'
            '1b31eeb328283bbabdd827c82ed06d99d1374661871ce2d50e52f592f1ccf359'
            'a2d1fb084490ff0bf3da0ce2a3c3b54d7410186737237d5b8e0d9631e18ce619'
            '9226b43c2dd6196cec123d50fe80e3bef542e8211220c0dcf3db29ee3b56c42c'
            'bd0d9a9b75c470745a8c507bf6490cd884940bbf4981fa83daf1fcc442c41fe2'
            'b4ae363640d93345c0a12d7d7522826a049a43032164d91dd6facd7a4bec4206')

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

prepare() {
  cd "AbiWord-release-${pkgver}"

  apply_patch_with_msg \
    001-support-python3.patch \
    002-support-enchant2.patch \
    003-do-not-redefine-snprintf.patch \
    004-fix-build-rc-file.patch \
    005-do-not-redefine-winnt-version.patch \
    007-fix-segfault.patch \
    119c7a86.patch

  if [[ ${CC} == clang ]]; then
    apply_patch_with_msg 006-fix-build-with-clang.patch
  fi

  ./autogen-common.sh
  autoreconf -fiv
}

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

  export lt_cv_deplibs_check_method='pass_all'

  CXXFLAGS+=" -w -DWIN32" \
  LDFLAGS+=" -lpthread" \
  ../AbiWord-release-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --enable-shared \
    --enable-plugins \
    --enable-clipart \
    --enable-templates

  make
}

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

  make install DESTDIR="${pkgdir}"

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