# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
# Contributor: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=libimagequant
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.4.0
pkgrel=1
pkgdesc="Palette quantization library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/ImageOptim/libimagequant'
msys2_references=(
  'purl: pkg:cargo/imagequant'
)
license=('spdx:GPL-3.0-or-later' OR 'custom')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cargo-c")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
source=("https://github.com/ImageOptim/${_realname}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('22f90ad2ba87fe44d8156862798491bf056034f031a9b8b95d720669047ede20')

prepare() {
  cp -r ${_realname}-${pkgver} build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cargo fetch \
    --manifest-path build-${MSYSTEM}/imagequant-sys/Cargo.toml
}

build() {
  ${MINGW_PREFIX}/bin/cargo cbuild \
    --release \
    --frozen \
    --meson-paths \
    --manifest-path build-${MSYSTEM}/imagequant-sys/Cargo.toml
}

package() {
  cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/cargo cinstall \
    --frozen \
    --offline \
    --meson-paths \
    --destdir="${pkgdir}" \
    --prefix=${MINGW_PREFIX} \
    --manifest-path imagequant-sys/Cargo.toml

  # Remove def file
  rm -f "${pkgdir}"${MINGW_PREFIX}/lib/*.def

  install -Dm644 CHANGELOG "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/CHANGELOG
  install -Dm644 README.md "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/README.md
  install -Dm644 COPYRIGHT "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT
}
