# Maintainer: Developer <dev@opalos.org>
# Maintainer: Developer <dev@opalos.org>

pkgname=jbigkit
pkgver=%version%
pkgrel=%release%
pkgdesc="Data compression library/utilities for bi-level high-resolution images"
arch=(x86_64)
url="https://www.cl.cam.ac.uk/~mgk25/jbigkit/"
license=(GPL-2.0-or-later)
depends=(glibc)
provides=(
  libjbig85.so
  libjbig.so
)
source=(
  https://www.cl.cam.ac.uk/~mgk25/download/$pkgname-$pkgver.tar.gz
  $pkgname-2.1-shared_lib.patch
  $pkgname-2.1-build_warnings.patch
  $pkgname-2.1-ldflags.patch
  $pkgname-2.1-coverity.patch
)
sha512sums=('c4127480470ef90db1ef3bd2caa444df10b50ed8df0bc9997db7612cb48b49278baf44965028f1807a21028eb965d677e015466306b44683c4ec75a23e1922cf'
            '9f46ee24f8b5eb57935c9b2cd9bd3d61b422c2352143c59a68adee8880511dd0bba0a057f0ed070dd0111c9eb0504df060343261c824c5a5734c06a1aedb14c8'
            '9be32ccec414de5c5de7d508ff14e1f1f3a40f81a2de252ca66e0f2844a8546d353d6a7abe36895ac798816165b2c5d8a9d043ea3f0c832fecdd9170dd6f92bf'
            'fb0c1f35ba5469dd4f7f6bd2a933d3484191d2ecd8367ea865fd2396bc99825b3afa7fcd09fbf8e841acbf92ed01cf743a83df7991e7e03f9e6cf8fbe0925ae6'
            '10f4f154c4f824c9361266f27bea231b35ed4cac680587c8659b6e12c0c06141f04a22cf72dd847abcb833578d25dfe5142d10d757f8c14c70d943c8eb3d5612')
b2sums=('7bd82f0e10a2d3794739ee85b77c5407d58753a00e014e937735f9af3043347de88a6e567609bc26de488887a6d61b0a162387fb72732b9be7e550e3f9b9539e'
        '52c5f7384f499e15a636b8e0089153750ac02da4ac850260a3243ee06d1194133f1264abc3457e52c5a15ee1a4bfad0b31042975d80273bf741b75e634845745'
        '6114609e5186e5fae1678de00f8c7336970c18923d3d3c01fbc431e95387bfe5696fe7914947ea3903c972eb54dfd24efe0d9ac3c7f4512de26f6ab138ce590c'
        'a8e36381543c0f701a589c115577da2509f02f3002926ab86b62e29e9f8a12db65b5d56387a61aa7a053e49d59e79e968c477ce096c73aa24ad3f94e28dff6b5'
        '2650020ba8ef433a3b935917297c034f01f0296a2a7c9c23c47521b823cc79ac32df8a3695f57e885b91604b138fedc1fcfea1cda8d2a79e9fd3c9680f0c06a8')

prepare() {
  # instead of a static library, create a shared library
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-2.1-shared_lib.patch
  # fix build warnings
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-2.1-build_warnings.patch
  # apply distribution LDFLAGS
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-2.1-ldflags.patch
  # fix coverity issues
  patch -Np1 -d $pkgname-$pkgver -i ../$pkgname-2.1-coverity.patch
}

build() {
  export EXTRA_CFLAGS="$CFLAGS"
  make -C $pkgname-$pkgver
}

check() {
  # NOTE: tests can not be parallelized
  make test -C $pkgname-$pkgver -j1
}

package() {
  cd $pkgname-$pkgver
  install -vDm 644 libjbig/*.h -t "$pkgdir/usr/include/"
  install -vDm 755 libjbig/*.so.* -t "$pkgdir/usr/lib/"
  for lib in libjbig.so libjbig85.so; do
    ln -sv "$lib.$pkgver" "$pkgdir/usr/lib/$lib"
  done
  install -vDm 755 pbmtools/{jbgtopbm{,85},pbmtojbg{,85}} -t "$pkgdir/usr/bin/"
  install -vDm 644 pbmtools/*.1* -t "$pkgdir/usr/share/man/man1/"
}
