# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=hyphen
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-en")
pkgver=2.8.8
pkgrel=1
pkgdesc="A text hyphenation library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://hunspell.sourceforge.io/'
license=(GPL-2.0+ or LGPL-2.0+ or MPL-1.1+)
makedepends=('perl')
optdepends=('perl: for substrings.pl script')
source=("https://downloads.sourceforge.net/hunspell/${_realname}-${pkgver}.tar.gz"
        "0001-Fix_COMPOUNDHYPHENMIN_1.patch"
        "0002-Tests_COMPOUNDHYPHENMIN_1.patch")
sha256sums=('304636d4eccd81a14b6914d07b84c79ebb815288c76fe027b9ebff6ff24d5705'
            'ea0095c8e9e713fcc99c66582e8ccff478f097c710ba77a1c7e59b7851f686e9'
            '497533bf2226c4137e1cedbcfc8dcb560f5a6190752fb72f838176d75269be4a')

prepare() {
  cd "$srcdir"/${_realname}-${pkgver}
  #remove files created by patch
  rm "tests/compound5.hyph" \
    tests/compound5.pat \
    tests/compound5.test \
    tests/compound5.word \
    tests/compound6.hyph \
    tests/compound6.pat \
    tests/compound6.test \
    tests/compound6.word || true
# Patches 0001 and 0002 are taken from the hyphen github repository
# so they will probably in the next version.
  patch -Np1 -i "${srcdir}/0001-Fix_COMPOUNDHYPHENMIN_1.patch"
  patch -Np1 -i "${srcdir}/0002-Tests_COMPOUNDHYPHENMIN_1.patch"
  autoreconf -fiv
}

build() {
  cd "$srcdir"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  #Note you have to copy the files for the tests and install to work.
  cp -a "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}" && \
    cd "${srcdir}"/build-${CARCH}

   ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared

  make
}

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

package_hyphen() {
  pkgdesc="library for high quality hyphenation and justification (mingw-w64)"
  optdepends=("perl: for substrings.pl")

  cd "${srcdir}"/build-${CARCH}
  make DESTDIR="${pkgdir}/" install-libLTLIBRARIES
  make DESTDIR="${pkgdir}/" install-binSCRIPTS
  make DESTDIR="${pkgdir}/" install-includeHEADERS

  # license
  install -D -m644 COPYING "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE

}

package_hyphen-en() {
pkgdesc="English hyphenation rules"
optdepends=("${MINGW_PACKAGE_PREFIX}-hyphen: offers hyphenation library functions")

  cd "${srcdir}"/build-${CARCH}
  make DESTDIR="${pkgdir}/" install-hyphDATA

#The rest of this is done to be consistant with ArchLinux's
#hyphen package.
  pushd ${pkgdir}${MINGW_PREFIX}/share/hyphen/
   en_US_aliases="en_AG en_AU en_BS en_BW en_BZ en_CA en_DK en_GB en_GH en_HK en_IE en_IN en_JM en_NA en_NZ en_PH en_SG en_TT en_ZA en_ZW"
   for lang in $en_US_aliases; do
        ln -s hyph_en_US.dic hyph_$lang.dic
   done
  popd
  
  # the symlinks
  install -dm755 ${pkgdir}${MINGW_PREFIX}/share/myspell/dicts
  pushd ${pkgdir}${MINGW_PREFIX}/share/myspell/dicts
    for file in ${pkgdir}${MINGW_PREFIX}/share/hyphen/*; do
      ln -sv ${pkgdir}${MINGW_PREFIX}/share/hyphen/$(basename ${file}) .
    done
  popd

}

# 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;
