# Contributor: Felix Yan <felixonmars@archlinux.org>
# Contributor: 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>

pkgname=static-compat-brotli
_gitcommit=e61745a6b7add50d380cfd7d3883dd6c62fc2c71
pkgver=1.0.9
pkgrel=1
pkgdesc='Generic-purpose lossless compression algorithm'
url='https://github.com/google/brotli'
arch=('x86_64')
license=('MIT')
depends=('gcc-libs-static-compat')
makedepends=('static-compat-cmake' 'git')
options=('staticlibs')
source=(${pkgname}::"git+${url}#commit=${_gitcommit}")
sha512sums=('SKIP')

prepare() {
  cd ${pkgbase}
  git cherry-pick -n 09b0992b6acb7faa6fd3b23f9bc036ea117230fc # Fix broken Libs: in pc file
}

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

build() {
  source static-compat-environment
  cd ${pkgbase}
  static-compat-cmake -B build \
    -DCMAKE_BUILD_TYPE=Release
  make -C build VERBOSE=1
}

check() {
  source static-compat-environment
  cd ${pkgbase}
  make test
}

package() {
  source static-compat-environment
  cd ${pkgbase}
  make -C build DESTDIR="$pkgdir" install
  find "$pkgdir/$static_compat_prefix" -iname '*.so*' -delete
  for file in "$pkgdir/$static_compat_prefix"/lib/pkgconfig/*.pc; do
    sed -i -E 's/-lbrotli(enc|dec|common)/\0-static/g' "$file"
  done
  install -Dm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname"
}
