# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ingo Maindorfer <ingo@liquidcooling.de>
# Based on work by AlexWMF <alexxwmf@gmail.com>

_realname=qwt
pkgbase=mingw-w64-${_realname}-qt5
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5")
pkgver=6.1.6
pkgrel=1
pkgdesc="Qt Widgets for Technical Applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=("custom:${_realname}")
url="https://qwt.sourceforge.io/"
depends=("${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=(!strip staticlibs !buildflags)
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.bz2"
        "qwtconfig-msys2.pri"
        001-separate-debug-info-build.patch
        002-link-to-release.patch)
sha256sums=('99460d31c115ee4117b0175d885f47c2c590d784206f09815dc058fbe5ede1f6'
            '5b85185c42bde551f406ab30fd3ea664bf6574e44caef65cad49cb9916597796'
            '0e103a7ae290bb706c87135b9f0fffc9f2fb83eea57922973817b220c0f45e9d'
            '6e0101d2c2897c94da6284ba0b929a7a39d82120313e6cd6353fd5e3aaf4b059')

prepare() {
  # copy our config file to the right place
  cp ${srcdir}/qwtconfig-msys2.pri ${srcdir}/${_realname}-${pkgver}/qwtconfig.pri

  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-separate-debug-info-build.patch
  patch -p1 -i ${srcdir}/002-link-to-release.patch
}

package() {
  local PREFIX_WIN=$(cygpath -am ${pkgdir}${MINGW_PREFIX})

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  cp -rf ${srcdir}/${_realname}-${pkgver} ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}
  sed -s "s|%VALUE_QWT_INSTALL_PREFIX%|${PREFIX_WIN}|g" -i qwtconfig.pri
  ${MINGW_PREFIX}/bin/qmake CONFIG-=debug_and_release CONFIG+=release qwt.pro

  make # VERBOSE=1
  make -j1 install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.{debug,dll} "${pkgdir}${MINGW_PREFIX}"/bin/
  
  # install debug info
  #install -Dm644 lib/*.debug -t "${pkgdir}${MINGW_PREFIX}"/bin/

  local PREFIX_WIN=$(cygpath -am ${pkgdir}${MINGW_PREFIX})

  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt5/mkspecs/features/qwtconfig.pri
}
