# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# contributers: Zach Bacon <zachbacon@budgiemedia.rocks>

_realname=SDL2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.14
pkgrel=2
pkgdesc="A library for portable low-level access to a video framebuffer, audio output, mouse, and keyboard (Version 2) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://libsdl.org"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-vulkan")
options=('staticlibs' 'strip')
source=("https://libsdl.org/release/SDL2-${pkgver}.tar.gz"
        001-fix-cmake-target-relocation.patch
        002-fix-link-order.patch
        003-fix-static-library-name.patch
        005-sdl-config-win-paths.patch)
sha256sums=('d8215b571a581be1332d2106f8036fcb03d12a70bae01e20f424976d275432bc'
            '76f767cd63df7d263976cee399496b1dfc5bca1d29fc3eb8e9344b5f0bfb0bf6'
            '4a3906a9b9989dba4967454512921660e0fb275c35263d8ef7d7b34fa5a95e57'
            'a327673cd9d0e60d762860cf8dc96134b6b283314640e5803d918e7f5cd8f9ee'
            '258b2e90da4bd38f61fab37f35cee25b84a45c4bb25d27bc0c05f57ab26d7d64')
validpgpkeys=('1528635D8053A57F77D1E08630A59377A7763BE6') # Sam Lantinga

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-cmake-target-relocation.patch
  patch -p1 -i ${srcdir}/002-fix-link-order.patch
  patch -p1 -i ${srcdir}/003-fix-static-library-name.patch
  patch -p1 -i ${srcdir}/005-sdl-config-win-paths.patch
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DVIDEO_VULKAN=ON \
    ../${_realname}-${pkgver}

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

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install
}
