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

_realname=qscintilla
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-qt5"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-qt6")
pkgver=2.14.1
pkgrel=4
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://riverbankcomputing.com/software/qscintilla/intro"
msys2_references=(
  'archlinux: qscintilla-qt5'
)
license=("spdx:GPL-3.0-only")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-sip"
             "${MINGW_PACKAGE_PREFIX}-pyqt-builder"
             "${MINGW_PACKAGE_PREFIX}-python-pyqt5"
             "${MINGW_PACKAGE_PREFIX}-python-pyqt6"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
source=(https://www.riverbankcomputing.com/static/Downloads/QScintilla/${pkgver}/QScintilla_src-${pkgver}.tar.gz)
sha256sums=('dfe13c6acc9d85dfcba76ccc8061e71a223957a6c02f3c343b30a9d43a4cdd4d')

build() {
  ## Build for Qt5
  [[ -d "${srcdir}"/build-${MSYSTEM}-qt5 ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-qt5
  cp -r "${srcdir}"/QScintilla_src-${pkgver} "${srcdir}"/build-${MSYSTEM}-qt5
  cd "${srcdir}"/build-${MSYSTEM}-qt5

  # build src
  cd "${srcdir}/build-${MSYSTEM}-qt5/src"
  export QMAKEFEATURES="${srcdir}/build-${MSYSTEM}-qt5/src/features/"
  ${MINGW_PREFIX}/bin/qmake
  make PREFIX=${MINGW_PREFIX}

  # build designer
  cd "${srcdir}/build-${MSYSTEM}-qt5/designer"
  ${MINGW_PREFIX}/bin/qmake INCLUDEPATH+=../src QMAKE_LIBDIR+=../src/release
  make PREFIX=${MINGW_PREFIX}

  # build python bindings
  cd "${srcdir}/build-${MSYSTEM}-qt5/Python"
  cp pyproject{-qt5,}.toml

  MSYS2_ARG_CONV_EXCL="--api-dir=;" \
  ${MINGW_PREFIX}/bin/sip-build \
    --no-make \
    --qsci-features-dir=../src/features \
    --qsci-include-dir=../src \
    --qsci-library-dir=../src/release \
    --api-dir=${MINGW_PREFIX}/share/qt5/qsci/api/python
  make -C build PREFIX=${MINGW_PREFIX}

  ## Build for Qt6
  [[ -d "${srcdir}"/build-${MSYSTEM}-qt6 ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-qt6
  cp -r "${srcdir}"/QScintilla_src-${pkgver} "${srcdir}"/build-${MSYSTEM}-qt6
  cd "${srcdir}"/build-${MSYSTEM}-qt6

  # build src
  cd "${srcdir}/build-${MSYSTEM}-qt6/src"
  export QMAKEFEATURES="${srcdir}/build-${MSYSTEM}-qt6/src/features/"
  ${MINGW_PREFIX}/bin/qmake6
  make PREFIX=${MINGW_PREFIX}

  # build designer
  cd "${srcdir}/build-${MSYSTEM}-qt6/designer"
  ${MINGW_PREFIX}/bin/qmake6 INCLUDEPATH+=../src QMAKE_LIBDIR+=../src/release
  make PREFIX=${MINGW_PREFIX}

  # build python bindings
  cd "${srcdir}/build-${MSYSTEM}-qt6/Python"
  cp pyproject{-qt6,}.toml

  MSYS2_ARG_CONV_EXCL="--api-dir=;" \
  ${MINGW_PREFIX}/bin/sip-build \
    --no-make \
    --qsci-features-dir=../src/features \
    --qsci-include-dir=../src \
    --qsci-library-dir=../src/release \
    --api-dir=${MINGW_PREFIX}/share/qt6/qsci/api/python \
    --qmake=${MINGW_PREFIX}/bin/qmake6.exe

  make -C build PREFIX=${MINGW_PREFIX}
}

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

  cd "${srcdir}/build-${MSYSTEM}-qt5"

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find src designer -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find src designer -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make -C "src" INSTALL_ROOT="${pkgdir}" install
  make -C "designer" INSTALL_ROOT="${pkgdir}" install

  [[ -d "${pkgdir}"/${MINGW_PREFIX}/bin/ ]] || mkdir -p "${pkgdir}"/${MINGW_PREFIX}/bin/
  mv "${pkgdir}"/${MINGW_PREFIX}/lib/*.dll "${pkgdir}"/${MINGW_PREFIX}/bin/

  install -Dm644 "${srcdir}/build-${MSYSTEM}-qt5/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt5/LICENSE"
}

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

  cd "${srcdir}/build-${MSYSTEM}-qt5"
  make -C "Python/build" INSTALL_ROOT="${pkgdir}" install

  install -Dm644 "${srcdir}/build-${MSYSTEM}-qt5/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}-qt5/LICENSE"
}

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

  cd "${srcdir}/build-${MSYSTEM}-qt6"

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find src designer -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find src designer -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make -C "src" INSTALL_ROOT="${pkgdir}" install
  make -C "designer" INSTALL_ROOT="${pkgdir}" install

  [[ -d "${pkgdir}"/${MINGW_PREFIX}/bin/ ]] || mkdir -p "${pkgdir}"/${MINGW_PREFIX}/bin/
  mv "${pkgdir}"/${MINGW_PREFIX}/lib/*.dll "${pkgdir}"/${MINGW_PREFIX}/bin/

  install -Dm644 "${srcdir}/build-${MSYSTEM}-qt6/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt6/LICENSE"
}

package_python-qscintilla-qt6() {
  pkgdesc="Python bindings for QScintilla2 - PyQt6 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt6"
           "${MINGW_PACKAGE_PREFIX}-python-pyqt6")

  cd "${srcdir}/build-${MSYSTEM}-qt6"
  make -C "Python/build" INSTALL_ROOT="${pkgdir}" install

  install -Dm644 "${srcdir}/build-${MSYSTEM}-qt6/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}-qt6/LICENSE"
}

# 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;
