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

_realname=libguess
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2
pkgrel=6
pkgdesc="High-speed character set detection library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/kaniini/libguess"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-libmowgli")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
source=("https://sources.archlinux.org/other/packages/libguess/${_realname}-${pkgver}.tar.bz2"
        "001-aarch64.patch")
sha256sums=('8019a16bdc7ca9d2efbdcc1429d48d033d5053d42e45fccea10abf98074f05f8'
            'eb8adbf4e6399abd82137ae5c23b21df856f498629b68fe1d1c1d0e4a32d7bd0')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

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

  sed 's/libmowgli >= 0.9.50/libmowgli-2/g' -i configure

  apply_patch_with_msg \
    001-aarch64.patch
}

build() {
  [[ -d "build-${MSYSTEM}" ]] && rm -rf "build-${MSYSTEM}"
  cp -rf ${_realname}-${pkgver} "${srcdir}"/build-${MSYSTEM}
  cd build-${MSYSTEM}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make
}

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