# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

pkgname=('zlib' 'zlib-devel')
pkgver=1.2.11
pkgrel=1
pkgdesc='Compression library implementing the deflate compression method found in gzip and PKZIP'
arch=('i686' 'x86_64')
groups=('libraries')
license=('custom')
url="https://www.zlib.net/"
makedepends=('gcc' 'make')
source=(#"https://zlib.net/current/zlib-${pkgver}.tar.gz"
        "https://sourceforge.net/projects/libpng/files/zlib/${pkgver}/zlib-${pkgver}.tar.gz"
        1.2.7-minizip-cygwin.patch
        1.2.7-zlib-symbols.patch
        1.2.11-cygwin-no-widechar.patch
        zlib-1.2.11-msys2.patch)
sha256sums=('c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1'
            '425e2e36e8c788f0fa725c9b713642d5ae2f6629eab8613a2c2886eed2de5c21'
            '16e9ce18df14a700dda0572843cdb42b5cb3d398c64706af0acc7cd77c181b7e'
            'ba195803acf87b49b2f007bc83015c138a19e666e1e935b8116b4928b88d72eb'
            '31d675180f528af0b172bbf09f1e9b2014acc1a0a8a23e0cc172a88eb3eb675f')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}
  grep -A 24 '^  Copyright' zlib.h > LICENSE

  patch -p2 -i ${srcdir}/1.2.7-minizip-cygwin.patch
  patch -p2 -i ${srcdir}/1.2.7-zlib-symbols.patch
  patch -p1 -i ${srcdir}/1.2.11-cygwin-no-widechar.patch
  patch -p1 -i ${srcdir}/zlib-1.2.11-msys2.patch
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}
  ./configure --prefix=/usr
  make
  make install DESTDIR="${srcdir}/dest"
}

check() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make test
}

package_zlib() {
  depends=('gcc-libs')

  mkdir -p ${pkgdir}/usr
  cp -rf $srcdir/dest/usr/bin ${pkgdir}/usr/
  cp -rf $srcdir/dest/usr/share ${pkgdir}/usr/
  install -D -m644 ${srcdir}/${pkgname}-${pkgver}/LICENSE ${pkgdir}/usr/share/licenses/zlib/LICENSE
}

package_zlib-devel() {
  pkgdesc="zlib headers and libraries"
  groups=('development')
  options=('staticlibs')  # needed by binutils testsuite
  depends=("zlib=${pkgver}")

  mkdir -p ${pkgdir}/usr
  cp -rf $srcdir/dest/usr/include ${pkgdir}/usr/
  cp -rf $srcdir/dest/usr/lib ${pkgdir}/usr/
  sed -e "s| -L\${sharedlibdir}||g" -i ${pkgdir}/usr/lib/pkgconfig/zlib.pc
}
