# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=mathgl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.4.4
pkgrel=4
pkgdesc="Library for high-quality scientific graphics (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://mathgl.sourceforge.io/"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-qtwebkit")
         #"${MINGW_PACKAGE_PREFIX}-wxWidgets"
source=("${_realname}-${pkgver}.tar.gz::https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz"
        "${_realname}-2.4.2.patch")
sha256sums=('0e5977196635962903eaff9b2f759e5b89108339b6e71427036c92bfaf3149e9'
            '3ee9d25a5de4db07ad43412eb69d95cfb7e6ef88f3ea4a5036810efd81ea9f9c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/${_realname}-2.4.2.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  CXXFLAGS=" -Wno-deprecated-declarations" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -Denable-all-widgets=OFF \
    -Denable-double=ON \
    -Denable-fltk=ON \
    -Denable-gif=ON \
    -Denable-glut=ON \
    -Denable-gsl=ON \
    -Denable-hdf5=ON \
    -Denable-jpeg=ON \
    -Denable-mgl2=ON \
    -Denable-opengl=ON \
    -Denable-png=ON \
    -Denable-qt5=ON \
    -Denable-openmp=OFF \
    "../${_realname}-${pkgver}"

    # -Denable-pdf=ON
    # -Denable-wx=ON

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

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

  # fixup references in associated cmake modules
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/lib/cmake/mathgl/*.cmake; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}
