# Maintainer: Dirk Stolle

_realname=gf2x
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.3.0
pkgrel=1
pkgdesc="A library for multiplying polynomials over the binary field (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.inria.fr/gf2x/gf2x'
msys2_references=(
  'anitya: 6340'
  'archlinux: gf2x'
  'gentoo: dev-libs/gf2x'
)
license=('spdx:GPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${_realname}-${pkgver}.tar.gz"::"https://gitlab.inria.fr/gf2x/gf2x/-/archive/gf2x-${pkgver}/gf2x-gf2x-${pkgver}.tar.gz"
        '001-fix-include.patch')
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('11bcf98b620c60c2ee3b4460b02b7be741f14cfdc26b542f22c92950926575e0'
            'ae021d15308d8806f1967eec84954b0cf95b09d537cb499e067e21071291f7b0')

prepare() {
  # Workaround for symlinks in archive, see <https://www.msys2.org/docs/symlinks/>.
  bsdtar -xzf "${srcdir}/${_realname}-${pkgver}.tar.gz" 2>/dev/null || bsdtar -xzf "${srcdir}/${_realname}-${pkgver}.tar.gz"

  # Adjust name from gf2x-gf2x-x.y.z to gf2x-x.y.z.
  mv "${srcdir}/${_realname}-${_realname}-${pkgver}"  "${srcdir}/${_realname}-${pkgver}"

  cd "${_realname}-${pkgver}"

  # Apply commit a2f0fd388c12ca0b9f4525c6cfbc515418dcbaf8 to fix build.
  # See <https://gitlab.inria.fr/gf2x/gf2x/-/commit/a2f0fd388c12ca0b9f4525c6cfbc515418dcbaf8>.
  patch -Np1 -i "${srcdir}/001-fix-include.patch"

  autoreconf -fiv
}

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

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

  make
}

check() {
  cd "build-${MSYSTEM}"
  make -k check
}

package() {
  cd "build-${MSYSTEM}"
  make prefix="${pkgdir}${MINGW_PREFIX}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
