# Maintainer: Dirk Stolle

_realname=ntl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=11.6.0
pkgrel=1
pkgdesc="A library for doing number theory (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://libntl.org/'
msys2_repository_url='https://github.com/libntl/ntl'
msys2_references=(
  'archlinux: ntl'
  'cygwin: ntl'
  'gentoo: dev-libs/ntl'
)
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gf2x"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://libntl.org/${_realname}-${pkgver}.tar.gz")
sha256sums=('bc0ef9aceb075a6a0673ac8d8f47d5f8458c72fe806e4468fbd5d3daff056182')

prepare() {
  cd ${_realname}-${pkgver}/src
  rm -r libtool-origin
}

build() {
  cd ${_realname}-${pkgver}/src

  local -a extra_config
  if [[ ${MSYSTEM} == CLANG64 ]]; then
    extra_config+=("CXX=clang++" "TUNE=x86")
  elif [[ ${MSYSTEM} == CLANGARM64 ]]; then
    extra_config+=("CXX=clang++" "TUNE=generic")
  else
    # ucrt64 + mingw64
    extra_config+=("TUNE=x86")
  fi

  ./configure \
    DEF_PREFIX="${MINGW_PREFIX}" \
    SHARED=on \
    NTL_GF2X_LIB=on \
    NTL_GMP_LIP=on \
    NATIVE=off \
    "${extra_config[@]}" \
    LIBTOOL=libtool \
    LIBTOOL_LINK_FLAGS="-no-undefined" \
    CXXFLAGS="${CXXFLAGS}" LDFLAGS="${LDFLAGS}"

  make
}

check() {
  cd ${_realname}-${pkgver}/src

  make -k check
}

package() {
  cd ${_realname}-${pkgver}/src

  make PREFIX="${pkgdir}${MINGW_PREFIX}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/doc/copying.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
