# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=glfw
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.3.4
pkgrel=1
pkgdesc="A free, open source, portable framework for OpenGL application development (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.glfw.org/"
license=('custom:ZLIB')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers")
options=('strip' '!debug' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/glfw/glfw/archive/${pkgver}.tar.gz"
        001-3.2-cmake-suffix.patch
        003-3.2-cmake-install.patch)
sha256sums=('cc8ac1d024a0de5fd6f68c4133af77e1918261396319c24fd697775a6bc93b63'
            '9eb7f6d494ff810fc5d4c0939545ce98fa2a4541e538146a1f85227328b430b0'
            '706d7b81d0e37e881bef33bcd330a89f7fa63031fbeb8b4f1790f900fa803739')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i ${srcdir}/001-3.2-cmake-suffix.patch
  patch -Np1 -i ${srcdir}/003-3.2-cmake-install.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

  # Build shared libs
  [[ -d "${srcdir}/shared-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/shared-${MINGW_CHOST}"
  mkdir -p "${srcdir}/shared-${MINGW_CHOST}" && cd "${srcdir}/shared-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DBUILD_SHARED_LIBS=ON \
    -DGLFW_BUILD_DOCS=OFF \
    ../${_realname}-${pkgver}

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

  # Build static libs
  [[ -d "${srcdir}/static-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/static-${MINGW_CHOST}"
  mkdir -p "${srcdir}/static-${MINGW_CHOST}" && cd "${srcdir}/static-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DBUILD_SHARED_LIBS=OFF \
    -DGLFW_BUILD_DOCS=OFF \
    ../${_realname}-${pkgver}

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

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

  cd "${srcdir}/shared-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  # License
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
