# Contributor: Martell Malone <martell malone at gmail dot com>

_realname=glew
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.0
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="GLEW, The OpenGL Extension Wrangler Library (mingw-w64)"
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
license=('Modified BSD/MIT/GPL')
url="https://glew.sourceforge.io/"
options=('strip' '!libtool' 'staticlibs')
source=("https://sourceforge.net/projects/glew/files/glew/${pkgver}/${_realname}-${pkgver}.tgz"
        001-makefile.patch)
sha256sums=('d4fc82893cfb00109578d0a1a2337fb8ca335b3ceccf97b97e5cc7f08e4353e1'
            'ab2e75b5161d60de68bd4b9b9ab1cc94d1b3db1ffc87e8a72f7a82f3ccfd440d')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/001-makefile.patch
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${pkgver}/build/cmake

  make
}

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

  mkdir -p ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  sed \
    -e "s|@prefix@|${MINGW_PREFIX}|g" \
    -e "s|@libdir@|${MINGW_PREFIX}/lib|g" \
    -e "s|@exec_prefix@|${MINGW_PREFIX}/bin|g" \
    -e "s|@includedir@|${MINGW_PREFIX}/include|g" \
    -e "s|@version@|${pkgver}|g" \
    -e "s|@cflags@||g" \
    -e "s|@libname@|glew32|g" \
    -e "s|@libgl@||g" \
    -e "s|@requireslib@||g" \
    ${srcdir}/${_realname}-${pkgver}/glew.pc.in \
    > ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/glew.pc

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
