# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=f2cblaslapack
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.8.0.q2
pkgrel=1
pkgdesc="f2c BLAS/LAPACK (mingw-w64)"
provides=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] && echo "${MINGW_PACKAGE_PREFIX}-lapack"))
arch=('any')
mingw_arch=('clang64' 'clangarm64')
url='https://www.petsc.org/' # Part of petsc
msys2_references=(
  'archlinux: '
  'aur: '
  'cygwin: '
  'gentoo: '
)
license=('spdx:BSD-2-Clause')
source=("http://ftp.mcs.anl.gov/pub/petsc/externalpackages/${_realname}-${pkgver}.tar.gz"
        001-no-petsc-conf.patch
        libf2cblas-i686.def
        libf2cblas-x86_64.def
        libf2clapack-i686.def
        libf2clapack-x86_64.def)
sha256sums=('12fa8d5001e313aeeea22eb2054883f59e3e3c656f11129f31abb02ddbf8ad60'
            '7a77aa2621945d2103bb85735b0e9d08526e1943b7f055c1ee7680fd401064f1'
            '57b49b7816ee67ab7c2b36da182931f06508a83be45493fa638fd05348033c6a'
            '57b49b7816ee67ab7c2b36da182931f06508a83be45493fa638fd05348033c6a'
            '7599c4e33f06f42f0c82776794a6a01d13fd82c6851a8be111743533e59d675d'
            '7599c4e33f06f42f0c82776794a6a01d13fd82c6851a8be111743533e59d675d')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-no-petsc-conf.patch

  # The following .def files were created in the MINGW32 or MINGW64
  # environments, respectively.  They have been slightly adapted for line
  # endings and formatting.
  # Commands used after the static libraries have been built:
  # dlltool --export-all-symbols -z blas/libf2cblas.def libf2cblas.a
  # dlltool --export-all-symbols -z lapack/libf2clapack.def libf2clapack.a
  if [[ "${CARCH}" == "i686" ]]; then
    cp "${srcdir}"/libf2cblas-i686.def "${srcdir}"/${_realname}-${pkgver}/blas/libf2cblas.def
    cp "${srcdir}"/libf2clapack-i686.def "${srcdir}"/${_realname}-${pkgver}/lapack/libf2clapack.def
  else
    cp "${srcdir}"/libf2cblas-x86_64.def "${srcdir}"/${_realname}-${pkgver}/blas/libf2cblas.def
    cp "${srcdir}"/libf2clapack-x86_64.def "${srcdir}"/${_realname}-${pkgver}/lapack/libf2clapack.def
  fi
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  make

  # link shared libraries
  ${CC} -shared \
    -Wl,--export-all-symbols -Wl,--whole-archive libf2cblas.a -Wl,--no-whole-archive -Wl,blas/libf2cblas.def \
    -o libf2cblas.dll -Wl,--enable-auto-image-base -Xlinker --out-implib -Xlinker libf2cblas.dll.a
  ${CC} -shared \
    -Wl,--export-all-symbols -Wl,--whole-archive libf2clapack.a -Wl,--no-whole-archive -Wl,lapack/libf2clapack.def -L. -lf2cblas \
    -o libf2clapack.dll -Wl,--enable-auto-image-base -Xlinker --out-implib -Xlinker libf2clapack.dll.a
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  install -Dm644 libf2cblas.a "${pkgdir}"${MINGW_PREFIX}/lib/libf2cblas.a
  install -Dm644 libf2clapack.a "${pkgdir}"${MINGW_PREFIX}/lib/libf2clapack.a
  install -Dm644 libf2cblas.dll.a "${pkgdir}"${MINGW_PREFIX}/lib/libf2cblas.dll.a
  install -Dm644 libf2clapack.dll.a "${pkgdir}"${MINGW_PREFIX}/lib/libf2clapack.dll.a
  install -Dm644 libf2cblas.dll "${pkgdir}"${MINGW_PREFIX}/bin/libf2cblas.dll
  install -Dm644 libf2clapack.dll "${pkgdir}"${MINGW_PREFIX}/bin/libf2clapack.dll

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    # "pretend" to be reference BLAS/LAPACK
    install -Dm644 libf2cblas.a "${pkgdir}"${MINGW_PREFIX}/lib/libblas.a
    install -Dm644 libf2clapack.a "${pkgdir}"${MINGW_PREFIX}/lib/liblapack.a
    install -Dm644 libf2cblas.dll.a "${pkgdir}"${MINGW_PREFIX}/lib/libblas.dll.a
    install -Dm644 libf2clapack.dll.a "${pkgdir}"${MINGW_PREFIX}/lib/liblapack.dll.a
    install -Dm644 libf2cblas.dll "${pkgdir}"${MINGW_PREFIX}/bin/libblas.dll
    install -Dm644 libf2clapack.dll "${pkgdir}"${MINGW_PREFIX}/bin/liblapack.dll
  fi
}
