# Maintainer: Angel Velasquez <angvp@archlinux.org> 
# Maintainer: AndyRTR <andyrtr@archlinux.org>

pkgbase=hunspell-es
pkgname=(hunspell-es_any hyphen-es mythes-es)
pkgver=2.6
pkgrel=1
epoch=1
arch=(any)
url="https://github.com/sbosio/rla-es"
license=(LGPL3)
makedepends=(qt5-webengine)
_countries=(
  'AR "Argentina"'
  'BO "Bolivia"'
  'CL "Chile"'
  'CO "Colombia"'
  'CR "Costa Rica"'
  'CU "Cuba"'
  'DO "Dominican Republic"'
  'EC "Ecuador"'
  'ES "Spain"'
  'GT "Guatemala"'
  'HN "Honduras"'
  'MX "Mexico"'
  'NI "Nicaragua"'
  'PA "Panama"'
  'PE "Peru"'
  'PR "Puerto Rico"'
  'PY "Paraguay"'
  'SV "El Salvador"'
  'UY "Uruguay"'
  'VE "Venezuela"'
)

source=(https://github.com/sbosio/rla-es/releases/download/v$pkgver/es.oxt)
noextract=(es.oxt)

for _co in "${_countries[@]}"; do
  _code=${_co%% *}
  noextract+=(es_$_code.oxt)
  source+=("https://github.com/sbosio/rla-es/releases/download/v$pkgver/es_$_code.oxt")
  _pkgname=hunspell-es_${_code,,}
  pkgname+=($_pkgname)
  eval "package_$_pkgname() {
    _package $_co
  }"
done

sha256sums=('10fa51dffb78f0fc15fd791770813f551da43cf007b52cb8fb6ccb0b4117e85f'
            '1075b8471797fb2b420a70f12e2da55035e121cad3a307d033a8a9c4575932af'
            '7fa4a0a261f97bd8c36216a1be32b4f7d77cb1018ae40deffa22b9e87d525713'
            '1c35ea3f3ebb344903e0c4bde3513b68d7ac153a4ebeef0f101d5c87a09f9202'
            '260e25e8799af8eea483b7c5c0794580583fd0d9fc90158d3dd479476b2cccef'
            '445b2bf4e4ef423967d99ded28188be040bb7a473742c213d27f91cbc5f09d98'
            'b6c26f66e0a1c62c5d6c0ab5c056313d765da2af952cd58aa9db7c966759384a'
            '018851b1dd6b2431fdb3bbf1e4c1f9bcf976d8bb400397b6ece9b7bd91a142df'
            '2469b2d20051aa4270e67c9de465c2cb9846bb39370844c9130c145c1df33e93'
            'b6d372b3e0fc51315e28c8bf46e9121ef935fb4a5456902f662ba92ec247837e'
            'c5292c0b22be9002dd3606763dda8ed6a39cd5f497ffa344d0c04e7d62ff5260'
            '96af0fe67b9ee70d6c0a30295554d8390e0b256924a578e0b9c79215b8400db3'
            '2ebefeb14870d219b924d3a253c54bc401dac07d316f73aa37b2202344d1b7ae'
            '0d15ca525ee21cf0eef107743e4d5ec8132a3d173073d39b04a240c5e0d1ccad'
            '019637b48b57313e2dd7b5c618aad3a071d79bf1f49d7a99ac5f5f5f5a142414'
            'f171141aad4f85897ae955c8b770a391b4b0b5b0530bf7233a5f09be4d6bda58'
            '7613c31667ba5b0a7e9fb9d30902beaf33d4a38da4254164b8b38cbfcffe1069'
            'ef38c998ebbe41801106bc2846bb2751933ca203e17121e4bf037f15bd18a81e'
            '7369dd2aeb8a9f9d5d185fd8a7b46f02277ba58a8bfa993cd13729f2ddfb4c88'
            'f01ef40500aa5416faa9ee6b6b198f5ddc10b30ef35c305173cedd9561634230'
            '6fdf74b7ac069541354321c82ba7e780aeaff15b2f5b2b8ae118d439d275ffa8')

_package() {
  pkgdesc="Spanish ($2) hunspell dictionary"
  depends=(hunspell)
  conflicts=(hunspell-es)

  LANG=en_US.utf8 bsdtar -xf es_$1.oxt

  install -dm755 "$pkgdir"/usr/share/hunspell
  install -Dm644 es_$1.dic es_$1.aff "$pkgdir/usr/share/hunspell"

  install -d "$pkgdir"/usr/share/myspell/dicts
  pushd "$pkgdir/usr/share/myspell/dicts"
    for _file in "$pkgdir"/usr/share/hunspell/*; do
      ln -sv /usr/share/hunspell/$(basename $_file) .
    done
  popd

# Install webengine dictionaries
  install -d "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/
  for _file in "$pkgdir"/usr/share/hunspell/*.dic; do
    _filename=$(basename $_file)
    qwebengine_convert_dict $_file "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/${_filename/\.dic/\.bdic}
  done

  install -Dm644 README.txt -t "$pkgdir/usr/share/doc/$pkgname"
}

package_hunspell-es_any() {
  pkgdesc="Spanish (generic) hunspell dictionary"
  depends=(hunspell)
  conflicts=(hunspell-es)
  replaces=(hunspell-es)

  LANG=en_US.utf8 bsdtar -xf es.oxt

  install -Dm644 es.dic es.aff -t "$pkgdir"/usr/share/hunspell

  install -d "${pkgdir}/usr/share/myspell/dicts"
  pushd "$pkgdir/usr/share/myspell/dicts"
    for file in "$pkgdir"/usr/share/hunspell/*; do
      ln -sv /usr/share/hunspell/$(basename $file) .
    done
  popd
  
# Install webengine dictionaries
  install -d "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/
  for _file in "$pkgdir"/usr/share/hunspell/*.dic; do
  _filename=$(basename $_file)
    qwebengine_convert_dict $_file "$pkgdir"/usr/share/qt/qtwebengine_dictionaries/${_filename/\.dic/\.bdic}
  done

  install -Dm644 README.txt -t "$pkgdir/usr/share/doc/$pkgname"
}

package_hyphen-es() {
  pkgdesc="Spanish hyphenation rules"
  depends=(hyphen)

  install -Dm644 hyph_es.dic -t "$pkgdir"/usr/share/hyphen/

  pushd "$pkgdir"/usr/share/hyphen/
  for _co in "${_countries[@]}"; do
    ln -s hyph_es.dic hyph_es_${_co%% *}.dic
  done
  popd

  install -d "$pkgdir"/usr/share/myspell/dicts
  pushd "$pkgdir"/usr/share/myspell/dicts
    for file in "$pkgdir"/usr/share/hyphen/*; do
      ln -sv /usr/share/hyphen/$(basename $file) .
    done
  popd
  
  install -Dm644 README_hyph_es.txt -t "$pkgdir"/usr/share/doc/$pkgname/
}

package_mythes-es() {
  pkgdesc="Spanish thesaurus"
  depends=(libmythes)

  LANG=en_US.utf8 bsdtar -xf es.oxt
  install -Dm644 th_es_v2.* -t "$pkgdir"/usr/share/mythes

  pushd "$pkgdir"/usr/share/mythes/
  for _co in "${_countries[@]}"; do
    ln -s th_es_v2.idx "th_es_"${_co%% *}"_v2.idx" || true
    ln -s th_es_v2.dat "th_es_"${_co%% *}"_v2.dat" || true
  done
  popd

  install -d "$pkgdir"/usr/share/myspell/dicts
  pushd "$pkgdir"/usr/share/myspell/dicts
  for file in "$pkgdir"/usr/share/mythes/*; do
    ln -sv /usr/share/mythes/$(basename $file) .
  done
  popd
  
  install -Dm644 README_th_es.txt -t "$pkgdir"/usr/share/doc/$pkgname/
}
