# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=jbig2dec
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.20
pkgrel=1
pkgdesc='Decoder implementation of the JBIG2 image compression format (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://jbig2dec.com/'
msys2_repository_url="https://github.com/ArtifexSoftware/jbig2dec"
msys2_references=(
  "cpe: cpe:/a:artifex:jbig2dec"
)
license=('spdx:AGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-libpng")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-cc")
source=(https://github.com/ArtifexSoftware/jbig2dec/archive/${pkgver}/${_realname}-${pkgver}.tar.gz
        01-dont-run-configure-from-autogen.patch
        02-mingw-no-inlines.patch
        03-avoid-mingw-macro-name-conflict.patch)
sha256sums=('a9705369a6633aba532693450ec802c562397e1b824662de809ede92f67aff21'
            '8715e0b3dfd738f9db57c656859897a41bc43d308ba86318ced625736784b54b'
            '18731257e8bddcf76663c0734ba3711b2e5c7c13f0841b6833ec17f90aa23186'
            '7e4090fe884384b6e1c4f2c35b6127661a0ebbd75f3e9830425fa425c8d475a0')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/01-dont-run-configure-from-autogen.patch
  patch -p1 -i "${srcdir}"/02-mingw-no-inlines.patch

  # mingw-w64 defines __argc and __argv as macros, which leads to wrong types there
  patch -p1 -i "${srcdir}"/03-avoid-mingw-macro-name-conflict.patch
  ./autogen.sh
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  export lt_cv_deplibs_check_method='pass_all'
  ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --host=${MINGW_CHOST} \
      --target=${MINGW_CHOST} \
      --build=${MINGW_CHOST} \
      --enable-shared \
      --enable-static

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
