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

_realname=qscintilla
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2.11.6
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=("GPL")
url="https://riverbankcomputing.com/software/qscintilla/intro"
depends=("${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-sip"
             "${MINGW_PACKAGE_PREFIX}-pyqt-builder"
             "${MINGW_PACKAGE_PREFIX}-pyqt5-sip"
             "${MINGW_PACKAGE_PREFIX}-python-pyqt5")
options=(!strip staticlibs !buildflags)
source=(https://www.riverbankcomputing.com/static/Downloads/QScintilla/${pkgver}/QScintilla-${pkgver}.tar.gz
        'run-command-via-sh.patch'
        'qmake-install-paths.patch'
        'designed-plugin-config-build.patch'
        'proper-module-extension.patch')
sha256sums=('e7346057db47d2fb384467fafccfcb13aa0741373c5d593bc72b55b2f0dd20a7'
            'a517b6427006c18db816dfcd087eee36e2f08c48c5147c48971f25118ea0c149'
            '517df6924d9d10104ee57372530692457cc3d4687d98ee6222f9764f0b31024b'
            'b9de93dc99426bebcb9516942b70b0cd8b17cba62de79becceb04737a1b0a2e7'
            'd574fa11c432a08653544c914d6da75d3cbc5ca8642ea16e90ed62af8c0cda4d')

prepare() {
  cd QScintilla-${pkgver}
  patch -p1 -i "${srcdir}"/run-command-via-sh.patch
  patch -p1 -i "${srcdir}"/qmake-install-paths.patch
  patch -p1 -i "${srcdir}"/designed-plugin-config-build.patch
  patch -p1 -i "${srcdir}"/proper-module-extension.patch
}

package_qscintilla() {
  pkgdesc="A port to Qt5 of Neil Hodgson's Scintilla C++ editor class (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5")

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin

  cd ${srcdir}/QScintilla-${pkgver}
  sed -e "s|%INSTALL_PREFIX%|$(cygpath -wm ${pkgdir})|g" \
    -i Qt4Qt5/qscintilla.pro \
    -i designer-Qt4Qt5/designer.pro

  cd Qt4Qt5
  ${MINGW_PREFIX}/bin/qmake CONFIG+=release CONFIG+=force_debug_info CONFIG+=separate_debug_info qscintilla.pro
  make install
  mv "${pkgdir}"/${MINGW_PREFIX}/lib/*.debug "${pkgdir}"/${MINGW_PREFIX}/bin/

  cd ../designer-Qt4Qt5
  QMAKEFEATURES=../Qt4Qt5/features \
  ${MINGW_PREFIX}/bin/qmake CONFIG+=release INCLUDEPATH+=../Qt4Qt5 designer.pro
  make install

  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
}

package_python-qscintilla() {
  pkgdesc="Python bindings for QScintilla2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python-pyqt5")
  provides=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common"
            "${MINGW_PACKAGE_PREFIX}-python3-qscintilla")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common"
             "${MINGW_PACKAGE_PREFIX}-python3-qscintilla")
  replaces=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common"
            "${MINGW_PACKAGE_PREFIX}-python3-qscintilla")

  # HACK: qmake can not find library
  cp "QScintilla-${pkgver}/Qt4Qt5/release/libqscintilla2_qt5.dll.a" \
     "QScintilla-${pkgver}/Qt4Qt5/libqscintilla2_qt5.dll.a"

  cd QScintilla-${pkgver}
  ${MINGW_PREFIX}/bin/sip-build \
    --no-make \
    --qsci-features-dir=Qt4Qt5/features \
    --qsci-include-dir=Qt4Qt5 \
    --qsci-library-dir=Qt4Qt5 \
    --api-dir=${MINGW_PREFIX}/share/qt5/qsci/api/python \
    --verbose

  cd build
  make
  MSYS2_ARG_CONV_EXCL="${MINGW_PREFIX}" \
  make INSTALL_ROOT="${pkgdir}" install -j1
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
