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

_realname=libpng
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.6.37
_apngver=1.6.37
pkgrel=4
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="A collection of routines used to create PNG format graphics (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
license=('custom')
url="http://www.libpng.org/pub/png/libpng.html"
options=('strip' '!libtool' 'staticlibs')
source=("https://downloads.sourceforge.net/sourceforge/libpng/${_realname}-${pkgver}.tar.xz"
        "https://downloads.sourceforge.net/project/libpng-apng/libpng16/${_apngver}/${_realname}-${_apngver}-apng.patch.gz")
sha256sums=('505e70834d35383537b6491e7ae8641f1a4bed1876dbfe361201fc80868d88ca'
            '823bb2d1f09dc7dae4f91ff56d6c22b4b533e912cbd6c64e8762255e411100b6')
validpgpkeys=('8048643BA2C840F4F92A195FF54984BFA16C640F')  # Glenn Randers-Pehrson (mozilla) <glennrp+bmo@gmail.com>

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

  # Add animated PNG (apng) support
  patch -p1 -i "${srcdir}/${_realname}-${_apngver}-apng.patch"
  # autoreconf to get updated libtool files with clang support
  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ../"${_realname}-${pkgver}"/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --enable-shared --enable-static \
    as_ln_s="cp -pR"
  make
  msg2 "Build contributed programs"
  mkdir -p ".libs/contrib/pngminus" && cd ".libs/contrib/pngminus"
  cp -r "${srcdir}/${_realname}-${pkgver}/contrib/pngminus"/* .
# libpng.a and libpng.dll.a are basically symlinks to libpng16.a 
# and libpng16.dll.a that are created at "make install" but we are
# building the contributed programs before the "make install" so
# we have to link to libpng16.dll.a or libpng16.a if building 
# statically. When building the static programs, we might also
# want to build with the static version of zlib.
  make PNGINC="-I${srcdir}/${_realname}-${pkgver} -I${srcdir}/build-${MINGW_CHOST}" \
    CC="${MINGW_PREFIX}/bin/gcc" \
    PNGLIB_SHARED="-L../.. -lpng16" \
    PNGLIB_STATIC="../../libpng16.a" \
    ZLIB_STATIC="${MINGW_PREFIX}/lib/libz.a" \
    CFLAGS="$CFLAGS" \
    LDFLAGS="-L../.. $LDFLAGS" \
    png2pnm pnm2png png2pnm-static pnm2png-static
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  make check
}

package () {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}"

  install -D -m644 "${srcdir}/libpng-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  msg2 "Install contributed programs"
  cd ".libs/contrib/pngminus"

# deploy both the shared lib and static programs in case a developer wants to
# redistribute those.  I was also building statically for some internal testing.
  install -m0755 png2pnm.exe pnm2png.exe png2pnm-static.exe pnm2png-static.exe "$pkgdir${MINGW_PREFIX}/bin/"
}

