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

_realname=libwmf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.2.12
pkgrel=3
pkgdesc="Library for Converting Metafile Images (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://wvWare.sourceforge.io/"
license=("GPL" "LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gtk2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "libtool")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('strip' 'staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/caolanm/libwmf/archive/v${pkgver}.tar.gz
        libwmf-0.2.8.4-libpng.patch
        libwmf-freetype.patch
        libwmf-check-incoming-metric-for-nullptr.patch)
sha256sums=('464ff63605d7eaf61a4a12dbd420f7a41a4d854675d8caf37729f5bc744820e2'
            '6d00df85c02d1db26e67d0ec78a47a4c314fd0bf152df8284a527084bdd73b13'
            'c2c7c0eba25dfab9fc97e2e212cc7f12e0342f84d87ed0e5cfca3a4a50e48900'
            '68d6646405a126f8c57facfb2c41e9902ef7f9cdbd45a64f78a4c012b6e00923')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/libwmf-0.2.8.4-libpng.patch
  patch -p1 -i ${srcdir}/libwmf-freetype.patch
  patch -p1 -i ${srcdir}/libwmf-check-incoming-metric-for-nullptr.patch #can be deleted the next update
  libtoolize --force --copy --install
  autoreconf -fiv -Ipatches
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  # for ucrt
  export ac_cv_func_sscanf=yes

  "${srcdir}"/${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    --with-libxml2 \
    --without-x

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="$pkgdir" install
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  rm "${pkgdir}${MINGW_PREFIX}"/lib/gdk-pixbuf-2.0/2.*/loaders/*.dll.a

  # fix .pc file
  sed -s "s|$(cygpath -m ${MINGW_PREFIX})|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/libwmf.pc
}
