# 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"
# FIXME: python-brotli is an example of something we dont' want to bootstrap if we don't have to
pkgname=("brotli" "brotli-testdata")  # 'python-brotli'
_gitcommit=ed738e842d2fbdf2d6459e39267a633c4a9b2f5d
pkgver=1.1.0
pkgrel=1.1
pkgdesc='Generic-purpose lossless compression algorithm'
url='https://github.com/google/brotli'
license=('MIT')
ncxfat_makedepends=(binutils gcc glibc)
ncxfab_makedepends=(
  base-devel
  git
  cmake
  python-setuptools
  python-build
  python-installer
  python-wheel
)
source=(brotli::"git+${url}#commit=${_gitcommit}" aarch64.cmake)
sha512sums=('SKIP' 'SKIP')

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

prepare() {
    cd brotli
    if (( MPKG_CROSSCOMPILED )); then
        cp ../$MPKG_TARGETARCH.cmake .
    fi
}

build() {
  cd brotli
  python -m build --wheel --no-isolation
  if (( MPKG_CROSSCOMPILED )); then
      _TOOLCHAIN_OPT="-DCMAKE_TOOLCHAIN_FILE=$MPKG_TARGETARCH.cmake"
  fi

  cmake -S . -B build \
    $_TOOLCHAIN_OPT \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=$MPKG_USRPATH \
    -DBUILD_SHARED_LIBS=True \
    -DCMAKE_C_FLAGS="$CFLAGS -ffat-lto-objects"
  cmake --build build -v
}

check() {
  cd brotli
  local python_version=$(python -c 'import sys; print("".join(map(str, sys.version_info[:2])))')
  PYTHONPATH="$PWD/bin/lib.linux-$CARCH-cpython-${python_version}" python -m unittest discover python "*_test.py"
  cd build
  ctest --output-on-failure --stop-on-failure -j$(nproc)
}

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

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

# package_python-brotli() {
#   pkgdesc+=' - python library'
#   ncxfah_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'
  ncxfah_depends=()

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

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