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

_realname=osgbullet
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-git")
_ver_base=3.0.0
pkgver=3.0.0.265
pkgrel=1
pkgdesc="Bullet physics and OpenSceneGraph integration (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('LGPL')
url="https://github.com/mccdo/osgbullet/"
depends=("${MINGW_PACKAGE_PREFIX}-bullet"
         "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
         "${MINGW_PACKAGE_PREFIX}-osgworks")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
             "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug"
             "${MINGW_PACKAGE_PREFIX}-osgworks"
             "${MINGW_PACKAGE_PREFIX}-osgworks-debug")
options=(!strip staticlibs !buildflags)
source=("${_realname}"::"git+https://github.com/mccdo/osgbullet.git")
sha256sums=('SKIP')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.%s" "${_ver_base}" "$(git rev-list --count HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
}

build() {
  plain "No build step"
}

package_osgbullet-git() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-svn")
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-svn)

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DosgWorks_DIR=${MINGW_PREFIX}/lib \
    ../${_realname}

  make DESTDIR=${pkgdir} install
}

package_osgbullet-debug-git() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-git=${pkgver}" "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug" "${MINGW_PACKAGE_PREFIX}-osgworks-debug")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug" "${MINGW_PACKAGE_PREFIX}-${_realname}-degug-svn")
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-debug-svn)

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Debug \
    -DosgWorks_DIR=${MINGW_PREFIX}/lib \
    ../${_realname}

  make DESTDIR=${pkgdir} install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/flagpoll
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/*.cmake
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/osgBullet/data
  rm -rf ${pkgdir}${MINGW_PREFIX}/share/osgBullet/extra
}

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