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

pkgname=('zlib' 'zlib-devel')
pkgver=1.3.1
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/"
msys2_repository_url="https://github.com/madler/zlib"
msys2_references=(
  "cpe: cpe:/a:gnu:zlib"
  "cpe: cpe:/a:zlib:zlib"
)
makedepends=('gcc' 'make' 'autotools')
source=("https://github.com/madler/zlib/releases/download/v${pkgver}/zlib-${pkgver}.tar.xz"{,.asc}
        zlib-1.2.13-configure.patch
        zlib-1.2.13-gzopen_w.patch
        zlib-1.2.13-minizip-fixuncrypt.patch)
sha256sums=('38ef96b8dfe510d42707d9c781877914792541133e1870841463bfa73f883e32'
            'SKIP'
            '43726a2170b298059391c0faec0c893aa7ca59b1201afd9f807c49d39ce25527'
            '3c5186940741cf380ab598362c564c792965e9d25906ef75087e9e7389becbc5'
            '3022d46bd933076cff754d8247eb12125cf72e4dbecdb8e90805cbe5393bd0b7')
validpgpkeys=('5ED46A6721D365587791E2AA783FCD8E58BCAFBA')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}

  patch -p2 -i ${srcdir}/zlib-1.2.13-configure.patch
  patch -p2 -i ${srcdir}/zlib-1.2.13-gzopen_w.patch
  patch -p2 -i ${srcdir}/zlib-1.2.13-minizip-fixuncrypt.patch
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  # configure doesn't really work.. but leave it here in case things improve
  export MSYSTEM=CYGWIN
  ./configure --prefix=/usr

  # Instead we use some "other" makefile
  make -j1 -f win32/Makefile.gcc \
    CFLAGS="${CFLAGS}" \
    SHAREDLIB=msys-z.dll

  make -f win32/Makefile.gcc install \
    DESTDIR="${srcdir}/dest" \
    SHAREDLIB=msys-z.dll \
    BINARY_PATH=/usr/bin \
    INCLUDE_PATH=/usr/include \
    LIBRARY_PATH=/usr/lib \
    prefix=/usr \
    SHARED_MODE=1

  install -D zlib.3 "${srcdir}/dest/usr/share/man/man3/zlib.3"
}

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
}
