#!/usr/bin/bash
# JOBoRun		: Jwm OpenBox Obarun RUNit
# Maintainer	: Joe Bo Run <joborun@disroot.org>
# PkgSource		: url="https://gittea.disroot.org/joborun-pkg/jobcore/$pkgname"
# Website		: https://pozol.eu
#-----------------------------------------| DESCRIPTION |---------------------------------------

pkgbase=zlib
pkgname=(zlib minizip)
pkgdesc='Compression library implementing the deflate compression method found in gzip and PKZIP'
epoch=1
pkgver=1.3
pkgrel=02
url="https://www.zlib.net/"
depends=('glibc')
makedepends=('automake' 'autoconf')
options=('staticlibs')  # needed by binutils testsuite
#options=('staticlibs' 'debug')   ## uncomment this to have the debug pkg produced
source=("https://zlib.net/zlib-${pkgver}.tar.gz"{,.asc}
        zlib-1.3-CVE-2023-45853.patch::https://github.com/madler/zlib/commit/73331a6a0481067628f065ffe87bb1d8f787d10c.patch)


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() {
  pkgdesc='Compression library implementing the deflate compression method found in gzip and PKZIP'
  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=('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"
}

#---- arch license gpg-key & sha256sums ----

arch=(x86_64)

#license=('custom')
license=(Zlib)

validpgpkeys=('5ED46A6721D365587791E2AA783FCD8E58BCAFBA')

sha256sums=(ff0ba4c292013dbc27530b3a81e1f9a813cd39de01ca5e0f8bf355702efa593e  # zlib-1.3.tar.gz
	bf1e20cc852e179fa38093565c3910d5ad9cd44eee4c45dd7e5445adcc0be1c3  # zlib-1.3.tar.gz.asc
	7aa0221bf62796c29ae665ef3dd138489e1995b7095924dc035104959da13417) # zlib-1.3-CVE-2023-45853.patch

# Note to packager: 
ls -l *pkg.tar.lz 
echo "you must rename this package to meet SF naming before you move to the repo"
echo "mv $pkgname-$epoch:$pkgver-$pkgrel-$arch.pkg.tar.lz $pkgname-$epoch_$pkgver-$pkgrel-$arch.pkg.tar.lz"

##  731add84f49cc773541fd2da4056215265b339daf7957eaed9ee78e60d2185f4  minizip-1_1.3-02-x86_64.pkg.tar.lz
##  adcb4679cd923d0367ca16a7937610f727cdf33bd06a7a4be7e075cd33978e62  zlib-1_1.3-02-x86_64.pkg.tar.lz
