# Maintainer: Markus Rickert <rickert@fortiss.org>

_realname=ode
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.16.2
pkgrel=1
pkgdesc="A library for simulating articulated rigid body dynamics (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://bitbucket.org/odedevs/ode"
license=('LGPL', 'BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('strip' 'staticlibs')
source=("https://bitbucket.org/odedevs/ode/downloads/${_realname}-${pkgver}.tar.gz")
sha256sums=('b26aebdcb015e2d89720ef48e0cb2e8a3ca77915f89d853893e7cc861f810f22')

build() {
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}"/build-${CARCH}

  ${MINGW_PREFIX}/bin/ctest
}

package() {
  cd "${srcdir}"/build-${CARCH}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE-BSD.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-BSD.txt"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/GIMPACT/GIMPACT-LICENSE-BSD.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/GIMPACT/GIMPACT-LICENSE-BSD.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/GIMPACT/GIMPACT-LICENSE-LGPL.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/GIMPACT/GIMPACT-LICENSE-LGPL.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/libccd/BSD-LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/libccd/BSD-LICENSE"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/OPCODE/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/OPCODE/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/ou/LICENSE.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ou/LICENSE.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/ou/LICENSE-BSD.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ou/LICENSE-BSD.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/ou/LICENSE-LESSER.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ou/LICENSE-LESSER.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/ou/LICENSE-ZLIB.TXT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/ou/LICENSE-ZLIB.TXT"
}
