# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Levente Polyak <anthraxx@archlinux.org>
# Contributor: Pierre Schmitz <pierre@archlinux.de>

pkgbase=zlib
pkgname=(zlib minizip)
epoch=1
pkgver=1.3
pkgrel=2
pkgdesc='Compression library implementing the deflate compression method found in gzip and PKZIP'
arch=(x86_64)
license=(Zlib)
url="https://www.zlib.net/"
options=(
  staticlibs  # needed by binutils testsuite
  debug
)
source=(https://github.com/madler/zlib/releases/download/v$pkgver/$pkgname-$pkgver.tar.xz{,.asc}
        zlib-1.3-CVE-2023-45853.patch::https://github.com/madler/zlib/commit/73331a6a0481067628f065ffe87bb1d8f787d10c.patch)
sha512sums=('3868ac4da5842dd36c9dad794930675b9082ce15cbd099ddb79c0f6bd20a24aa8f33a123f378f26fe0ae02d91f31f2994dccaac565cedeaffed7b315e6ded2a2'
            'SKIP'
            '77de58fec1875b72ba47e2c21ff5cafd3251924baf047b4eb520fdecc63e9414c34d9224dae86982161d7e4f867120c6a7f4c5165adf6b090205513865e82e09')
b2sums=('5fe0f32339267348a313f23a21e9588bdb180b7415be303c85f5f169444d019e5f176ef7322f6e64297c360acc2a6041c50e2f66d1860e5c392d8970990f176a'
        'SKIP'
        'bba55b9074c6ac863b5dc65c3a087a2f5b9190e69106d9b2009ec3bd89e20a940091cb10464b3014c46c3c2e5248d578169164dfb9096b8ebcea08de4feee442')
validpgpkeys=('5ED46A6721D365587791E2AA783FCD8E58BCAFBA')  # Mark Adler <madler@alumni.caltech.edu>

prepare() {
  cd $pkgbase-$pkgver/contrib/minizip
  patch -Np3 < "${srcdir}/zlib-1.3-CVE-2023-45853.patch"
  cp Makefile Makefile.orig
  cp ../README.contrib readme.txt
  autoreconf -fiv
}

build() {
  cd $pkgbase-$pkgver

  CFLAGS+=" -ffat-lto-objects"
  ./configure --prefix=/usr
  make

  cd contrib/minizip
  ./configure --prefix=/usr --enable-static=no
  make
}

check() {
  make test -C $pkgbase-$pkgver

  make -f Makefile.orig test -C $pkgbase-$pkgver/contrib/minizip
}

package_zlib() {
  depends=(glibc)
  provides=(libz.so)

  make install DESTDIR="$pkgdir" -C $pkgbase-$pkgver
  install -D -m644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}

package_minizip() {
  pkgdesc='Mini zip and unzip based on zlib'
  depends=(
    glibc
    zlib
  )

  make install DESTDIR="$pkgdir" -C $pkgbase-$pkgver/contrib/$pkgname
  install -D -m644 $pkgbase-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"

  # https://github.com/madler/zlib/pull/229
  rm "$pkgdir/usr/include/minizip/crypt.h"
}

# vim: ts=2 sw=2 et:
