# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=taucs
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.2.1
pkgrel=1
pkgdesc="TAUCS is a C library of sparse linear solvers (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
        "${MINGW_PACKAGE_PREFIX}-metis"
		"${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc")
url="https://www.tau.ac.il/~stoledo/taucs/"
license=('spdx:Apache-2.0 OR LGPL-3.0-or-later OR GPL-3.0-or-later')
source=("https://github.com/sivantoledo/taucs/archive/refs/tags/v2.2.1.tar.gz"
        "000_fix_mingw.patch"
	"001_added_compilation_flag.patch::https://github.com/tunnellm/taucs/commit/e9148b39a34fe3ac443140f34570364bd735698d.patch"
	"002_default_not_compile.patch::https://github.com/tunnellm/taucs/commit/dc483ddf28a32ee62bc26e0fc96d272e47a14d36.patch"
	"003_not_cilk.patch::https://github.com/tunnellm/taucs/commit/3077a1f11742f6bd509e0b604e7549dad9d02326.patch")
sha256sums=('7b682b602bc173110bc68e586a7d36129cff6bf59c6766b518e60adff16a31e5'
            '3a39b4db1c5fdbe9dc3326e6818259167fb1811f6f8a7451021d0b20c47a8275'
            'b8f1d876f9d8448e3657c19dde8d65ff992ba2d126844d0ab34481ec77f2c03a'
            '4541a9ef128493582b2c1356e0d20eeed0382ec10d3805c8efc1ebdb2f2f7562'
            'ca362ae30c277ad956199f2bcaf46a99de8571c3075b27b541f29b5bab2e94ba')

prepare() {
  #cd "${srcdir}/${_realname}-${pkgver}"
  #autoreconf -fiv
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}"/000_fix_mingw.patch
  patch -Np1 -i "${srcdir}"/001_added_compilation_flag.patch
  patch -Np1 -i "${srcdir}"/002_default_not_compile.patch
  patch -Np1 -i "${srcdir}"/003_not_cilk.patch
  
  find . -type f -not -name 'taucs.h' -print0 | xargs -0 sed -i 's/#ifdef OSTYPE_win32/#ifdef _WIN32/g'
  find . -type f -print0 | xargs -0 sed -i 's/#elif defined(OSTYPE_win32)/#elif defined(_WIN32)/g'
  find . -type f -print0 | xargs -0 sed -i 's/#ifndef OSTYPE_win32/#ifndef _WIN32/g'
}

build() {
  cd $srcdir/${_realname}-${pkgver}
  
  export OSTYPE=mingw
  
  ./configure

  make
}

package() {
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{lib,include/${_realname},bin}
  cd "${srcdir}/${_realname}-${pkgver}/lib/mingw"
  install -Dm644 lib*.a "${pkgdir}${MINGW_PREFIX}/lib"

  find "${srcdir}/${_realname}-${pkgver}" -type f -name "*.h" -exec cp {} "${pkgdir}${MINGW_PREFIX}/include/taucs/" \;
  find "${srcdir}/${_realname}-${pkgver}" -type f -name "*.exe" -exec cp {} "${pkgdir}${MINGW_PREFIX}/bin" \;
  
  # Fix permissions
  #cd "${pkgdir}"${MINGW_PREFIX}/include/taucs
  #chmod -R oug+r *
  
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/LICENSE.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/apache-2.0.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/apache-2.0
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/gpl-3.0.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/gpl-3.0
  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/lgpl-3.0.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/lgpl-3.0
}
