# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Lex Black <autumn-wind at web dot de>
# Contributor: TingPing <tingping@tingping.se>
# Contributor: Guillaume Horel <guillaume.horel@gmail.com>

pkgbase=brotli
pkgname=('brotli' 'python-brotli' 'brotli-testdata')
_gitcommit=ed738e842d2fbdf2d6459e39267a633c4a9b2f5d
pkgver=1.1.0
pkgrel=3
pkgdesc='Generic-purpose lossless compression algorithm'
url='https://github.com/google/brotli'
arch=(x86_64 powerpc64le powerpc64 powerpc espresso riscv64)
license=('MIT')
makedepends=(
  git
  cmake
  python-setuptools
  python-build
  python-installer
  python-wheel
)
source=(${pkgname}::"git+${url}#commit=${_gitcommit}")
sha512sums=('SKIP')

pkgver() {
  cd ${pkgbase}
  git describe --tags --match 'v*' | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

build() {
  cd ${pkgbase}
  python -m build --wheel --no-isolation
  cmake -S . -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DBUILD_SHARED_LIBS=True \
    -DCMAKE_C_FLAGS="$CFLAGS -ffat-lto-objects"
  cmake --build build -v
}

check() {
  cd ${pkgbase}
  local python_version=$(python -c 'import sys; print("".join(map(str, sys.version_info[:2])))')

  case "${CARCH}" in
    powerpc)     _arch=ppc   ;;
    powerpc64)   _arch=ppc64 ;;
    powerpc64le) _arch=ppc64le ;;
    *)           _arch=${CARCH} ;;
  esac

  PYTHONPATH="$PWD/bin/lib.linux-${_arch}-cpython-${python_version}" python -m unittest discover python "*_test.py"
  cd build
  ctest --output-on-failure --stop-on-failure -j$(nproc)
}

package_brotli() {
  depends=('glibc')
  provides=(libbrotlicommon.so libbrotlidec.so libbrotlienc.so)

  cd ${pkgbase}
  DESTDIR="$pkgdir" cmake --install build
  local man
  for man in docs/*.?; do
    install -Dm 644 "$man" "$pkgdir/usr/share/man/man${man##*.}/${man##*/}"
  done
  install -Dm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname"
}

package_python-brotli() {
  pkgdesc+=' - python library'
  depends=('python')

  cd ${pkgbase}
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -Dm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname"
}

package_brotli-testdata() {
  pkgdesc+=' - test data'
  depends=()

  cd ${pkgbase}
  install -dm755 "$pkgdir"/usr/share/brotli
  cp -a tests/testdata "$pkgdir"/usr/share/brotli/
  install -Dm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname"
}

# vim:set ts=2 sw=2 et:
