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

_realname=cglm
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.9.6
pkgrel=1
pkgdesc="OpenGL Mathematics (glm) for C (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/recp/cglm"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx_rtd_theme")
source=("https://github.com/recp/cglm/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('be5e7d384561eb0fca59724a92b7fb44bf03e588a7eae5123a7d796002928184')

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCGLM_SHARED=OFF \
    -DCGLM_STATIC=ON \
    -DCGLM_USE_TEST=ON \
    ../${_realname}-${pkgver}
  cmake --build .

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCGLM_SHARED=ON \
    -DCGLM_STATIC=OFF \
    -DCGLM_USE_TEST=ON \
    ../${_realname}-${pkgver}
  cmake --build .


  cd "${srcdir}/${_realname}-${pkgver}/docs"

  ${MINGW_PREFIX}/bin/sphinx-build source ${_realname}
}

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

  ctest . || true

  cd "${srcdir}/build-${MSYSTEM}-shared"

  ctest . || true
}

package_cglm() {
  DESTDIR="${pkgdir}" cmake --install "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" cmake --install "${srcdir}/build-${MSYSTEM}-shared"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/LICENSE \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  # split docs
  mkdir -p "dest${MINGW_PREFIX}/share/doc/"
  cp -r "${srcdir}/${_realname}-${pkgver}/docs/${_realname}" "dest${MINGW_PREFIX}/share/doc/${_realname}"
}

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