# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=osgQt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
pkgver=3.5.7
pkgrel=7
pkgdesc="OpenSceneGraph Qt5 Modules (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('LGPL')
url="https://github.com/openscenegraph/osgQt"
makedepends=("make"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
             "${MINGW_PACKAGE_PREFIX}-qt5")
options=(!strip staticlibs !buildflags)
source=(${_realname}-${pkgver}.tar.gz::https://github.com/openscenegraph/osgQt/archive/${pkgver}.tar.gz
        001-qt5-render-issue.patch
        002-osg-compatibility-old-version.patch
        003-cmake-bad-libs-parse.patch)
sha256sums=('dcc4436590639168e6470fe9c5343c82bca738d3296ebee014f40f2dc029afa1'
            'c0b2483583f0d1b8c8f828e953dfad8a5ae47453df86714a1caf091e5ba3f3d1'
            'c253e66e5153075e117c70e6014dee085ec7c6289b61505609b6912612a4c35b'
            '599283f2269f532faa22d160c0f65747c29333cf3bf2cd64e3f01ad6c6370a09')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-qt5-render-issue.patch
  patch -p1 -i ${srcdir}/002-osg-compatibility-old-version.patch
  patch -p1 -i ${srcdir}/003-cmake-bad-libs-parse.patch
}
build() {
  plain "No build step"
}

package_prog() {
  local _buildtype=${1}
  local _builddir=${_buildtype}-${MINGW_CHOST}
  local _dsuf=

  if [[ "${_buildtype}" != "Release" ]]; then
    _dsuf=d
  fi

  [[ -d ${srcdir}/${_builddir} ]] && rm -rf ${srcdir}/${_builddir}
  mkdir -p ${srcdir}/${_builddir} && cd ${srcdir}/${_builddir}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_buildtype} \
    -DBUILD_OSG_EXAMPLES=ON \
    ../${_realname}-${pkgver}

  make DESTDIR=${pkgdir} -j1 install
  cp -f ${pkgdir}${MINGW_PREFIX}/lib/libosgQt{5,}${_dsuf}.dll.a

  if [[ "${_buildtype}" == "Debug" ]]; then
    rm -rf ${pkgdir}${MINGW_PREFIX}/include
    rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  fi
}

package_osgQt() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt5" "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph")
  package_prog Release
}

package_osgQt-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt5" "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug")
  package_prog Debug
}

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