# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=mgba
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-mgba")
pkgver=0.10.5
pkgrel=1
pkgdesc='A Nintendo Gameboy Advance Emulator focusing on speed and accuracy (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://mgba.io'
msys2_repository_url="https://github.com/mgba-emu/mgba"
license=('spdx:MPL-2.0')
depends=(
  "${MINGW_PACKAGE_PREFIX}-cc-libs"
  "${MINGW_PACKAGE_PREFIX}-ffmpeg"
  "${MINGW_PACKAGE_PREFIX}-libpng"
  "${MINGW_PACKAGE_PREFIX}-libzip"
  "${MINGW_PACKAGE_PREFIX}-lua"
  "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
  "${MINGW_PACKAGE_PREFIX}-SDL2"
  "${MINGW_PACKAGE_PREFIX}-zlib"
  "${MINGW_PACKAGE_PREFIX}-libepoxy"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-qt5-tools"
  "${MINGW_PACKAGE_PREFIX}-SDL2"
)
source=("${_realname}-${pkgver}.tar.gz::https://github.com/mgba-emu/mgba/archive/${pkgver}.tar.gz")
sha256sums=('91d6fbd32abcbdf030d58d3f562de25ebbc9d56040d513ff8e5c19bee9dacf14')

build() {
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake -B build -S "${_realname}-${pkgver}" \
    -DCMAKE_BUILD_TYPE=None \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DWIN32_UNIX_PATHS=ON \
    -DUSE_DISCORD_RPC=OFF \
    -Wno-dev
  cmake --build build
}

package() {
  DESTDIR="${pkgdir}" cmake --install build --component libmgba
  DESTDIR="${pkgdir}" cmake --install build --component mgba-dev
  DESTDIR="${pkgdir}" cmake --install build --component mgba-sdl
  DESTDIR="${pkgdir}" cmake --install build --component mgba-qt

  # fixup broken lib installation
  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  mv "${pkgdir}${MINGW_PREFIX}/lib/libmgba"*.dll "${pkgdir}${MINGW_PREFIX}/bin"
  cp build/libmgba.dll.a "${pkgdir}${MINGW_PREFIX}/lib"
}
