# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Nicolas Frattaroli <ovdev@fratti.ch>

_realname=pngnq
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1
pkgrel=3
pkgdesc="Pngnq is a tool for quantizing PNG images in RGBA format (mingw-w64)"
url="https://pngnq.sourceforge.io/"
license=("BSD")
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs" "${MINGW_PACKAGE_PREFIX}-libpng" "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz"
        no-syslog.patch
        configure-mistake.patch)
sha256sums=('c147fe0a94b32d323ef60be9fdcc9b683d1a82cd7513786229ef294310b5b6e2'
            '1bc7a7d87630a721def43876f9235bc72b58846553b85019e9d19e01c1cf26cd'
            '340d08b3e21515f9e3dcc6ba8282d69ac6057bcec60b23ffe7146a6c3fcb760e')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p0 -i "${srcdir}/no-syslog.patch"
  patch -p1 -i "${srcdir}/configure-mistake.patch"

  autoreconf -fi
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  sed "35i#include <zlib.h>" -i src/rwpng.c

  # The LIBS line fixes some build script weirdness where it only passes the zlib
  # link command to gcc in the right order, but not the libpng one.
  LIBS="`libpng-config --ldflags` -lz" \
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --mandir="${pkgdir}${MINGW_PREFIX}/share/man"
  make

}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  make prefix="${pkgdir}${MINGW_PREFIX}" mandir="${pkgdir}${MINGW_PREFIX}/share/man" install
}
