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

_realname=freeglut
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pre=
_ver=3.2.1
pkgver=${_ver}${_pre}
pkgrel=2
pkgdesc="Freeglut allows the user to create and manage windows containing OpenGL contexts (mingw32-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://freeglut.sourceforge.io/"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('strip' '!libtool' 'staticlibs')
source=("https://downloads.sourceforge.net/freeglut/${_realname}-${_ver}${_pre}.tar.gz"
        "003-freeglut-3.2.1-install-glut-h.patch")
sha256sums=('d4000e02102acaf259998c870e25214739d1f16f67f99cb35e4f46841399da68'
            '0d091fba73641bdc51d28763101452f606c26aed30c9ac1682a19ba9306de8aa')
noextract=(${_realname}-${_ver}${_pre}.tar.gz)

prepare() {
  # Clean up old sources so re-patching doesn't fail.
  [[ -d ${srcdir}/${_realname}-${_ver}${_pre} ]] && rm -rf ${srcdir}/${_realname}-${_ver}${_pre}
  tar -xzvf ${_realname}-${_ver}${_pre}.tar.gz -C ${srcdir}
  # Upstreaming at https://github.com/dcnieho/FreeGLUT/pull/41
  cd ${_realname}-${_ver}${_pre}
  patch -p1 -i "${srcdir}"/003-freeglut-3.2.1-install-glut-h.patch
}

build() {
  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  CFLAGS+=" -fcommon"  # work around gcc 10 being stricter here

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}-${_ver}${_pre}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
