# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=freeglut
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.8.0
pkgrel=1
pkgdesc="Freeglut allows the user to create and manage windows containing OpenGL contexts (mingw32-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://freeglut.sourceforge.io/"
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/FreeGLUTProject/freeglut/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "003-freeglut-3.2.1-install-glut-h.patch"
        "004-pkg-config.patch")
sha256sums=('674dcaff25010e09e450aec458b8870d9e98c46f99538db457ab659b321d9989'
            '0d091fba73641bdc51d28763101452f606c26aed30c9ac1682a19ba9306de8aa'
            'c867bf702a2507e28139659f5c0fa54c83588aa7402a45c5031d3f15d8918148')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/003-freeglut-3.2.1-install-glut-h.patch
  patch -p1 -i "${srcdir}"/004-pkg-config.patch
}

build() {
  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[@]}" \
    -S "${_realname}-${pkgver}" \
    -B "build-${MSYSTEM}"

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

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

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