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

_realname=solid3
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_commit=c53f6bb1eaaafb1cfb305ef71b1c3a2edb4844e6
pkgver=3.5.8
pkgrel=1
pkgdesc="A software library for performing intersection tests and proximity queries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/dtecta/solid3"
license=('GPL' 'QPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qhull"
             "patch")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/dtecta/solid3/archive/${_commit}.tar.gz"
        "disable-examples.patch"
        "use-external-qhull.patch")
sha256sums=('fccb1fb30d8836d1c9216d3b36042b0a9651697fd743a4c4e9251dfea59ac964'
            '2c663db880d38c862c0b33eae9a44be3918547cff35196d0077365b5aa376a2b'
            '6dce2fea19456640a81a935b0405ae9b2d5d1167816578b86dd8299abbb47241')

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

  patch -Np1 -i "${srcdir}"/disable-examples.patch
  patch -Np1 -i "${srcdir}"/use-external-qhull.patch
}

build() {
  cd "${srcdir}"/${_realname}-${_commit}
  [[ -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[@]}" \
    -DDYNAMIC_SOLID=ON \
    -DUSE_DOUBLES=ON \
    ../${_realname}-${_commit}

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

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

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

  install -Dm644 "${srcdir}/${_realname}-${_commit}/LICENSE_GPL.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE_GPL.txt"
  install -Dm644 "${srcdir}/${_realname}-${_commit}/LICENSE_QPL.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE_QPL.txt"
  install -Dm644 "${srcdir}/${_realname}-${_commit}/README.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/README.md"
}
