# Maintainer: Nazar Mishturak <nazar m x at gmail dot com>

_realname=globjects
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.0.r179.g63013594
pkgrel=1
_commit=630135941c42ca18bb47fc99ed50b62059578d3a
pkgdesc="Cross-platform C++ wrapper for OpenGL API objects. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/cginternals/globjects'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-glbinding"
         "${MINGW_PACKAGE_PREFIX}-glm")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "git")
source=(${_realname}::"git+https://github.com/cginternals/globjects.git#commit=$_commit"
        'mingw-unix-layout.patch'
        'config-export-location.patch'
        '001-fix-linking-to-glm.patch')
sha256sums=('SKIP'
            '193434a6351f71926998813afa1830812f666eff6f749e6e640fa3f4ce0d6d28'
            'b1d176d7788d973fe19a13f1d6524b75058fa8cdd66b4a65c4811feef5978ac9'
            'ee24969a0b74dedc7995d8e8ae417bc142a6df7edf8841413a0f46ace940e863')

pkgver() {
  cd "${srcdir}/${_realname}"
  git describe --long --tags | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}"
  # sed -i 's/$/\r/' "${srcdir}/mingw-unix-layout.patch"
  apply_patch_with_msg \
    mingw-unix-layout.patch \
    config-export-location.patch \
    001-fix-linking-to-glm.patch
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DOPTION_BUILD_EXAMPLES=OFF \
    -DSYSTEM_DIR_INSTALL=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DOPTION_BUILD_DOCS=ON \
    ../${_realname}

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

package () {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
