# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Peter Budai <peterbudai@hotmail.com>

_realname=hunspell
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
# Please rebuild enchant when updating this if needed, otherwise it may
# silently break spell checking in applications
pkgver=1.7.0
pkgrel=5
pkgdesc="Spell checker and morphological analyzer library and program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://hunspell.github.io/"
# We omit MPL because it cannot be applied to the whole source, see COPYING
license=(GPL2+ LGPL2.1+)
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" 
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-gettext
         ${MINGW_PACKAGE_PREFIX}-ncurses
         ${MINGW_PACKAGE_PREFIX}-readline)
options=('!libtool' 'strip' '!debug')
optdepends=("perl: for ispellaff2myspell")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/hunspell/hunspell/archive/v${pkgver}.tar.gz"
        01-relocate.patch
        02-fix-link-with-pdcurses.patch)
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('bb27b86eb910a8285407cf3ca33b62643a02798cf2eef468c0a74f6c3ee6bc8a'
            'abb8bff7367a80bd3cc4237c118ba2beb3d9ecf446a40a1c5d9990166bfb92fc'
            '260432a8f01525e83124c79e375cc9f0692b1a6c97773add4c69b8efe081cf1d')

prepare() {
  cd ${srcdir}
  tar zxf ${_realname}-${pkgver}.tar.gz || true
  cd "${srcdir}/${_realname}-${pkgver}"
  cp -Rp README.md README
  #remove files created by patch
  rm -rf src/tools/pathtools.cxx src/tools/pathtools.hxx
  patch -Np1 -i ${srcdir}/01-relocate.patch
  #patch -Np1 -i ${srcdir}/02-fix-link-with-pdcurses.patch
  autoreconf -fi
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  #CPPFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  #CFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"
  #CXXFLAGS+=" -I${MINGW_PREFIX}/include/ncurses"

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-rpath \
    --with-ui \
    --with-readline

  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=" \
  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make check
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=" \
  make DESTDIR="${pkgdir}" install
  cp ${pkgdir}${MINGW_PREFIX}/lib/lib${_realname}-${pkgver%.*}.dll.a ${pkgdir}${MINGW_PREFIX}/lib/lib${_realname}.dll.a

  # Licenses
  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 COPYING      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 COPYING.LESSER "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
  install -Dm644 COPYING.MPL  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MPL"

}
