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

_realname=leptonica
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.81.1
pkgrel=1
pkgdesc="Leptonica library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="http://www.leptonica.org"
license=("Leptonica")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
checkdepends=("${MINGW_PACKAGE_PREFIX}-gnuplot")
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-giflib
         ${MINGW_PACKAGE_PREFIX}-libtiff
         ${MINGW_PACKAGE_PREFIX}-libpng
         ${MINGW_PACKAGE_PREFIX}-libwebp
         ${MINGW_PACKAGE_PREFIX}-openjpeg2
         ${MINGW_PACKAGE_PREFIX}-zlib)
source=(https://github.com/DanBloomberg/leptonica/releases/download/${pkgver}/${_realname}-${pkgver}.tar.gz)
sha256sums=('0f4eb315e9bdddd797f4c55fdea4e1f45fca7e3b358a2fc693fd957ce2c43ca9')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  # autoreconf to get updated libtool files with clang support
  autoreconf -fiv
}

build() {
  local _builddir="${srcdir}/build-${MINGW_PACKAGE_PREFIX}"
  test -d "${_builddir}" && rm -rf "${_builddir}"
  mkdir -p "${_builddir}" && cd "${_builddir}"
  CFLAGS+=" -DMINIMUM_SEVERITY=L_SEVERITY_WARNING"
  ../${_realname}-${pkgver}/configure -C \
    --disable-dependency-tracking \
    --disable-silent-rules \
    --host="${MINGW_CHOST}" \
    --prefix="${MINGW_PREFIX}"
  make
}

check() {
  make -j1 check -C "${srcdir}/build-${MINGW_PACKAGE_PREFIX}"
}

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

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

  install -D -m644 "${srcdir}/${_realname}-${pkgver}/leptonica-license.txt" \
      "${pkgdir}/${MSYSTEM_PREFIX}/share/licenses/${_realname}/leptonica-license.txt"
}
