# Maintainer: Dirk Stolle

_realname=4ti2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.6.14
pkgrel=1
pkgdesc="Software package for algebraic, geometric and combinatorial problems on linear spaces (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://4ti2.github.io/'
msys2_repository_url='https://github.com/4ti2/4ti2'
msys2_references=(
  'archlinux: 4ti2'
  'cygwin: lib4ti2'
  'gentoo: sci-mathematics/4ti2'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-glpk"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         'bash')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
source=("https://github.com/4ti2/4ti2/releases/download/Release_${pkgver//./_}/4ti2-${pkgver}.tar.gz")
sha256sums=('1bc340173f93ca4abd30ea962118bd5057fdedf7e79c71d2a0c4cc9569f8b0b1')

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

  autoreconf -fiv
}

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

  # _GNU_SOURCE has to be defined to enable the function asprintf(), which is a
  # GNU extension and not part of standard C or POSIX. But 4ti2 1.6.14 uses it
  # in various places. Also see <https://github.com/4ti2/4ti2/issues/57> for
  # the upstream discussion about it.
  CFLAGS+=" -D_GNU_SOURCE "

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

  # Fix overlinking (copied from Arch's PKGBUILD)
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool

  make
}

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

  make check
}

package() {
  cd "build-${MSYSTEM}"

  make install DESTDIR="${pkgdir}"

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