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

_realname=openturns
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.25
pkgrel=2
pkgdesc="Uncertainty treatment library (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://openturns.github.io/www"
msys2_repository_url="https://github.com/openturns/openturns"
msys2_references=(
  'aur: openturns'
)
license=('spdx:LGPL-3.0-only')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-ceres-solver"
         "${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-mpc"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-muparser"
         "${MINGW_PACKAGE_PREFIX}-nlopt"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-dill"
         "${MINGW_PACKAGE_PREFIX}-python-psutil"
         "${MINGW_PACKAGE_PREFIX}-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-python-scipy"
             "${MINGW_PACKAGE_PREFIX}-python-matplotlib"
             "${MINGW_PACKAGE_PREFIX}-python-pandas"
             "${MINGW_PACKAGE_PREFIX}-spectra"
             "${MINGW_PACKAGE_PREFIX}-swig")
source=("https://github.com/openturns/openturns/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-default-site-packages.patch")
sha256sums=('d9606f25824a84233b2ccf07e0d03716e387453a4be26fdd031bd0d47458d045'
            'f3795c535aee543e6895bb033647ac87e9038e51b8cb202cb34696f2529cbde2')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  # https://github.com/openturns/openturns/pull/2005
  patch -p1 -i "${srcdir}"/001-fix-default-site-packages.patch
}

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

  local -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DOPENTURNS_DOC_PATH=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DOPENTURNS_DOC_PATH=${MINGW_PREFIX}/share/licenses/${_realname} \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DUSE_SPHINX=OFF \
    "${_extra_config[@]}" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="$pkgdir" ${MINGW_PREFIX}/bin/cmake --install .

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*

  local PREFIX_MINGW=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_MINGW}|\${PACKAGE_PREFIX_DIR}|g" \
    -i "$pkgdir${MINGW_PREFIX}/lib/cmake/${_realname}/OpenTURNSConfig.cmake"
  find "$pkgdir${MINGW_PREFIX}/lib/cmake/${_realname}" -name '*.cmake' -exec \
    sed -s "s|${PREFIX_MINGW}|\$\{_IMPORT_PREFIX\}|g" -i {} \;

  local PREFIX_MSYS2=$(cygpath -am /)
  sed -s "s|${PREFIX_MSYS2::-1}||g" \
    -i "$pkgdir${MINGW_PREFIX}/lib/cmake/${_realname}/OpenTURNSConfig.cmake"
}
