# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=optipng
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.9.1
pkgrel=1
pkgdesc="Compresses PNG files to a smaller size, without losing any information (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://optipng.sourceforge.io/"
msys2_references=(
  "cpe: cpe:/a:optipng:optipng"
  "cpe: cpe:/a:optipng_project:optipng"
)
license=('spdx:Zlib')
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/sourceforge/${_realname}/${_realname}-${pkgver}.tar.gz")
sha256sums=('c2579be58c2c66dae9d63154edcb3d427fef64cb00ec0aff079c9d156ec46f29')

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

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}
  cp -R "${srcdir}"/${_realname}-${pkgver}/* "${srcdir}"/build-${MSYSTEM}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --with-system-libpng \
    --with-system-zlib

  make
}

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

  # Install license
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
