# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pybind11
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.6.0
pkgrel=2
pkgdesc="A lightweight header-only library that exposes C++ types in Python and vice versa (mingw-w64)"
url='https://pybind11.readthedocs.org/'
license=('BSD')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
optdepends=("${MINGW_PACKAGE_PREFIX}-python: to target bindings supporting python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-breathe"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx_rtd_theme"
             "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-moderncmakedomain"
             "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-svg2pdfconverter"
             "${MINGW_PACKAGE_PREFIX}-python-pytest")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/pybind/pybind11/archive/v${pkgver}.tar.gz"
        01-mingw-python.patch)
sha256sums=('90b705137b69ee3b5fc655eaca66d0dc9862ea1759226f7ccd3098425ae69571'
            'af43a5bcb89bc1b07e0bb4b23213f526d4c775fe3570b03333ed3d191ce7e031')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/01-mingw-python.patch
}

build () {
  cd "${srcdir}/${_realname}-${pkgver}"
  ${MINGW_PREFIX}/bin/python setup.py build

  cd "${srcdir}/${_realname}-${pkgver}/build"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    ..
  make mock_install

  cd "${srcdir}/${_realname}-${pkgver}/docs"
  make man
}

package() {
  local _pythonver
  _pythonver="$(${MINGW_PREFIX}/bin/python --version | awk '{ print $2 }' | grep -o '^[0-9]*\.[0-9]*')"
  
  cd "${srcdir}/${_realname}-${pkgver}/build"
  make install DESTDIR="$pkgdir"

  # python modules
  cd "${srcdir}/${_realname}-${pkgver}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install_lib --install-dir="${pkgdir}${MINGW_PREFIX}/lib/python${_pythonver}/site-packages" --optimize='1'

  # C++ headers
  #cd "${srcdir}/${_realname}-${pkgver}"
  #MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  #${MINGW_PREFIX}/bin/python setup.py install_headers --install-dir="${pkgdir}${MINGW_PREFIX}/include/${_realname}"
  
  # cmake files
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/cmake/${_realname}"
  cd build/mock_install/share/cmake/pybind11
  install -m644 *.cmake "${pkgdir}${MINGW_PREFIX}/share/cmake/${_realname}"
  
  # manpage
  cd "${srcdir}/${_realname}-${pkgver}/docs/.build/man"
  install -D -m644 "${_realname}.1" -t "${pkgdir}${MINGW_PREFIX}/share/man/man1"
  
  # license
  install -D -m644 ${srcdir}/${_realname}-${pkgver}/LICENSE -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
}
