# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer (ArchLinux): Andrzej Giniewicz <gginiu@gmail.com>
# Contributor (ArchLinux): Cristóvão D. Sousa <crisjss@gmail.com>

_realname=dsdp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.8
pkgrel=4
pkgdesc="A free open source implementation of an interior-point method for semidefinite programming (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.mcs.anl.gov/hs/software/DSDP"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || \
               echo "${MINGW_PACKAGE_PREFIX}-gcc-fortran"))
source=("https://www.mcs.anl.gov/hs/software/${_realname^^}/${_realname^^}${pkgver}.tar.gz")
#        "make.include.patch")
sha256sums=('26aa624525a636de272c0b329e2dfd01a0d5b7827f1c1c76f393d71e37dead70')

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

  sed -i 's|-llapack||g' make.include
  sed -i 's|-lblas|-lopenblas|g' make.include
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    sed -i 's|-lg2c|-lgfortran|g' make.include
  else
    sed -i 's|-lg2c||g' make.include
  fi
  sed -i 's|^CC\s*=|#&|' make.include
#  patch -p1 -i "${srcdir}"/make.include.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  cp -rf ${_realname^^}${pkgver} build-${MSYSTEM} &&  cd build-${MSYSTEM}

  make DSDPROOT="${PWD}" dsdpapi
}

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

  install -d "${pkgdir}"${MINGW_PREFIX}/{lib,include/dsdp}
  install -Dm755 bin/dsdp5 "${pkgdir}"${MINGW_PREFIX}/bin/dsdp5
  install -Dm644 lib/* "${pkgdir}"${MINGW_PREFIX}/lib/
  install -Dm644 include/*.h "${pkgdir}"${MINGW_PREFIX}/include/dsdp/
  install -Dm644 dsdp-license "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/dsdp-license
}
