# Contributor: David Runge <dvzrv@archlinux.org>
# Contributor: Gaetan Bisson <bisson@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: Simone Sclavi 'Ito' <darkhado@gmail.com>

_pkgname=libjpeg-turbo
pkgname=static-compat-$_pkgname
pkgver=2.1.5
pkgrel=1
pkgdesc="JPEG image codec with accelerated baseline compression and decompression"
arch=(x86_64)
url="https://libjpeg-turbo.org/"
license=(BSD)
depends=(glibc-static-compat)
makedepends=(static-compat-cmake nasm ninja)
options=(staticlibs)
source=(https://sourceforge.net/projects/$_pkgname/files/$pkgver/$_pkgname-$pkgver.tar.gz{,.sig}
        $_name-initialize-simd_support.patch::https://github.com/libjpeg-turbo/libjpeg-turbo/commit/4e028ecd63aa.patch)
sha512sums=('ceab6d1c3c46d2816b06402e3b912e950d887fc8a6fb32fa6876f4aaa663b4769d8f4c50e1b967addf6704d0760ca14ed723d55e0312d7ca69e7f2a91484c0dc'
            'SKIP'
            '9275268b5e138ee76a7245aded05917a16c05951441d53bd1b6e65a306a75dc0d0c672c7aa5d52edce7ec197a7fba5658ffe7db0a406d83a88d55819338a5023')
b2sums=('c65de2717ddc33447064412738b2db94a92693faad18f29f14c5c368d9eb4c617fe447a6f9428d9a101458dec0c65abc527e20632571eaa727d98cd5435241d4'
        'SKIP'
        '9bbad32e30289951a95ebef4ce36a0045d69f50a73a72a4f4820257acabfb6e6a9234538bfcc76441885c2fccf01d44008f7795c3b85921b23cbb1e4a0c05228')
validpgpkeys=('0338C8D8D9FDA62CF9C421BD7EC2DBB6F4DBF434') # The libjpeg-turbo Project (Signing key for official binaries) <information@libjpeg-turbo.org>

build() {
  source static-compat-environment
  local cmake_options=(
    -B build
    -DCMAKE_INSTALL_LIBDIR="$static_compat_prefix"/lib
    -DWITH_JAVA=OFF
    -DWITH_JPEG8=ON
    -G Ninja
    -S $_pkgname-$pkgver
    -W no-dev
  )
  static-compat-cmake "${cmake_options[@]}"
  cmake --build build -v
}

check() {
  source static-compat-environment
  ctest --test-dir build --output-on-failure --stop-on-failure -j$(nproc)
}

package() {
  source static-compat-environment
  export docdir="$static_compat_prefix"/share/doc/$_pkgname exampledir="$static_compat_prefix"/share/doc/$_pkgname
  DESTDIR="$pkgdir" cmake --install build -v

  install -d "$pkgdir/usr/share/licenses/$pkgname"
  ln -s ../../doc/$_pkgname/LICENSE.md "$pkgdir/usr/share/licenses/$pkgname"
  # header required by some dependants
  # https://bugs.archlinux.org/task/24787
  install -vDm 644 $_pkgname-$pkgver/jpegint.h "$pkgdir/$static_compat_prefix/include"
  rm -r "$pkgdir/$static_compat_prefix"/share
  find "$pkgdir/$static_compat_prefix"/lib -iname '*.so*' -delete
}
