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

_realname=coin
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=4.0.5
pkgrel=1
pkgdesc="A high-level 3D graphics toolkit, fully compatible with SGI Open Inventor 2.1 (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'archlinux: coin'
  'gentoo: media-libs/coin'
)
url="https://github.com/coin3d/coin"
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-bzip2"
             "${MINGW_PACKAGE_PREFIX}-fontconfig"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             "${MINGW_PACKAGE_PREFIX}-openal"
             "${MINGW_PACKAGE_PREFIX}-zlib")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat")
optdepends=("${MINGW_PACKAGE_PREFIX}-bzip2"
            "${MINGW_PACKAGE_PREFIX}-fontconfig"
            "${MINGW_PACKAGE_PREFIX}-freetype"
            "${MINGW_PACKAGE_PREFIX}-openal"
            "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://github.com/coin3d/coin/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('4bd23e11f0934ce8311497770d2f9e89703e6876c232831f004fcdd496e5f5a6')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

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

  export CXXFLAGS+=" -Wno-attributes"
  export CFLAGS+=" -Wno-attributes"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DCOIN_BUILD_DOCUMENTATION=ON \
    -DUSE_EXTERNAL_EXPAT=ON \
    -DCOIN_THREADSAFE=ON \
    -DCOIN_EXTRA_CFLAGS=-DCOIN_DLL \
    "../${_realname}-${pkgver}"

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

check() {
  cd "build-${MSYSTEM}"
  "${MINGW_PREFIX}"/bin/ctest.exe . || warning "Tests failed"
}

package_coin() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install "build-${MSYSTEM}"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_coin-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

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